2017-04-15 69 views
1

我想寫一個java方法,將採取一個2d數組,並將內容添加到一個新的二維數組期望指定的行。所以,如果我有二維數組從一個二維數組中刪除一列java

1234 
1234 
1234 

,我想刪除第3列,我想獲得

124 
124 
124 

問題是我無法弄清楚如何得到這個工作。我能想到的最好的方法如下。

private static int[][] removeCol(int [][] array, int colRemove) 
{ 
    int row = array.length; 
    int col = array[0].length; 

    int [][] newArray = new int[row][col]; 

    for(int i = 0; i < row; i++) 
    { 
     for(int j = 0; j < col; j++) 
     { 
      if(j != colRemove) 
      { 
       newArray[i][j] = array[i][j]; 
      } 
     } 
    } 

    return newEx; 
} 

眼下這個方法將返回該

1204 
1204 
1204 

但它會運行得更好,如果我能得到我想要的結果。有沒有辦法做到這一點,或者我堅持我目前的結果?

+1

提示:(a)新陣列必須具有較少的列 - 比原來的更小。 (b)要刪除的列後面的每個列應該位於索引處*比以前少*。因此,如果您刪除第3列,則第4列中的內容現在在第3列,第5列中的內容現在在第4列等等。 – RealSkeptic

回答

0

只需使用另一個索引並不會自動循環運行增量:

private static int[][] removeCol(int [][] array, int colRemove) 
{ 
int row = array.length; 
int col = array[0].length-1; 
int oldCol = array[0].length; 

int [][] newArray = new int[row][col]; 

for(int i = 0; i < row; i++) 
{ 
    for(int j = 0, k=0; j < oldCol && k < col; j++) 
    { 
     if(j != colRemove) 
     { 
      newArray[i][k++] = array[i][j]; 
     } 
    } 
} 

return newArray; 
} 
0

通過保持你的邏輯和從零開始的列數:

private static int[][] removeCol(int[][] array, int colRemove) { 
    int row = array.length; 
    int col = array[0].length; 

    int[][] newArray = new int[row][col - 1]; // You will have one column less 

    for (int i = 0; i < row; i++) { 
     for (int j = 0; j < col; j++) { 
     if (j != colRemove) { 
      newArray[i][j > colRemove ? j -1 : j] = array[i][j]; // If you're looking at an index greater than the one to remove, you have to reduce index by one 
     } 
     } 
    } 

    return newArray; 
    } 
1

你可以有一個變量currColumn這表示當前列的位置,結果數組的列將比原始列少一列。所以根據這個你可以改變你的代碼。

private static int[][] removeCol(int [][] array, int colRemove) 
{ 
    int row = array.length; 
    int col = array[0].length; 

    int [][] newArray = new int[row][col-1]; //new Array will have one column less 


    for(int i = 0; i < row; i++) 
    { 
     for(int j = 0,currColumn=0; j < col; j++) 
     { 
      if(j != colRemove) 
      { 
       newArray[i][currColumn++] = array[i][j]; 
      } 
     } 
    } 

    return newEx; 
} 

另一個更好的方法是使用像ArrayList這樣的動態結構。所以在這裏你需要有ArrayList Array,然後你可以用remove()方法去除元素。如果你想更新任何元素,那麼你可以使用set()方法。

0

在第二次迭代中,檢查colRemove值併爲其他迭代移動+1。見下面

private static int[][] removeCol(int [][] array, int colRemove) 
{ 
    int row = array.length; 
    int col = array[0].length-1; 

    int [][] newArray = new int[row][col]; 

    for(int i = 0; i < row; i++) 
    { 
     for(int j = 0; j < col; j++) 
     { 
      if(j>=colRemove){ 
       newArray[i][j] = array[i][j+1]; 
      } 
      else{ 
       newArray[i][j] = array[i][j]; 
      } 
     } 
    } 

    return newArray; 
} 
0

代碼其實你替換的元素由int默認值去掉,那就是:0

如果不刪除您複製元素的元素:

if(j != colRemove) 
{ 
    newArray[i][j] = array[i][j]; 
} 

否則你什麼也不做(所以從int採用默認0值)。

您應該從1減少新創建的數組的第二維。

你可以按照這種方式:

  • 使用全局循環,該行迭代(數組的第一個維度)

  • 使用兩個連續的內部循環迭代的列(陣列的第二維)。
    第一個迭代直到「要刪除的列-1」,並在新數組中創建元素的簡單副本。
    第二個從「要移除的列」開始,並移動到新數組中原始數組的每個元素左側。

這裏是一個工作碼:

import java.util.Arrays; 

public class Array2D { 

    public static void main(String[] args) { 

     int[][] arrayOriginal = new int[][]{{1,2,3,4},{1,2,3,4},{1,2,3,4}}; 
     int[][] arrayNew = removeCol(arrayOriginal, 2); 
     System.out.println(Arrays.deepToString(arrayNew));; 
    } 
    private static int[][] removeCol(int [][] array, int colRemove) 
    { 
     int row = array.length; 
     int col = array[0].length; 

     int [][] newArray = new int[row][col-1]; 

     for(int i = 0; i < row; i++) 
     { 
      for(int j = 0; j < colRemove; j++) 
      {    
       newArray[i][j] = array[i][j];     
      } 

      for(int j = colRemove; j < col-1; j++) 
      {       
       newArray[i][j] = array[i][j+1]; 
      } 

     } 

     return newArray; 
    } 
} 

的輸出是:

[[1, 2, 4], [1, 2, 4], [1, 2, 4]]