需求:数组拷贝,将给定的数组生成一个一模一样的数组之后输出结果
代码实现:
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
//黑马案例05-数组拷贝
//需求:eg原数组(11,22,33),拷贝一个一模一样的数组
int[] arr1 = add();
int[] arr3 = copy(arr1);
for (int i = 0; i < arr3.length; i++) {
if (i==0)
{
System.out.print("{" + arr3[i] + ",");
}
else if (i <arr3.length-1)
{
System.out.print(arr3[i] + ",");
}
else {
System.out.print(arr3[i] + "}");
}
}
}
public static int[] copy(int[] arr1) {
int[] arr2 = new int[arr1.length];
for (int i = 0; i < arr2.length; i++) {
arr2[i] = arr1[i];
}
return arr2;
}
public static int[] add ()
{
int [] arr =new int[4];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
System.out.println("请输入第" + (i + 1) + "个整数数字");
arr[i] = sc.nextInt();
}
return arr;
}
}