Arrays类
Java 1.5(或者说Java 5.0、Tiger)给我们带来了很多新特性,这些新特性都有哪些呢?让我们拭目以待。今天,我们先来看看Arrays类,一个全部由静态方法构成的类,提供了许多有用的操作数组的方法。
我们先来看一个示例:
下面再看看输出:
上面的程序使用了Arrays类的大多数方法,下面我们就来看看Arrays到底为我们带来了哪些有用的方法:
1. asList:将指定的数组转换为List;
2. binarySearch:采用二分搜索方法从数组中查找指定的值
3. deepEquals:比较两个数组是否“深层次相等”,5.0引入
4. deepHashCode:计算数组的“深层次哈希码”,5.0引入
5. deepToString:将数组转换为字符串,5.0引入
6. equals:比较两个数组是否相等
7. fill:用指定值填充数组
8. hashCode:计算数组的哈希值,5.0引入
9. sort:对数组排序
10. toString:将数组转换为字符串,5.0引入。
上面的方法中除了4、5、6外,其余方法都比较简单。4、5、6三个方法主要用来操作多维数组,如果有些数组的元素类型本身也是数组,它们会对该元素也调用相同的方法,直至最终没有数组元素的类型为数组。这三个方法功能十分强大,但有一点需要注意:千万不要将自身作为数组元素,例如上面示例中的a[1]=a,这样会导致无限循环,最终导致栈溢出。
我们先来看一个示例:
package
com.jiang.tiger.chap1;
import java.util.Arrays;
public class ArraysTester {
private int [] ar;
public ArraysTester( int numValues) {
ar = new int [numValues];
for ( int i = 0 ; i < ar.length; i ++ ) {
ar[i] = ( 1000 - ( 300 + i));
}
}
public int [] get( ) {
return ar;
}
public static void main(String[] args) {
ArraysTester tester = new ArraysTester( 50 );
int [] myArray = tester.get( );
// 比较两个数组
int [] myOtherArray = tester.get().clone( );
if (Arrays.equals(myArray, myOtherArray)) {
System.out.println( " The two arrays are equal! " );
} else {
System.out.println( " The two arrays are not equal! " );
}
// 用指定的值填充数组
Arrays.fill(myOtherArray, 2 , 10 , new Double(Math.PI).intValue( ));
myArray[ 30 ] = 98 ;
// 打印无序数组
System.out.println( " Here's the unsorted array " );
System.out.print(Arrays.toString(myArray));
System.out.println( );
// 数组排序
Arrays.sort(myArray);
// 打印有序数组
System.out.println( " Here's the sorted array " );
System.out.print(Arrays.toString(myArray));
System.out.println( );
// 用二分搜索法查找指定的值。
int index = Arrays.binarySearch(myArray, 98 );
System.out.println( " 98 is located in the array at index " + index);
// 数组“深层次内容”的字符串表示形式,简而言之,将多维数组所有元素转换为字符串。
String[][] ticTacToe = { { " X " , " O " , " O " } ,
{ " O " , " X " , " X " } ,
{ " X " , " O " , " X " } } ;
System.out.println(Arrays.deepToString(ticTacToe));
String[][] ticTacToe2 = { { " O " , " O " , " X " } ,
{ " O " , " X " , " X " } ,
{ " X " , " O " , " X " } } ;
String[][] ticTacToe3 = { { " X " , " O " , " O " } ,
{ " O " , " X " , " X " } ,
{ " X " , " O " , " X " } } ;
// 比较两个数组是否“深层次相等”,适用于任何维次的数组
if (Arrays.deepEquals(ticTacToe, ticTacToe2)) {
System.out.println( " Boards 1 and 2 are equal. " );
} else {
System.out.println( " Boards 1 and 2 are not equal. " );
}
if (Arrays.deepEquals(ticTacToe, ticTacToe3)) {
System.out.println( " Boards 1 and 3 are equal. " );
} else {
System.out.println( " Boards 1 and 3 are not equal. " );
}
System.out.println( " Here's the array of the ArrayTester " );
System.out.print(Arrays.toString(tester.get()));
System.out.println( );
System.out.println( " Here's myArray " );
System.out.print(Arrays.toString(myArray));
System.out.println( );
System.out.println( " Here's myOtherArray " );
System.out.print(Arrays.toString(myOtherArray));
System.out.println( );
Object[] a = new Object[ 2 ];
a[ 0 ] = 1 ;
a[ 1 ] = a;
System.out.println(Arrays.deepHashCode(a));
}
}
import java.util.Arrays;
public class ArraysTester {
private int [] ar;
public ArraysTester( int numValues) {
ar = new int [numValues];
for ( int i = 0 ; i < ar.length; i ++ ) {
ar[i] = ( 1000 - ( 300 + i));
}
}
public int [] get( ) {
return ar;
}
public static void main(String[] args) {
ArraysTester tester = new ArraysTester( 50 );
int [] myArray = tester.get( );
// 比较两个数组
int [] myOtherArray = tester.get().clone( );
if (Arrays.equals(myArray, myOtherArray)) {
System.out.println( " The two arrays are equal! " );
} else {
System.out.println( " The two arrays are not equal! " );
}
// 用指定的值填充数组
Arrays.fill(myOtherArray, 2 , 10 , new Double(Math.PI).intValue( ));
myArray[ 30 ] = 98 ;
// 打印无序数组
System.out.println( " Here's the unsorted array " );
System.out.print(Arrays.toString(myArray));
System.out.println( );
// 数组排序
Arrays.sort(myArray);
// 打印有序数组
System.out.println( " Here's the sorted array " );
System.out.print(Arrays.toString(myArray));
System.out.println( );
// 用二分搜索法查找指定的值。
int index = Arrays.binarySearch(myArray, 98 );
System.out.println( " 98 is located in the array at index " + index);
// 数组“深层次内容”的字符串表示形式,简而言之,将多维数组所有元素转换为字符串。
String[][] ticTacToe = { { " X " , " O " , " O " } ,
{ " O " , " X " , " X " } ,
{ " X " , " O " , " X " } } ;
System.out.println(Arrays.deepToString(ticTacToe));
String[][] ticTacToe2 = { { " O " , " O " , " X " } ,
{ " O " , " X " , " X " } ,
{ " X " , " O " , " X " } } ;
String[][] ticTacToe3 = { { " X " , " O " , " O " } ,
{ " O " , " X " , " X " } ,
{ " X " , " O " , " X " } } ;
// 比较两个数组是否“深层次相等”,适用于任何维次的数组
if (Arrays.deepEquals(ticTacToe, ticTacToe2)) {
System.out.println( " Boards 1 and 2 are equal. " );
} else {
System.out.println( " Boards 1 and 2 are not equal. " );
}
if (Arrays.deepEquals(ticTacToe, ticTacToe3)) {
System.out.println( " Boards 1 and 3 are equal. " );
} else {
System.out.println( " Boards 1 and 3 are not equal. " );
}
System.out.println( " Here's the array of the ArrayTester " );
System.out.print(Arrays.toString(tester.get()));
System.out.println( );
System.out.println( " Here's myArray " );
System.out.print(Arrays.toString(myArray));
System.out.println( );
System.out.println( " Here's myOtherArray " );
System.out.print(Arrays.toString(myOtherArray));
System.out.println( );
Object[] a = new Object[ 2 ];
a[ 0 ] = 1 ;
a[ 1 ] = a;
System.out.println(Arrays.deepHashCode(a));
}
}
下面再看看输出:
The two arrays are equal!
Here's the unsorted array
[ 700, 699, 698, 697, 696, 695, 694, 693, 692, 691, 690, 689, 688, 687, 686, 685, 684, 683, 682, 681, 680, 679, 678, 677, 676, 675, 674, 673, 672, 671, 98, 669, 668, 667, 666, 665, 664, 663, 662, 661, 660, 659, 658, 657, 656, 655, 654, 653, 652, 651 ]
Here's the sorted array
[ 98, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700 ]
98 is located in the array at index 0
[ [X, O, O ] , [ O, X, X ] , [ X, O, X ] ]
Boards 1 and 2 are not equal.
Boards 1 and 3 are equal.
Here's the array of the ArrayTester
[ 98, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700 ]
Here's myArray
[ 98, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700 ]
Here's myOtherArray
[ 700, 699, 3, 3, 3, 3, 3, 3, 3, 3, 690, 689, 688, 687, 686, 685, 684, 683, 682, 681, 680, 679, 678, 677, 676, 675, 674, 673, 672, 671, 670, 669, 668, 667, 666, 665, 664, 663, 662, 661, 660, 659, 658, 657, 656, 655, 654, 653, 652, 651 ]
Exception in thread " main " java.lang.StackOverflowError
at java.util.Arrays.deepHashCode(Unknown Source)...
Here's the unsorted array
[ 700, 699, 698, 697, 696, 695, 694, 693, 692, 691, 690, 689, 688, 687, 686, 685, 684, 683, 682, 681, 680, 679, 678, 677, 676, 675, 674, 673, 672, 671, 98, 669, 668, 667, 666, 665, 664, 663, 662, 661, 660, 659, 658, 657, 656, 655, 654, 653, 652, 651 ]
Here's the sorted array
[ 98, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700 ]
98 is located in the array at index 0
[ [X, O, O ] , [ O, X, X ] , [ X, O, X ] ]
Boards 1 and 2 are not equal.
Boards 1 and 3 are equal.
Here's the array of the ArrayTester
[ 98, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700 ]
Here's myArray
[ 98, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700 ]
Here's myOtherArray
[ 700, 699, 3, 3, 3, 3, 3, 3, 3, 3, 690, 689, 688, 687, 686, 685, 684, 683, 682, 681, 680, 679, 678, 677, 676, 675, 674, 673, 672, 671, 670, 669, 668, 667, 666, 665, 664, 663, 662, 661, 660, 659, 658, 657, 656, 655, 654, 653, 652, 651 ]
Exception in thread " main " java.lang.StackOverflowError
at java.util.Arrays.deepHashCode(Unknown Source)...
上面的程序使用了Arrays类的大多数方法,下面我们就来看看Arrays到底为我们带来了哪些有用的方法:
1. asList:将指定的数组转换为List;
2. binarySearch:采用二分搜索方法从数组中查找指定的值
3. deepEquals:比较两个数组是否“深层次相等”,5.0引入
4. deepHashCode:计算数组的“深层次哈希码”,5.0引入
5. deepToString:将数组转换为字符串,5.0引入
6. equals:比较两个数组是否相等
7. fill:用指定值填充数组
8. hashCode:计算数组的哈希值,5.0引入
9. sort:对数组排序
10. toString:将数组转换为字符串,5.0引入。
上面的方法中除了4、5、6外,其余方法都比较简单。4、5、6三个方法主要用来操作多维数组,如果有些数组的元素类型本身也是数组,它们会对该元素也调用相同的方法,直至最终没有数组元素的类型为数组。这三个方法功能十分强大,但有一点需要注意:千万不要将自身作为数组元素,例如上面示例中的a[1]=a,这样会导致无限循环,最终导致栈溢出。