开发者

Is there a way to drop a specific row in a 2d array?

Possible Duplicate:

How to remove a row in two-dimensional array

So I have a 2d array String[][] array, and there's like 9 rows, I want to remove row number five. Is there a way I can tell java to remove that row five like I can开发者_如何学JAVA easily do with ArrayLists?


primitive arrays are really very limited in their functional scope. If you need to be able to perform more sophisticated operations like this, the easiest way is to jump across some List implementation.

Like:

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[][]{});


Use the following code to remove specific row from 2d array

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


Well and if you dont want to use Lists, I made this method:

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


The two dimension array is nothing but the array of arrays. So we can remove the just assign empty:

 arr[4] = new String[n];

If you want to remove the 5th row entirely then you can do so by assigning null. Array is meant to be fixed length and flexibility like ArrayList would come with additional coding over the arrays.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜