Java数组拷贝
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/219
Java数组的四种拷贝方式
1.for循环实现数组copy
2.clone实现数组copy
3.System.arraycopy()实现数组copy
4.Arrays.copyOf实现数组copy
下面通过下面实现一维数组和二维数组的copy
package com.joshua317;
import java.util.*;
public class Test {
public static void main(String[] args) {
String[] namesArr = {"joshua317","joshua","jack"};
String[] namesArrCopy1 = new String[namesArr.length];
//1.1 一维数组--for循环实现数组copy
System.out.println("==========================一维数组--for循环实现数组copy============================");
for (int i=0; i<namesArrCopy1.length; i++) {
namesArrCopy1[i] = namesArr[i];
}
printArr(namesArrCopy1);
//1.2 一维数组--clone实现数组copy
System.out.println("==========================一维数组--clone实现数组copy============================");
String[] namesArrCopy2 = namesArr.clone();
printArr(namesArrCopy2);
//1.3 一维数组--System.arraycopy()
System.out.println("==========================一维数组--System.arraycopy()============================");
String[] namesArrCopy3 = new String[namesArr.length];
System.arraycopy(namesArr, 0,namesArrCopy3,0, namesArr.length);
printArr(namesArrCopy3);
//1.4 一维数组--Arrays.copyOf实现数组copy
System.out.println("==========================一维数组--Arrays.copyOf实现数组copy============================");
String[] namesArrCopy4 = Arrays.copyOf(namesArr,namesArr.length);
printArr(namesArrCopy4);
//2.1 二维数组--for循环实现数组copy
System.out.println("==========================二维数组--for循环实现数组copy============================");
String[][] names2Arr = {{"joshua317","joshua","jack"},{"joshua318","joshua2","jack2"}};
String[][] names2ArrCopy1 = new String[names2Arr.length][names2Arr[0].length];
for (int i = 0; i < names2ArrCopy1.length; i++) {
for (int j = 0; j < names2ArrCopy1[i].length; j++) {
names2ArrCopy1[i][j] = names2Arr[i][j];
}
}
printArr2(names2ArrCopy1);
//2.2 二维数组--clone实现数组copy
System.out.println("==========================二维数组--clone实现数组copy============================");
String[][] names2ArrCopy2 = new String[names2Arr.length][names2Arr[0].length];
for (int i = 0; i < names2ArrCopy2.length; i++) {
names2ArrCopy2[i] = names2Arr[i].clone();
}
printArr2(names2ArrCopy2);
//2.3 二维数组--System.arraycopy()
System.out.println("==========================二维数组--System.arraycopy()============================");
String[][] names2ArrCopy3 = new String[names2Arr.length][names2Arr[0].length];
for (int i = 0; i < names2ArrCopy3.length; i++) {
System.arraycopy(names2Arr[i], 0,names2ArrCopy3[i],0, names2Arr[i].length);
}
printArr2(names2ArrCopy3);
//2.4 二维数组--Arrays.copyOf实现数组copy
System.out.println("==========================二维数组--Arrays.copyOf实现数组copy============================");
String[][] names2ArrCopy4 = new String[names2Arr.length][names2Arr[0].length];
for (int i = 0; i < names2ArrCopy4.length; i++) {
names2ArrCopy4[i] = Arrays.copyOf(names2Arr[i],names2Arr[i].length);
}
printArr2(names2ArrCopy4);
}
public static <T> void printArr(T[] t)
{
System.out.println(Arrays.toString(t));
}
public static <T> void printArr2(T[][] t)
{
for (int i=0; i<t.length; i++) {
System.out.println(Arrays.toString(t[i]));
}
}
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/219