2

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 easily do with ArrayLists?

Community
  • 1
  • 1
Anthony Honciano
  • 1,683
  • 5
  • 24
  • 30
  • maybe this will help you: http://stackoverflow.com/questions/1805147/how-to-remove-a-row-in-two-dimensional-array – Andrei Sfat Jun 14 '11 at 07:00
  • You have to create new 2D array. You can't remove rows. – Pankaj Kumar Jun 14 '11 at 07:13
  • Possible duplicate of [How to remove a row in two-dimensional array](https://stackoverflow.com/questions/1805147/how-to-remove-a-row-in-two-dimensional-array) – Cœur Jul 12 '18 at 15:29

4 Answers4

4

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[][]{});
pap
  • 27,064
  • 6
  • 41
  • 46
  • Wow...that was very simple. As a matter of fact, this was my answer. @Deepak, thanks for the effort too. – Anthony Honciano Jun 14 '11 at 10:23
  • There's no need to create an `ArrayList` because using `Arrays.asList(array)` returns you a `List` – Grekz Jun 14 '11 at 20:10
  • Arrays.asList returns an immutable list backed by the original array. That's way you have to create a new ArrayList instance. – pap Jun 15 '11 at 10:58
0

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);
    }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • Very similar to http://stackoverflow.com/questions/1805147/how-to-remove-a-row-in-two-dimensional-array/1805249#1805249 – Robert Harvey Jun 14 '11 at 19:08
0

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;
}
Grekz
  • 1,570
  • 15
  • 22
-2

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.

  • That will not remove the row, it will just set it to null-values. It will still be there in the array. – pap Jun 14 '11 at 07:11