Java 中 System.arraycopy() 和 Arrays.copyOf() 的区别

Posted by icoding168 on 2020-03-13 16:28:02

分类: Java  

两个方法都可以用于数组的复制,但还是有一些区别的:

1. Arrays.copyOf() 需要依赖 System.arraycopy();

2. Arrays.copyOf() 会自动新建一个目标数组,System.arraycopy() 需要外部提供一个目标数组;

3. System.arraycopy() 可以指定源头数组任意区间来复制数据,也可以指定目标数组任意区间来粘贴数据,而 Arrays.copyOf() 只能从下标为 0 的元素开始复制。