Java语言基础知识:Arrays常用的工具类

1.boolean equals(int[] a,int[] b):判断两个数组是否相等

int[] arr1 = new int[]{1,2,3,4};
int[] arr12= new int[]{1,3,2,4};
boolean isEquals = Arryays.equals(arr1,arr2);
System.out.println(isEquals);//false

2.String toString(int[] a):输出数组信息

System.out.println(Arrays.toString(arr1));//{1,2,3,4}

3.void fill (int[] a,int val):将指定的值填充到数组中

Arrays.fill(arr1,65);
System.out.println(Arrays.toString(arr1));//{65,65,65,65}

4.void sort(int[] a);对数组排序

Arrays.sort(arr2);
System.out.println(Arrays.toString(arr2));//{1,2,3,4}

5.int binarySearch(int[] a,int key);二分查找索引

int[] arr3 = new int[]{10,6,43,4,99,76};
int index = Arrays.binarySearch(arr3,6);
if(index >= 0){
	System.out.println("找到索引为"+index);
}else{
	System.out.println("未找到");//{1,2,3,4}
}

你可能感兴趣的:(java基础,java,Arrays工具栏)