//知識點:實現int類型數組元素拷貝 class ArrayElementCopy { public static void main(String[] args) { int[] src = new int[]{1,2,3,4,5,6,7,8,9,10}; int[] dest = new int[]{10,0,0,0,0,0,0,0,0,0}; //需求:從src數組中拷貝3,4,5,6元素到dest數組中 printArray(dest); copy(src,2,4,dest,3); //調用拷貝操做的方法 printArray(dest); } /* 參數: src: 原數組 srcPos: 從原數組中哪個索引位置開始拷貝 number: 拷貝元素的個數 dest:目標數組 destPost: 目標起始位置 */ static void copy(int[] src, int srcPos, int number, int[] dest, int destPos) { int srcStartPos = srcPos; int srcEndPos = srcStartPos + number; for(int i=srcStartPos; i<srcEndPos; i++) { dest[destPos] = src[i]; destPos++; } } static void printArray(int[] arr) { String ret = "["; for (int i=0; i<arr.length; i++) { ret = ret + arr[i]; if (i != arr.length -1) { ret = ret + ", "; } } ret = ret + "]"; System.out.println(ret); } }