1、Arrays.copyOf()接收两个参数,其一是拷贝的数组,其二为数组长度
2、Arrays.copyOfRange()接收三个参数,分别是拷贝的数组,开始位置,结束位置(不包含该位置)
public class CopyTest {
void printByte(byte[] btCopy) {
for(byte b:btCopy) {System.out.print(b);}
}
void printByte1(byte[] btCopy1) {
for(byte b:btCopy1) {System.out.print(b);}
}
public static void main(String args[]) {
byte[] bt = new byte[] {1,2,3,4,5};
byte[] btCopy = Arrays.copyOf(bt, bt.length);
byte[] btCopy1 = Arrays.copyOfRange(bt, 0, bt.length);
new CopyTest().printByte(btCopy);
System.out.println(" ");
new CopyTest().printByte(btCopy1);
}
}
本文介绍如何使用Java中的Arrays类来复制数组。通过示例代码展示了使用Arrays.copyOf()和Arrays.copyOfRange()方法的具体用法,前者可以复制整个数组,后者则可以指定范围进行复制。
1670

被折叠的 条评论
为什么被折叠?



