public class ListInsert {;public static long[] insert(long[] arr,int i,long l){;//新建陣列,對原陣列擴容;long[] arr1 = new long[arr.length+1];;//將原陣列資料賦值給新陣列;for(int j = 0;j<arr.length;j++){;arr1[j] = arr[j];;};//將大於i的資料向後移動一位;for(int j = arr1.length-2;j>i;j--){;arr1[j+1] = arr1[j];;};//賦值到i位置;arr1[i+1] = l;;return arr1;;};//測試;public static void main(String[] args){;long[] arr = {12,25,11,36,14};;long[] arr1 = insert(arr, 2, 100);;for (long l : arr1) {;System.out.print(l+" ");;};};陣列的擴容方式:arr = Arrays.copyOf(arr, arr.length+1);
public class ListInsert {;public static long[] insert(long[] arr,int i,long l){;//新建陣列,對原陣列擴容;long[] arr1 = new long[arr.length+1];;//將原陣列資料賦值給新陣列;for(int j = 0;j<arr.length;j++){;arr1[j] = arr[j];;};//將大於i的資料向後移動一位;for(int j = arr1.length-2;j>i;j--){;arr1[j+1] = arr1[j];;};//賦值到i位置;arr1[i+1] = l;;return arr1;;};//測試;public static void main(String[] args){;long[] arr = {12,25,11,36,14};;long[] arr1 = insert(arr, 2, 100);;for (long l : arr1) {;System.out.print(l+" ");;};};陣列的擴容方式:arr = Arrays.copyOf(arr, arr.length+1);