数组的排序:
public static void main(String[] args) { int[] arr = {11,22,44,55,33}; System.out.println(Arrays.toString(arr));//[11, 22, 44, 55, 33] Arrays.sort(arr); System.out.println(Arrays.toString(arr));//[11, 22, 33, 44, 55] }
数组转集合,不能增删,否则抛异常,数组的其它方法都可以。如果例子中list增加方法,例如:list.add("d") 则抛 java.lang.UnsupportedOperationException
private static void arrToList() { String[] arr = {"a","b","c"}; List<String> list = Arrays.asList(arr); System.out.println(list.size()); int[] ints = {1,2,3,4}; List<int[]> list2 = Arrays.asList(ints); System.out.println(list2.size());//长度为1 //用包装类 Integer[] integers = {1,2,3,4,5,6}; List<Integer> list3 = Arrays.asList(integers); System.out.println(list3.size());//长度为6 }
合并数组:
public static String[] concat(String[] a, String[] b) { String[] c= new String[a.length+b.length]; System.arraycopy(a, 0, c, 0, a.length); System.arraycopy(b, 0, c, a.length, b.length); return c; }
数组复制
public static void main(String[] args) { String[] strs1 = { "123", "234", "345", "456", "789" }; String[] str2 = Arrays.copyOf(strs1, 3); String[] str3 = Arrays.copyOf(strs1, 8); System.out.println(); for (String ele : str2) { System.out.print(ele + ";"); // 输出:123;234;345; } System.out.println(); for (String ele : str3) { System.out.print(ele + ";");// 输出:123;234;345;456;789;null;null;null; } int[] in = { 1, 2, 3, 4, 5 }; int[] int1 = Arrays.copyOf(in, 3); int[] int2 = Arrays.copyOf(in, 8); System.out.println(); for (int ele : int1) { System.out.print(ele + ";");// 输出:1;2;3; } System.out.println(); for (int ele : int2) { System.out.print(ele + ";");// 输出:1;2;3;4;5;0;0;0; } }
数组相等 returns true if the arrays have equal lengths and equal elements in corresponding positions. The arrays can have component types Object, int, long, short, char, byte,boolean, float, or double.:
public static void main(String[] args) { String[] str1 = {"1","2","3","4"}; String[] str2 = {"1","1","3","4"}; System.out.println(Arrays.equals(str1, str2)); }
END
END