2011-06-14 78 views
2

可能重複:
How to remove a row in two-dimensional array有沒有辦法在2d數組中刪除特定的行?

所以我有一個二維數組的String [] []數組,並有像9行,我想刪除行排名第五。有沒有一種方法可以告訴java刪除第5行,就像我可以輕鬆地用ArrayLists做的那樣?

+0

也許這將幫助你:http://stackoverflow.com/questions/1805147/how-to-remove-a-row-in-two-dimensional-array – sfat 2011-06-14 07:00:18

+0

你甲肝e創建新的二維數組。你不能刪除行。 – 2011-06-14 07:13:34

回答

3

原始陣列真的很在其功能範圍有限。如果您需要能夠執行更復雜的操作,最簡單的方法是跳過一些List實現。

像:

String[][] array; 
array = new String[][]{new String[]{"a", "b", "c"},new String[]{"d", "e", "f"},new String[]{"g", "h", "i"}}; 

List<String[]> l = new ArrayList<String[]>(Arrays.asList(array)); 

l.remove(1); 
String[][] array2 = l.toArray(new String[][]{}); 
+0

哇......這很簡單。事實上,這是我的答案。 @Deepak,謝謝你的努力。 – 2011-06-14 10:23:47

+0

沒有必要創建一個ArrayList的''由於採用'Arrays.asList(陣列)'回報你'名單' – Grekz 2011-06-14 20:10:07

+0

Arrays.asList返回由原始數組支持的不可變列表。這就是你必須創建一個新的ArrayList實例。 – pap 2011-06-15 10:58:10

-2

的二維數組只不過是數組的數組。 因此,我們可以刪除只分配空:

arr[4] = new String[n]; 

如果要刪除第5行完全,那麼你可以通過指定空這麼做。 數組意味着固定的長度和靈活性,就像ArrayList會在數組中添加額外的代碼一樣。

+0

這不會刪除該行,它只會將其設置爲空值。它仍然會在陣列中。 – pap 2011-06-14 07:11:50

0

使用下面的代碼從二維數組中刪除特定行

import java.util.ArrayList; 
import java.util.List; 


public class RemoveRowFrom2dArray 
{ 
    private double[][] data; 

    public RemoveRowFrom2dArray(double[][] data) 
    { 
     int r= data.length; 
     int c= data[0].length; 
     System.out.println("....r...."+r+"....c...."+c); 
     this.data= new double[r][c]; 
     for(int i = 0; i < r; i++) { 
      for(int j = 0; j < c; j++) { 
        this.data[i][j] = data[i][j]; 
      } 
     } 
    } 

    /* convenience method for getting a 
     string representation of matrix */ 
    public String toString() 
    { 
     StringBuilder sb = new StringBuilder(1024); 
     for(double[] row : this.data) 
     { 
       for(double val : row) 
       { 
         sb.append(val); 
         sb.append(" "); 
       } 
       sb.append("\n"); 
     } 

     return(sb.toString()); 
    } 

    public void removeRowsWithRowNumber(double rowNotToBeAdd) 
    { 
     List<double[]> rowsToKeep = new ArrayList<double[]>(this.data.length); 
     for(int i =0; i<this.data.length; i++){ 
      if(i!=rowNotToBeAdd){ 
      double[] row = this.data[i]; 
      rowsToKeep.add(row); 
      } 
     } 

     this.data = new double[rowsToKeep.size()][]; 
     for(int i=0; i < rowsToKeep.size(); i++) 
     { 
       this.data[i] = rowsToKeep.get(i); 
     } 
    } 

    public static void main(String[] args) 
    { 
     double[][] test = { {1, 2, 3, 4, 5, 6, 7, 8, 9}, 
               {6, 2, 7, 2, 9, 6, 8, 10, 5}, 
               {2, 6, 4, 7, 8, 4, 3, 2, 5}, 
               {9, 8, 7, 5, 9, 7, 4, 1, 10}, 
               {5, 3, 6, 8, 2, 7, 3, 7, 2} }; 

      //make the original array and print it out   
     RemoveRowFrom2dArray m = new RemoveRowFrom2dArray(test); 
     System.out.println(m); 

      //remove rows with trow number 4 from the 2d array 
     m.removeRowsWithRowNumber(4); 
     System.out.println(m); 
    } 
} 
+0

非常類似於http://stackoverflow.com/questions/1805147/how-to-remove-a-row-in-two-dimensional-array/1805249#1805249 – 2011-06-14 19:08:20

0

好吧,如果你不想使用列表,我做了這個方法:

public String[][] removeRowFrom2dArray(String[][] array, int row){ 
    int rows = array.length; 
    String[][] arrayToReturn = new String[rows-1][]; 
    for(int i = 0; i < row; i++) 
     arrayToReturn[i] = array[i]; 
    for(int i = row; i < arrayToReturn.length; i++) 
     arrayToReturn[i++] = array[i]; 
    return arrayToReturn; 
} 
相關問題