例:
public static void main(String[] args) {
//現有陣列,長度為4
int[] array1 = new int[] {4, 5, 6, 7};
int indexForDelete= 2;
//新建陣列,長度為3 即 [array1.length-1]
int[] newArray = new int[array1.length-1];
for(int i=0;i<newArray.length; i++) {
//跳過array[2]的賦值
if(i<num) {
//索引0,1對應元素賦值
newArray[i] = array1[i];
}else {
//索引3對應元素賦值
newArray[i] = array1[i+1];
}
// 列印輸出陣列內容
System.out.println(Arrays.toString(array1));
array1 = newArray;
例:
public static void main(String[] args) {
//現有陣列,長度為4
int[] array1 = new int[] {4, 5, 6, 7};
int indexForDelete= 2;
//新建陣列,長度為3 即 [array1.length-1]
int[] newArray = new int[array1.length-1];
for(int i=0;i<newArray.length; i++) {
//跳過array[2]的賦值
if(i<num) {
//索引0,1對應元素賦值
newArray[i] = array1[i];
}else {
//索引3對應元素賦值
newArray[i] = array1[i+1];
}
}
// 列印輸出陣列內容
System.out.println(Arrays.toString(array1));
array1 = newArray;
System.out.println(Arrays.toString(array1));
}