Arrys常用方法
1.toString()
Arrays.toString(arr) //打印数组的内容,观察数组元素的值
2.sort()
Arrays.sort(arr); //升序
3.copyOf()
int[] a1 = Arrays.copyOf(arr, 10); #长度大于现有数组长度叫扩容
int[] a2 = Arrays.copyOf(arr, 3); #长度小于现有数组长度叫缩容
常见数组方法demo
public static void main(String[] args) {
int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
arr[i]=new Random().nextInt(100);
System.out.println("arr[i]"+arr[i]);
}
System.out.println("打印数组"+Arrays.toString(arr));
System.out.println("数组长度:"+arr.length);
for (int j = 0; j < arr.length; j++) {
System.out.println(arr[j]);
}
Arrays.sort(arr);
System.out.println("排序"+Arrays.toString(arr));
//数组的复制,大于原来长度相当于扩容
int[] a1 = Arrays.copyOf(arr, 10);
System.out.println("扩容:"+Arrays.toString(a1));
int[] a2 = Arrays.copyOf(arr, 3);
System.out.println("缩容:"+Arrays.toString(a2));
}