Think in Java(十三):数组

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);
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值