Java标准类库的System.arraycopy()方法,及在java.utils.Arrays类中一套用于数组的static方法,都是操纵数组实用功能。下面分别介绍。
(1) 数组的复制
(2) 数组的比较
(3) 数组的排序和查找
(1) 数组的复制
System.arraycopy(源数组, 从源数组的什么位置开始复制的偏移量, 目标数组, 从标数数组的什么位置开始复制的偏移量, 需要复制的元素的个数)
System.arraycopy已经对所有基本类型(包装类型同样适用)做了重载,如果复制对象,则只是浅复制(复制引用)。
下面以int作为示例:
1 import java.util.Arrays; 2 3 public class Test1 { 4 public static void main(String[] args) { 5 int[] i = new int[3]; 6 int[] j = new int[7]; 7 Arrays.fill(i, 47); 8 Arrays.fill(j, 99); 9 System.out.println("i = " + Arrays.toString(i)); // i = [47, 47, 47] 10 System.out.println("j = " + Arrays.toString(j)); // j = [99, 99, 99, 99, 99, 99, 99] 11 int[] k = new int[5]; 12 Arrays.fill(k, 103); 13 System.arraycopy(i, 0, k, 0, i.length); 14 System.out.println("k = " + Arrays.toString(k)); // k = [47, 47, 47, 103, 103] 15 System.arraycopy(k, 1, j, 0, 4); // 16 System.out.println("j = " + Arrays.toString(j)); // j = [47, 47, 103, 103, 99, 99, 99] 17 } 18 }
(2) 数组的比较
同样,Arrays.equals已经对所有基本类型(包装类型同样适用)做了重载,对于对象比较,使用了对象的equals方法,所以必须重写对象的equals方法。
1 import java.util.Arrays; 2 3 public class Test2 { 4 public static void main(String[] args) { 5 int[] a1 = new int[5]; 6 int[] a2 = new int[5]; 7 Arrays.fill(a1, 47); 8 Arrays.fill(a2, 47); 9 System.out.println(Arrays.equals(a1, a2)); // true 10 a2[3] = 11; 11 System.out.println(Arrays.equals(a1, a2)); // false 12 String[] s1 = new String[4]; 13 Arrays.fill(s1, "Hi"); 14 String[] s2 = { "Hi", "Hi", new String("Hi"), new String("Hi") }; 15 System.out.println(Arrays.equals(s1, s2)); // true 16 } 17 }
(3) 数组的排序和查找
同样,Arrays.sort已经对所有基本类型(包装类型同样适用)做了重载,对于对象比较,必须实现Comparable接口。
如果数组已经排好序,就可以使用Arrays.binarySearch执行快速查找(前提必须是排好序的数组)。
如果使用了Comparator<T>排序了某个对象数组,使用Arrays.binarySearch时必须提供同样的Comparator<T>。
1 import java.util.Arrays; 2 import java.util.Collections; 3 import java.util.Random; 4 5 class CompType implements Comparable<CompType> { 6 int i; 7 int j; 8 9 public CompType(int n1, int n2) { 10 i = n1; 11 j = n2; 12 } 13 14 @Override 15 public String toString() { 16 return "(" + i + ", " + j + ")"; 17 } 18 19 @Override 20 public int compareTo(CompType ct) { 21 return i == ct.i ? Integer.compare(j, ct.j) : Integer.compare(i, ct.i); 22 } 23 } 24 25 public class Test3 { 26 public static void main(String[] args) { 27 28 // 基本类型 29 Random random = new Random(47); 30 int[] a = random.ints(5, 5, 10).toArray(); 31 System.out.println(Arrays.toString(a)); // [8, 5, 8, 6, 6] 32 Arrays.sort(a); // Arrays.sort(基本类型) 33 System.out.println(Arrays.toString(a)); // [5, 6, 6, 8, 8] 34 35 // 包装类型 36 Integer[] b = { 3, 5, 9, 8, 2 }; 37 Arrays.sort(b); // Arrays.sort(Object) 38 System.out.println(Arrays.toString(b)); // [2, 3, 5, 8, 9] 39 Arrays.sort(b, Collections.reverseOrder()); // Arrays.sort(Object, Comparator<T>) 40 System.out.println(Arrays.toString(b)); // [9, 8, 5, 3, 2] 41 42 // String 43 String[] c = { "A", "B", "AB", "AC", "a", "b", "ab", "ac" }; 44 Arrays.sort(c); // 字符串默认是字典排序[A-Za-z] 45 System.out.println(Arrays.toString(c)); // [A, AB, AC, B, a, ab, ac, b] 46 Arrays.sort(c, String.CASE_INSENSITIVE_ORDER); // 忽略大小写排序 47 System.out.println(Arrays.toString(c)); // [A, a, AB, ab, AC, ac, B, b] 48 49 // 对象类型 50 CompType[] d = { new CompType(2, 2), new CompType(1, 2), new CompType(2, 4), new CompType(0, 3), 51 new CompType(3, 4), new CompType(3, 0), new CompType(2, 2), new CompType(2, 1) }; 52 Arrays.sort(d); 53 System.out.println(Arrays.toString(d)); // [(0, 3), (1, 2), (2, 1), (2, 2), (2, 2), (2, 4), (3, 0), (3, 4)] 54 Arrays.sort(d, Collections.reverseOrder()); 55 System.out.println(Arrays.toString(d)); // [(3, 4), (3, 0), (2, 4), (2, 2), (2, 2), (2, 1), (1, 2), (0, 3)] 56 57 // 快速查找 58 Arrays.sort(b); 59 int location = Arrays.binarySearch(b, 8); 60 System.out.println("Location of [5] is " + location + ", b[" + location + "] = " + b[location]); // Location of [5] is 3, b[3] = 8 61 62 Arrays.sort(c); 63 location = Arrays.binarySearch(c, "AC"); 64 System.out.println("Location of [AC] is " + location + ", c[" + location + "] = " + c[location]); // Location of [AC] is 2, c[2] = AC 65 66 Arrays.sort(c, String.CASE_INSENSITIVE_ORDER); 67 location = Arrays.binarySearch(c, "AC", String.CASE_INSENSITIVE_ORDER); 68 System.out.println("Location of [AC] is " + location + ", c[" + location + "] = " + c[location]); // Location of [AC] is 5, c[5] = ac 69 70 Arrays.sort(d); 71 location = Arrays.binarySearch(d, new CompType(2, 4)); 72 System.out.println("Location of (2, 4) is " + location + ", d[" + location + "] = " + d[location]); // Location of (2, 4) is 5, d[5] = (2, 4) 73 } 74 }