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;
}
/** */
/**
*
@param
args
*/
public
static
void
main(String[] args)
{
//
TODO Auto-generated method stub
ArraysTester test
=
new
ArraysTester(
50
);
int
[] myArray
=
test.get();
int
[] myOtherArray
=
test.get().clone();
if
(Arrays.equals(myArray, myOtherArray))
{
System.out.println(
"
相等
"
);
}
else
{
System.out.println(
"
不相等
"
);
}
Arrays.fill(myOtherArray,
2
,
10
,
new
Double(Math.PI).intValue());
myArray[
30
]
=
98
;
System.out.println(
"
这是一个未排序的数组
"
);
System.out.println(Arrays.toString(myArray));
Arrays.sort(myArray);
System.out.println(
"
这是一个排序的数组
"
);
System.out.println(Arrays.toString(myArray));
int
index
=
Arrays.binarySearch(myArray,
98
);
System.out.println(
"
98被定位在第
"
+
index
+
"
个位置上
"
);
String[][] ticTacToe
=
{
{
"
X
"
,
"
0
"
,
"
0
"
}
,
{
"
0
"
,
"
X
"
,
"
X
"
}
,
{
"
X
"
,
"
0
"
,
"
X
"
}
}
;
System.out.println(Arrays.deepToString(ticTacToe));
String[][] ticTacToe2
=
{
{
"
0
"
,
"
0
"
,
"
X
"
}
,
{
"
0
"
,
"
X
"
,
"
X
"
}
,
{
"
X
"
,
"
0
"
,
"
X
"
}
}
;
String[][] ticTacToe3
=
{
{
"
X
"
,
"
0
"
,
"
0
"
}
,
{
"
0
"
,
"
X
"
,
"
X
"
}
,
{
"
X
"
,
"
0
"
,
"
X
"
}
}
;
if
(Arrays.deepEquals(ticTacToe, ticTacToe2))
{
System.out.println(
"
相等
"
);
}
else
{
System.out.println(
"
不相等
"
);
}
if
(Arrays.deepEquals(ticTacToe, ticTacToe3))
{
System.out.println(
"
相等
"
);
}
else
{
System.out.println(
"
不相等
"
);
}
}
}
Arrays.equals(myArray, myOtherArray)---直接比较两个数组是否相等
Arrays.fill(a, val)---将数组a中所有元素都用val来填充
Arrays.fill(a, 2,10,val);---将数组a,从索引为2的元素到第10的元素用val来填充
Arrays.sort(a);---按照自然规律排序,升序
Arrays.binarySearch(myArray, 98);---搜索myArray中值为98的索引号,并返回索引
Arrays.toString(a)--打印数组a格式:[2asdf, 3asd, afas]
Arrays.deepToString(ticTacToe)--打印多维数组ticTacToe
Arrays.deepEquals(ticTacToe, ticTacToe2)---比较两个多维数组
List<String> d = Arrays.asList(a);---将数组转换为List