Arrays.copyOf()跟System.arrayCopy的用法和区别

arraycopy

arraycopy(Object src, int srcPos, Object dest, int destpos, int length);

  • src - the source array.(原始数组,要复制到目标数组的数组)
  • srcPos - starting position in the source array.(从原始数组的哪个位置开始复制)
  • dest - the destination array.(目标数组,用来存储赋值后的结果)
  • destPos - starting position in the destination data.(复制的时候存放在目标数组中的初始位置)
  • length - the number of array elements to be copied.(需要复制的元素的长度)
		int[] src = new int[] {1,2,3};
		int[] des = new int[] {1,2,3,0,0,0,0};
		System.arraycopy(src, 0, des, 3, 3);

最后得到的des变为:[1, 2, 3, 1, 2, 3, 0]

  • Arrays.copyOf()跟System.arrayCopy的区别:
    Arrays.copyOf()不仅仅只是拷贝数组中的元素,在拷贝元素时,会创建一个新的数组对象。而System.arrayCopy只拷贝已经存在数组元素。
发布了6 篇原创文章 · 获赞 0 · 访问量 78

猜你喜欢

转载自blog.csdn.net/o0o0o0o0o0oo/article/details/104189354
今日推荐