??? Java 1.5(或者說Java 5.0、Tiger)給我們帶來了很多新特性,這些新特性都有哪些呢?讓我們拭目以待。今天,我們先來看看Arrays類,一個(gè)全部由靜態(tài)方法構(gòu)成的類,提供了許多有用的操作數(shù)組的方法。
我們先來看一個(gè)示例:
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(?);

????
//
?比較兩個(gè)數(shù)組
????
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!
"
);
????}
????
//
?用指定的值填充數(shù)組
????Arrays.fill(myOtherArray,?
2
,?
10
,?
new
?Double(Math.PI).intValue(?));

????myArray[
30
]?
=
?
98
;
????
????
//
?打印無序數(shù)組
????System.out.println(
"
Here's?the?unsorted?array
"
);
????System.out.print(Arrays.toString(myArray));
????System.out.println(?);

????
//
?數(shù)組排序
????Arrays.sort(myArray);

????
//
?打印有序數(shù)組
????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);

????
//
數(shù)組“深層次內(nèi)容”的字符串表示形式,簡(jiǎn)而言之,將多維數(shù)組所有元素轉(zhuǎn)換為字符串。
????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
"
}
}
;
????
????
//
比較兩個(gè)數(shù)組是否“深層次相等”,適用于任何維次的數(shù)組
????
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)...
上面的程序使用了Arrays類的大多數(shù)方法,下面我們就來看看Arrays到底為我們帶來了哪些有用的方法: 1. asList:將指定的數(shù)組轉(zhuǎn)換為L(zhǎng)ist; 2. binarySearch:采用二分搜索方法從數(shù)組中查找指定的值 3. deepEquals:比較兩個(gè)數(shù)組是否“深層次相等”,5.0引入 4. deepHashCode:計(jì)算數(shù)組的“深層次哈希碼”,5.0引入 5. deepToString:將數(shù)組轉(zhuǎn)換為字符串,5.0引入 6. equals:比較兩個(gè)數(shù)組是否相等 7. fill:用指定值填充數(shù)組 8. hashCode:計(jì)算數(shù)組的哈希值,5.0引入 9. sort:對(duì)數(shù)組排序 10. toString:將數(shù)組轉(zhuǎn)換為字符串,5.0引入。
上面的方法中除了4、5、6外,其余方法都比較簡(jiǎn)單。4、5、6三個(gè)方法主要用來操作多維數(shù)組,如果有些數(shù)組的元素類型本身也是數(shù)組,它們會(huì)對(duì)該元素也調(diào)用相同的方法,直至最終沒有數(shù)組元素的類型為數(shù)組。這三個(gè)方法功能十分強(qiáng)大,但有一點(diǎn)需要注意:千萬(wàn)不要將自身作為數(shù)組元素,例如上面示例中的a[1]=a,這樣會(huì)導(dǎo)致無限循環(huán),最終導(dǎo)致棧溢出。
|