Day07
1. Background
今天是学习java的第七天,今天我学习到了数组。
2. Description
2.1 About
数组是一种数据结构,用来存储同一类型值的集合。具体的表现方式和C语言和很像,在此就不再赘述。
需要注意的是java声明数组和C语言不同
int[] a;
int[] a = new int[100];
在上面代码中,第一句声明了数组变量,其中数据类型紧跟[],这里与C语言有很大的不同。但是这句只声明了变量a,并没有将a初始化为真正的数组,所以要用 new
运算符来创建数组.
PS:想C语言那样[]在变量名之后的方法也可以创建数组,只不过java程序员更多的喜欢使用 datetype[] 的方法
当创建一个数字数组时,所有元素都会被初始化为0,如果是Boolean类型数组,则会被初始化false。对象数组的元素则被初始化为一个特殊值null,表示这些元素还没有任何对象。
一旦创建了数组就不可再改变数组大小,如果想要在运行得时候改变数组大小,可以使用另一种数据结构——数组列表(array list)
2.2 java.util.Arrays
这是java常用得一个库,如名字一样,用于数组的运算。
-
sort()
public static void sort(int[] a) {} public static void sort(int[] a, int fromIndex, int toIndex) {}
只有一个参数的
sort()
对给定的数组参数按照升序排序。有三个参数的sort()
对第一个数组参数的指定索引范围进行排序,该范围由后两个参数指定,从第二个参数fromIndex
到第三个参数toIndex
(不包含该索引)。Arrays
中的sort()
方法对除boolean
外的所有 7 基本数据类型进行了重载,所以这里的int
可以替换为其它 6 种基本数据类型种的任何一种。这些sort()
方法使用的排序算法属于快速排序。public static void sort(Object[] a) {} public static void sort(Object[] a, int fromIndex, int toIndex) {}
输入参数为
Object
类型的sort()
方法,所有参数数组中的元素都必须实现Comparable
接口,并且所有的元素之间必须可以相互比较(即e1.compareTo(e2)
对任意的两个数组元素e1
和e2
都不能产生异常ClassCastException
)。其排序方式属于归并排序。public static <T> void sort(T[] a, Comparator<? super T> c) {} public static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) {}
这是使用泛型的
sort()
方法,它需要根据给定的比较器Comparator
所得到的顺序对给定数组进行排序。其排序方式属于归并排序。 -
parallelSort()
public static void parallelSort(byte[] a) {} public static void parallelSort(byte[] a, int fromIndex, int toIndex) {}
parallelSort()
的各参数意义同sort()
一样,返回数组的升序排序结果。parallelSort()
通过并行的方式使用归并排序,它将原数组不断分解为子数组,分别对子数组进行排序。当子数组长度达到一个最小粒度时,则通过适当的Array.sort()
方法对其进行排序。该算法需要一个不大于原始数组大小的工作空间。和
sort()
一样,Arrays
中的parallelSort()
方法对除boolean
外的所有 7 基本数据类型进行了重载,所以这里的byte
可以替换为其它 6 种基本数据类型种的任何一种。public static <T extends Comparable<? super T>> void parallelSort(T[] a) {} public static <T extends Comparable<? super T>> void parallelSort(T[] a, int fromIndex, int toIndex) {}
输入参数为泛型的
parallelSort()
方法,所有参数数组中的元素都必须实现Comparable
接口,并且所有的元素之间必须可以相互比较(即e1.compareTo(e2)
对任意的两个数组元素e1
和e2
都不能产生异常ClassCastException
)。其排序方式属于归并排序。public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) {} public static <T> void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) {}
这是使用泛型的
parallelSort()
方法,它需要根据给定的比较器Comparator
所得到的顺序对给定数组进行排序。其排序方式属于归并排序。 -
parallelPrefix()
public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) {} public static <T> void parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator<T> op) {
使用提供的函数操作,对数组进行累积操作,相当于连续空间种的积分。比如输入的原始数组是
[2, 1, 0, 3]
,那么累积操作后的数组是[2, 3, 3, 6]
。对于含有两个参数的parallelPrefix
,第一个参数array
是原始数组,第二个参数op
提供执行累积操作的函数。对于含有四个参数的parallelPrefix
,array
是原始数组,第二和第三个参数指定进行累积操作数组范围为从fromIndex
到toIndex
(不包含该索引),第四个参数op
提供执行累积操作的函数。Arrays
还提供了这两种形式的parallelPrefix()
方法对于int/long/double
的重载方法,参数意义和方法作用类似。 -
binarySearch()
// 使用二分搜索在整个有序数组 a 中寻找 key 值的所在位置索引 public static int binarySearch(long[] a, long key) {} // 使用二分搜索在整个有序数组 a 的指定范围 [fromIndex, toIndex) 中寻找 key 值的所在位置索引 public static int binarySearch(long[] a, int fromIndex, int toIndex, long key) {}
使用二分搜索在有序数组
a
中寻找指定参数key
值的所在位置索引。如果找得到,则返回找到的索引值;如果没有找到,则返回值为(-(insertion point)-1)
,这里的insertion point
就是key
应该插入到有序数组a
中的位置,保证数组a
仍然有序。这个返回方式确实有些怪异,它的目的是保证只有在数组a
中找到key
时,返回值才一定时整数,而没找到情况下返回值一定是负数(该负数包含了key
的插入点信息)。Arrays
中的binarySearch()
方法对除boolean
外的所有 7 基本数据类型进行了重载,所以这里的long
可以替换为其它 6 种基本数据类型种的任何一种。Arrays
还提供了binarySearch()
方法对Object
类型以及泛型的重载方法。提供的方式和sort()
类似,具体形式如下。public static int binarySearch(Object[] a, Object key){} public static binarySearch(Object[] a, int fromIndex, int toIndex, Object key) {} public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {} public static <T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) {}
-
equals()
// 判断对于两个数组中的所有元素时候满足相等的要求。 public static boolean equals(long[] a, long[] a2) {} // 判断对于两个参数指定范围中的元素是否满足相等的要求。 public static boolean equals(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex) {
判断两个数组是否相等,它的涵义是,两个数组中是否包含相同的元素且排列顺序也相同。
Arrays
提供了equals()
方法对 8 种基本数据类型的重载方法。Arrays
还提供了equals()
方法对Object
类型以及泛型的重载方法。作用相同,不再赘述。对于使用泛型的重载方法,需要提供一个Comparator<? super T>
参数作为比较器判断两个元素是否相等,cmp.compare(e1, e2) == 0
表示e1
和e2
相等。public static boolean equals(Object[] a, Object[] a2) {} public static boolean equals(Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex) {} public static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp) {} public static <T> boolean equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) {
-
fill()
public static void fill(long[] a, long val) {} public static void fill(long[] a, int fromIndex, int toIndex, long val) {}
使用指定数值
val
填充数组a
。对于两个参数的fill()
方法,表示使用val
赋值a
中的每个元素。对于四个参数的fill()
方法,表示使用val
赋值指定a
中范围的元素,即从fromIndex
到toIndex
(不包含该索引)。Arrays
提供了fill()
方法对 8 种基本数据类型以及Object
类型的重载方法。作用相同,不再赘述。 -
copyOf()
public static boolean[] copyOf(boolean[] original, int newLength) {}
复制数组
original
到长度为newLength
的新数组中。在原数组和新数组中同时有效的索引处,两者具有相等的元素值,在新数组中有效而在原数组中无效的索引处,元素置为默认值。Arrays
提供了copyOf()
方法对 8 种基本数据类型以及泛型的重载方法。作用相同,不再赘述。 -
copyOfRange()
public static byte[] copyOfRange(byte[] original, int from, int to) {}
复制数组
original
指定范围(从from
到to
(不包含to
索引))的元素到新数组中。from
的值必须处于 0 到original.length
之间,它对应于新数组的起始索引。to
的值必须大于或等于from
的值,to
可以超过original.length
,超出原数组范围的索引,其对应的新数组中的索引处元素置为默认值。Arrays
提供了copyOf()
方法对 8 种基本数据类型以及泛型的重载方法。作用相同,不再赘述。 -
asList()
public static <T> List<T> asList(T... a) {}
通过给定数组
a
得到一个定长的List
对象,即将一个数组转换为一个List
。此方法还可以方便地创建一个初始状态包含多个元素的定长List
对象,像这样:List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
-
hashCode()
public static int hashCode(long a[]) {}
根据参数数组
a
的内容计算一个哈希码。对于任意两个数组
a
和b
,hashCode(a)==hashCode(b)
跟Arrays.equals(a, b)
结果相同。该方法返回的哈希码,跟包含与
a
相同元素且排列顺序相同的List
对象调用hashCode()
方法得到的哈希码,具有相同的值。如果a
为null
,则方法返回 0。Arrays
提供了hashCode()
方法对 8 种基本数据类型以及Object
类型的重载方法。作用相同,不再赘述。 -
deepHashCode()
public static int deepHashCode(Object a[]) {}
根据参数数组
a
的深层内容计算一个哈希码。如果数组a
的元素也是数组,哈希值的计算也会考虑到该数组的内容。对于任意两个数组
a
和b
,deepHashCode(a)==deepHashCode(b)
跟Arrays.deepEquals(a, b)
结果相同。 -
deepEquals()
public static boolean deepEquals(Object[] a1, Object[] a2) {}
判断两个数组
a1
和a2
是否深层相等。返回true
则相等,返回false
则不相等。与equals()
方法不同,该方法适用于任意深度嵌套的数组。 -
toString()
public static String toString(long[] a) {}
返回数组
a
内容的字符串表示形式。Arrays
提供了toString()
方法对 8 种基本数据类型以及Object
类型的重载方法。作用相同,不再赘述。 -
deepToString()
public static String deepToString(Object[] a) {}
返回数组
a
的深层内容的字符串表示形式。就是说,如果数组a
的元素也是数组,那么返回的字符串中也表示除了其包含的内容。如果数组
a
的某个元素是它本身的引用,返回的字符串中对应位置表示为[...]
的形式。 -
setAll()
public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {}
通过给定的生成器函数
generator
,设置数组array
的所有元素。Arrays
提供了``setAll()方法对
int/long/double`类型的重载方法。 -
parallelSetAll()
public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {}
通过给定的生成器函数
generator
,并行地设置数组array
的所有元素。Arrays
提供了parallelSetAll()
方法对int/long/double
类型的重载方法。 -
spliterator()
// 返回一个覆盖整个数组的 Spliterator 对象 public static <T> Spliterator<T> spliterator(T[] array) {} // 回一个覆盖数组指定范围(从 startInclusive 到 endExclusive (不包含该索引))的 Spliterator 对象。 public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) {}
返回一个覆盖数组
array
的Spliterator
对象。Arrays
提供了spliterator()
方法对int/long/double
类型的重载方法。 -
stream()
// 返回一个以整个数组作为源的 Stream 对象 public static <T> Stream<T> stream(T[] array) {} // 返回一个以数组指定范围(从 startInclusive 到 endExclusive(不包含该索引))作为源的 Stream 对象 public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) {}
返回以数组
array
作为源的顺序Stream
。Arrays
提供了stream()
方法对int/long/double
类型的重载方法。
以下是从 Java 9 开始加入的方法:
-
compare()
// 按字典序比较两个数组 a 和 b public static int compare(boolean[] a, boolean[] b) {} // 按字典序比较数组 a 的指定范围 [aFromIndex, aToIndex) 和 b 的指定范围 [bFromIndex, bToIndex) public static int compare(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex) {}
按字典序比较两个数组
a
和b
。简单来说,就是比较两数组按索引从前到后第一个不相同的元素值之间的大小。返回 0 表示连个数组含有相同值且按相同顺序排列的元素;返回负数值表示第一个参数数组按字典序小于第二个参数数组;返回正数值表示第一个参数数组按字典序大于第二个参数数组。Arrays
提供了compare()
方法对 8 种基本数据类型的重载方法。Arrays
提供了compare()
方法使用泛型的重载方法,形式如下:// 前提是 T[] 中的元素都是可比较的 public static <T extends Comparable<? super T>> int compare(T[] a, T[] b) {} public static <T extends Comparable<? super T>> int compare( T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) {} // 使用给定的比较器 cmp 对数组中的元素进行比较 public static <T> int compare(T[] a, T[] b, Comparator<? super T> cmp) {} public static <T> int compare( T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) {}
-
compareUnsigned()
public static int compareUnsigned(byte[] a, byte[] b) {} public static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex) {}
作用与
compare()
差不多,区别是compareUnsigned()
方法将数组中的元素视为无符号数。Arrays
提供了compareUnsigned()
方法对byte/short/int/long
四种有符号数据类型的重载方法。 -
mismatch()
// 将数组元素看作无符号数,按字典序比较两个数组 a 和 b public static int mismatch(boolean[] a, boolean[] b) {} // 将数组元素看作无符号数,按字典序比较数组 a 的指定范围 [aFromIndex, aToIndex) 和 b 的指定范围 [bFromIndex, bToIndex) public static int mismatch(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex) {}
找到两个数组
a
和b
按照索引顺序从前到后第一个不相同的元素位置,返回该位置的索引。如果没有找到不同的元素,则返回 -1,这表示两数组完全相同。Arrays
提供了mismatch()
方法对 8 种基本数据类型的重载方法。Arrays
提供了mismatch()
方法对Object
类型以及使用泛型的重载方法,形式如下:public static int mismatch(Object[] a, Object[] b) {} public static int mismatch( Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex) {} public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp) {} public static <T> int mismatch( T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) {}
以上库中的函数均来自网络:https://www.cnblogs.com/alterwl/p/15187581.html
今天的代码中,用到的函数是deepToString
,作用是把数组以字符串的形式打印出来。
3. Code
package basic;
import java.util.Arrays;
/**
* This is the seventh code. Names and comments should follow my style stritly.
*
* @author Fan Min minfanphd@163.com.
*/
public class Day07 {
/**
*****************
* The entrance of the program.
*
* @param args Not used now.
*****************
*/
public static void main(String[] args) {
matrixElementSumTest();
matrixAdditionTest();
} // Of main
/**
*****************
* Sum the elements of a matrix.
*
* @param paraMatrix The given matrix.
* @return The sum of all its elements.
*****************
*/
public static int matrixElementSum(int[][] paraMatrix) {
int resultSum = 0;
for (int i = 0; i < paraMatrix.length; i++) {
for (int j = 0; j < paraMatrix[0].length; j++) {
resultSum += paraMatrix[i][j];
} // Of for j
} // Of for i
return resultSum;
} // Of matrixElementSum
/**
*****************
* Unit test for respective method.
*****************
*/
public static void matrixElementSumTest() {
int[][] tempMatrix = new int[3][4];
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix[0].length; j++) {
tempMatrix[i][j] = i * 10 +j;
} // Of for j
} // Of for i
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "\r\n");
} // Of matrixElementSumTest
/**
*****************
* Add two matrices.Attention: NO error check is provided at this moment.
*
* @param paraMatrix1 The first matrix.
* @param paraMatrix2 The second matrix. It should have the same size as the first one's.
* @return The addition of the these matrices.
*****************
*/
public static int[][] matrixAddition(int[][] paraMatrix1, int[][] paraMatrix2) {
int[][] resultMatrix = new int[paraMatrix1.length][paraMatrix1[0].length];
for (int i = 0; i < paraMatrix1.length; i++) {
for (int j = 0; j < paraMatrix1[0].length; j++) {
resultMatrix[i][j] = paraMatrix1[i][j] + paraMatrix2[i][j];
} // Of for j
} // Of for i
return resultMatrix;
} // Of matrixAddition
/**
****************
* Unit test for respective method.
****************
*/
public static void matrixAdditionTest() {
int[][] tempMatrix = new int[3][4];
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix[0].length; j++) {
tempMatrix[i][j] = i * 10 +j;
} // Of for j
} // Of for i
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix);
System.out.println("The new matrix is: \r\n" + Arrays.deepToString(tempNewMatrix));
} // Of matrixAdditionTest
} // Of class Day07
运行结果:
4. Summarize
通过今天的学习,了解到了java中数组的用法和arrays库的使用。
总的来说,数组的声明和C语言有一些出入之外,其余的都很类似,然后不得不说java比C语言方便多了。在java中直接array.length
就得出了数组的长度(在二维数组中这个求出来的是数组的列数),这个属实太方便了,以往写C语言的时候,还要专门声明一个变量,然后用函数求出要算的数组的长度,不胜其烦。