Arrays类
Data tool class java.util.Arrays
Since the array object itself does not have any method for us to call, but the API provides a tool class Arrays for us to use, so that we can perform some basic operations on the data object.
Check the JDK basic documentation
. The methods in the Arrays class are static methods modified by static. When in use, you can directly use the class name to call , but "not use" to use the object to call (note: "not used" instead of "cannot")
It has the following common functions:
Assign a value to the array: through the fill method.
Sort the array: through the sort method, in ascending order.
Compare arrays: compare whether the element values in the array are equal through the equals method.
Find array elements: Binary Search can be performed on the sorted array through the binarySearch method.
public static void main(String[] args) {
//找Arrays类
//Arrays
int[]a={
1,35,6,4565,45,7565,4};
System.out.println(a); //[I@1b6d3586
//1.打印数组元素,用Arrays下面的toString方法
System.out.println(Arrays.toString(a));//输出结果 [1, 35, 6, 4565, 45, 7565, 4]
printArray(a); //输出结果为:[1, 35, 6, 4565, 45, 7565, 4]
System.out.println("");
//2.数组进行排序,用Arrays下面的sort方法,升序
Arrays.sort(a);
System.out.println(Arrays.toString(a));//输出结果为:[1, 4, 6, 35, 45, 4565, 7565]
//3.数组填充,用Arrays下面的fill方法,fill(数组,填充的值)
Arrays.fill(a,0);
System.out.println(Arrays.toString(a));//输出结果为 [0, 0, 0, 0, 0, 0, 0]
//4.填充对应序列得知
Arrays.fill(a,2,4,11);
System.out.println(Arrays.toString(a));//输出结果为:[0, 0, 11, 11, 0, 0, 0],第二到第四个元素间被11填充
}
//自己编写的方法和toString效果一样
public static void printArray(int[] a){
System.out.print("[");
int t=0;
for (int i = 0; i < a.length-1 ; i++) {
System.out.print(a[i]+", ");
t=i+1;
}
System.out.print(a[t]);
System.out.print("]");
}