1年多了,jdk还不是很熟,补补。
package lhy.core.util; import java.util.Arrays; import java.util.List; public class ArraysTest { public static void main(String[] args) { int[] aa = {2,1,3,15,12,6}; //1,数组转换成字符串:toString(int[] a) System.out.println(Arrays.toString(aa));//[1, 2, 3, 4, 5, 6] //2,复制指定的数组,截取或用 0 填充 copyOf(float[] original,int newLength) double[] dd = {4.1,6.2,3.4,9.6,8.8}; double[] ddCopy = Arrays.copyOf(dd, 7); for (double d : ddCopy) { System.out.print(d + " "); //4.1 6.2 3.4 9.6 8.8 0.0 0.0 } System.out.println(" 复制数组指定范围到新数组:"); //将指定数组的指定范围复制到一个新数组。 int[] aaCopy = Arrays.copyOfRange(aa, 0, 3); for (int i : aaCopy) { System.out.print(i + " ");//2 1 3 } System.out.println(" 数组转list:"); //3,数组转list String[] str = {"tom","jack","mashibing"}; List<String> list = Arrays.asList(str); for (String string : list) { System.out.println(string); } System.out.println("快速排序sort:"); //4,数组排序sort:快速排序 Arrays.sort(aa); for (int a : aa) { System.out.print(a + " "); //1 2 3 6 12 15 } System.out.println(" 只排序数组指定下标部分:"); //对指定下标部分 排序 Arrays.sort(dd, 2, 4); for (double d : dd) { System.out.print(d +" "); } System.out.println(" binarySearch二分查找:"); //5:二分查找,必须先排序 Arrays.sort(aa); int key = Arrays.binarySearch(aa, 4); if(key >= 0){//找到了key >= 0 System.out.println("查找结果,key:"+key+" 值为:"+aa[key]); }else{ System.out.println("数组中未查找到4,key:"+key); //数组中未查找到4,key:-4 } //二分查找数组指定范围: key = Arrays.binarySearch(aa, 0, 3, 3); if(key >= 0){//找到了key >= 0 System.out.println("查找结果,key:"+key +" 值:"+aa[key]); //查找结果,key:2 值:3 } System.out.println(" 替换数组的值:"); //6:替换数组的值 int[] arr = new int[5]; Arrays.fill(arr, 2); for (int i : arr) { System.out.print(i+" "); //2 2 2 2 2 } System.out.println(" 部分替换数组的值:"); Arrays.fill(arr, 1, 3, 5); for (int i : arr) { System.out.print(i + " ");//2 5 5 2 2 } System.out.println(" 挨着比较2个数组值是否相等:"); //7:比较2个数组是否相等 int[] arr1 = {1,2,3,4}; int[] arr2 = {2,1,3,4}; System.out.println(Arrays.equals(arr1, arr2)); //false } }