数组元素的复制
数组元素复制的三种方法及例子
1.一个元素一个元素的复制。
使用“ = ”号一个一个的赋值。
将数组(a1)的元素一个一个的复制给另一个数组(a2)。
(1)代码
//方法一
//赋值复制【用=】
System.out.println("第一种方法:");
int[] a1 = {20, 30, 20, 40};
int[] a2 = new int[a1.length];
System.out.println("复制数组的全部元素:");
for (var i = 0; i < a1.length; i++) {
a2[i] = a1[i];
System.out.println("a2数组:"+a2[i]);
}
System.out.println("————————");
(2)结果
2.使用System.arrycop()方法。
可以将原数组中的全部元素复制,也可以复制单个
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
src是原数组名
srcPos是原数组的起始下标(包含此下标)
dest是目标数组名
destPos是目标数组起始下标(包含此下标)
length复制的元素个数(例如:2就是两个元素)
说明:
使用System.arrycop()方法可以全部复制,也可以部分复制。
(1)代码
//方法二
//系统自带的arraycopy()方法【System.arrycop()方法】
System.out.println("第二种方法:");
Day29DuixiangInit x = new Day29DuixiangInit();
int[] b1 = {1, 2, 3, 5};
int[] b2 = new int[a1.length];
int[] b3 = new int[a1.length];
//复制全部数组
System.arraycopy(b1, 0, b2, 0, 4);
//复制部分数组,具有连续性
System.arraycopy(b1, 2, b3, 0, 2);
//输出数组b2
System.out.println("复制数组的全部元素:");
for (int i = 0; i < 4; i++) {
System.out.println("b2数组:" + b2[i]);
}
System.out.println();
//输出数组b3
System.out.println("复制数组的部分元素,且具有连续性:");
for (int i1 = 0; i1 < 4; i1++) {
System.out.println("b3数组:" + b3[i1]);
}
System.out.println("————————");
(2)结果
3.使用Arrays类的copyOf()或copyOfRange()方法。
public static int[] copyOf(int[] original,int newLength)
public static int[] copyOfRange(int[] original,int startPos,int endPos)
original:数组名
newLength:数组复制的长度(3就是三个元素)
startPos:数组起始下标(包含此下标)
endPos:数组结束下标(不包含此下标)
source为原数组
target为复制过原数组的数组
若target=source只是将原数组的引用,没有实现数组的复制。
说明:
Arrays类的copyOf()或copyOfRange()方法可以复制全部元素,也可以复制部分元素。
(1)代码
//方法三
System.out.println("第三种方法:");
int[] c1 = {1, 4, 3, 55};
int[] c2 = new int[c1.length];
int[] c3 = new int[c1.length];
//输出c2数组
System.out.println("复制数组的全部元素:");
c2 = Arrays.copyOf(c1, 4);
for (int j1 = 0; j1 <4 ; j1++) {
System.out.println("c2数组:" + c2[j1]);
}
System.out.println();
//输出c3数组
System.out.println("复制数组的部分元素,且具有连续性:");
c3 = Arrays.copyOfRange(c1,0,3);
for (int j2 = 1; j2< 3; j2++) {
System.out.println("c3数组:" + c3[j2]);
}
(2)结果
4.全部代码
import java.util.Arrays;
public class Day34shuzuyy {
public static void main(String[] args) {
//方法一
//赋值复制【用=】
System.out.println("第一种方法:");
int[] a1 = {20, 30, 20, 40};
int[] a2 = new int[a1.length];
System.out.println("复制数组的全部元素:");
for (var i = 0; i < a1.length; i++) {
a2[i] = a1[i];
System.out.println("a2数组:"+a2[i]);
}
System.out.println("————————");
//方法二
//系统自带的arraycopy()方法【System.arrycop()方法】
System.out.println("第二种方法:");
Day29DuixiangInit x = new Day29DuixiangInit();
int[] b1 = {1, 2, 3, 5};
int[] b2 = new int[a1.length];
int[] b3 = new int[a1.length];
//复制全部数组
System.arraycopy(b1, 0, b2, 0, 4);
//复制部分数组,具有连续性
System.arraycopy(b1, 2, b3, 0, 2);
//输出数组b2
System.out.println("复制数组的全部元素:");
for (int i = 0; i < 4; i++) {
System.out.println("b2数组:" + b2[i]);
}
System.out.println();
//输出数组b3
System.out.println("复制数组的部分元素,且具有连续性:");
for (int i1 = 0; i1 < 4; i1++) {
System.out.println("b3数组:" + b3[i1]);
}
System.out.println("————————");
//方法三
System.out.println("第三种方法:");
int[] c1 = {1, 4, 3, 55};
int[] c2 = new int[c1.length];
int[] c3 = new int[c1.length];
//输出c2数组
System.out.println("复制数组的全部元素:");
c2 = Arrays.copyOf(c1, 4);
for (int j1 = 0; j1 <4 ; j1++) {
System.out.println("c2数组:" + c2[j1]);
}
System.out.println();
//输出c3数组
System.out.println("复制数组的部分元素,且具有连续性:");
c3 = Arrays.copyOfRange(c1,0,3);
for (int j2 = 1; j2< 3; j2++) {
System.out.println("c3数组:" + c3[j2]);
}
}
结果:
5.下标越界提示
有类似的提示,说明下标越界。
仅个人观点