1 public class Homework04 { 2 //编写一个 main 方法 3 public static void main(String[] args) { 4 5 int[] oldArr = {10,30,50}; 6 A03 a03 = new A03(); 7 int[] newArr = a03.copyArr(oldArr); 8 //遍历newArr,验证 9 System.out.println("==返回的newArr元素情况=="); 10 for(int i = 0; i < newArr.length;i++){ 11 System.out.print(newArr[i] + "\t"); 12 } 13 14 15 16 } 17 } 18 /* 19 编写类A03,实现数组的复制功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样 20 */ 21 class A03{ 22 public int[] copyArr(int[] oldArr){ 23 //在堆中,创建一个长度为oldArr.length数组 24 int[] newArr = new int[oldArr.length]; 25 //遍历oldArr,将元素拷贝到newArr 26 for(int i = 0;i<oldArr.length;i++){ 27 newArr[i] = oldArr[i]; 28 } 29 30 return newArr; 31 } 32 }
标签:copyArr,int,A03,数组,newArr,oldArr From: https://www.cnblogs.com/wzyxslx/p/18373063