在Java中,有哪些不同的方法可以将一个数组复制到另一个数组?
一般来说,数组是存储多个相同数据类型变量的容器。它们是固定大小的,大小在创建时确定。数组中的每个元素都由从0开始的数字定位。您可以使用名称和位置访问数组的元素,例如:
System.out.println(myArray[3]); //Which is 1457 Creating an array in Java:
在Java中,数组被视为引用类型,您可以使用与对象类似的new关键字创建数组,并使用索引填充它,例如:
int myArray[] = new int[7]; myArray[0] = 1254; myArray[1] = 1458; myArray[2] = 5687; myArray[3] = 1457; myArray[4] = 4554; myArray[5] = 5445; myArray[6] = 7524;
或者,您可以直接在大括号中赋值,用逗号 (,) 分隔它们,例如:
int myArray = { 1254, 1458, 5687, 1457, 4554, 5445, 7524};
数组复制
您可以通过几种方法将一个数组复制到另一个数组:
逐元素复制 - 一种方法是创建一个与原始数组长度相同的空数组,然后(在循环中)复制每个元素。
示例
import java.util.Arrays; public class CopyingArray { public static void main(String args[]) { //Source array (int) int integerArray1[] = { 1254, 1458, 5687, 1457, 4554, 5445, 7524}; //Length of the array int length1 = integerArray1.length; //Creating an empty array int integerArray2[] = new int[length1]; for (int i=0; i<length1; i++) { integerArray2[i] = integerArray1[i]; } System.out.println("Original array: "+Arrays.toString(integerArray1)); System.out.println("Copied array: "+Arrays.toString(integerArray2)); //Source array (String) String StringArray1[] = { "Mango", "Apple", "Orange", "Banana", "Cherries"}; //Length of the array int length2 = StringArray1.length; //Creating an empty array String StringArray2[] = new String[length2]; for (int i=0; i<length2; i++) { StringArray2[i] = StringArray1[i]; } System.out.println("Original array: "+Arrays.toString(StringArray1)); System.out.println("Copied array: "+Arrays.toString(StringArray2)); } }
输出
Original array: [1254, 1458, 5687, 1457, 4554, 5445, 7524] Copied array: [1254, 1458, 5687, 1457, 4554, 5445, 7524] Original array: [Mango, Apple, Orange, Banana, Cherries] Copied array: [Mango, Apple, Orange, Banana, Cherries]
使用clone()方法 - `java.lang.Object`类的**clone()**方法接受一个对象作为参数,创建并返回它的一个副本。
示例
import java.util.Arrays; public class CopyingArray { public static void main(String args[]) { //Source array (int) int integerArray1[] = { 1254, 1458, 5687, 1457, 4554, 5445, 7524}; //Cloning the array int integerArray2[] = integerArray1.clone(); System.out.println("Original array: "+Arrays.toString(integerArray1)); System.out.println("Copied array: "+Arrays.toString(integerArray2)); //Source array (String) String StringArray1[] = { "Mango", "Apple", "Orange", "Banana", "Cherries"}; //Cloning the array String StringArray2[] = StringArray1.clone(); System.out.println("Original array: "+Arrays.toString(StringArray1)); System.out.println("Copied array: "+Arrays.toString(StringArray2)); } }
输出
Original array: [1254, 1458, 5687, 1457, 4554, 5445, 7524] Copied array: [1254, 1458, 5687, 1457, 4554, 5445, 7524] Original array: [Mango, Apple, Orange, Banana, Cherries] Copied array: [Mango, Apple, Orange, Banana, Cherries]
使用System.arraycopy()方法 - System类的`arraycopy()`方法接受两个数组(以及其他细节),并将一个数组的内容复制到另一个数组。
示例
import java.util.Arrays; public class CopyingArray { public static void main(String args[]) { //Source array (int) int integerArray1[] = { 1254, 1458, 5687, 1457, 4554, 5445, 7524}; //Length of the array int length1 = integerArray1.length; //Destination array int integerArray2[] = new int[length1]; //Copying Arrays System.arraycopy(integerArray1, 0, integerArray2, 0, length1); System.out.println("Original array: "+Arrays.toString(integerArray1)); System.out.println("Copied array: "+Arrays.toString(integerArray2)); //Source array (String) String StringArray1[] = { "Mango", "Apple", "Orange", "Banana", "Cherries"}; //Length of the array int length2 = StringArray1.length; //Destination array String StringArray2[] = new String[length2]; //Copying arrays System.arraycopy(StringArray1, 0, StringArray2, 0, length2); System.out.println("Original array: "+Arrays.toString(StringArray1)); System.out.println("Copied array: "+Arrays.toString(StringArray2)); } }
输出
Original array: [1254, 1458, 5687, 1457, 4554, 5445, 7524] Copied array: [1254, 1458, 5687, 1457, 4554, 5445, 7524] Original array: [Mango, Apple, Orange, Banana, Cherries] Copied array: [Mango, Apple, Orange, Banana, Cherries]
广告