1.数组的拷贝:System.arraycopy()
public class CopyingArrays { public static void main(String[] args) { int[] i = new int[7]; int[] j = new int[10]; Arrays.fill(i, 47); Arrays.fill(j, 99); print("i = " + Arrays.toString(i)); print("j = " + Arrays.toString(j)); System.arraycopy(i, 0, j, 0, i.length); } } /* * Output: i = [47, 47, 47, 47, 47, 47, 47] * j = [99, 99, 99, 99, 99, 99, 99, 99, 99, 99] * j = [47, 47, 47, 47, 47, 47, 47, 99, 99, 99] k = [47, 47, 47, 47, 47] */// :~
2.数组的比较:Arrays.equals()
数组相等的条件是元素个数必须相等,并且对应位置的元素也相等:
public class ComparingArrays { public static void main(String[] args) { int[] a1 = new int[10]; int[] a2 = new int[10]; Arrays.fill(a1, 47); Arrays.fill(a2, 47); print(Arrays.equals(a1, a2)); a2[3] = 11; print(Arrays.equals(a1, a2)); String[] s1 = new String[4]; Arrays.fill(s1, "Hi"); String[] s2 = { new String("Hi"), new String("Hi"), new String("Hi"), new String("Hi") }; print(Arrays.equals(s1, s2)); } } /* * Output: true false true */// :~
3. 往数组里面填充内容:Arrays.fill()
public class Test { public static void main(String[] args) { String[] str = new String[10]; Arrays.fill(str, "?"); for(String s : str){ System.out.print(s); } } }