??? Java 1.5(或者說Java 5.0、Tiger)給我們帶來了很多新特性,這些新特性都有哪些呢?讓我們拭目以待。今天,我們先來看看Arrays類,一個全部由靜態方法構成的類,提供了許多有用的操作數組的方法。
我們先來看一個示例:
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));
??}
}
下面再看看輸出:
上面的程序使用了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,這樣會導致無限循環,最終導致棧溢出。
我們先來看一個示例:






















































































































































下面再看看輸出:
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,這樣會導致無限循環,最終導致棧溢出。