有没有办法在2d数组中删除特定的行?

问题描述:

可能重复:
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

原始阵列真的很在其功能范围有限。如果您需要能够执行更复杂的操作,最简单的方法是跳过一些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

的二维数组只不过是数组的数组。 因此,我们可以删除只分配空:

arr[4] = new String[n]; 

如果要删除第5行完全,那么你可以通过指定空这么做。 数组意味着固定的长度和灵活性,就像ArrayList会在数组中添加额外的代码一样。

+0

这不会删除该行,它只会将其设置为空值。它仍然会在阵列中。 – pap 2011-06-14 07:11:50

使用下面的代码从二维数组中删除特定行

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

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

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; 
}