java.util.Arrays:操作数组的工具类,里面定义了很多操作数组的方法
-
判断两个数组是否相等
package lll; import java.util.Arrays; public class Test { public static void main(String[] args) { int[] arr1=new int[]{1,2,3,4}; int[] arr2=new int[]{1,2,3,4}; boolean isEquals = Arrays.equals(arr1, arr2); System.out.println(isEquals); } }
-
输出数组信息
package lll; import java.util.Arrays; public class Test { public static void main(String[] args) { int[] arr1=new int[]{1,2,3,4}; int[] arr2=new int[]{1,2,3,4}; System.out.println(Arrays.toString(arr2)); } }
-
将指定值填充到数组当中
package lll; import java.util.Arrays; public class Test { public static void main(String[] args) { int[] arr2=new int[]{1,2,3,4}; System.out.println(Arrays.toString(arr2)); //将数组中的所有元素都替换成99 Arrays.fill(arr2, 99); System.out.println(Arrays.toString(arr2)); } }
-
对数组进行排序
package lll; import java.util.Arrays; public class Test { public static void main(String[] args) { int[] arr2=new int[]{3,2,1,4}; Arrays.sort(arr2); System.out.println(Arrays.toString(arr2)); } }
-
二分查找
package lll; import java.util.Arrays; public class Test { public static void main(String[] args) { int[] arr2=new int[]{1,2,3,4,5,6,7,8,9}; int index = Arrays.binarySearch(arr2, 61); //如果找到,返回索引值,如果找不到,返回一个负数 System.out.println(index); System.out.println(Arrays.toString(arr2)); } }