Delete a row of a two-dimensional Array in JAVA

1

I have the following Bidimensional arrangement called info :

Laura,9,8,10,10,10,10,20  
Pedro,10,50,10,35,10,10,90  
Luis,10,60,10,70,10,41,17  
Mario,10,10,10,43,10,10,23  
Blanca,10,89,41,10,10,85,17  

What would be the code to be able to eliminate any row?
For example, the whole row 2. I know that it is in the position info[2][0] however, I do not know what the code would be to delete the row.

The code I have only goes through the arrangement:

for (int x=0; x < info.length; x++) {  
     for (int y=0; y < info[x].length; y++)  
          System.out.print(info[x][y]+"\t");     
     System.out.println("\n");  
}  

Investigating I have only found that when removing the array , only replace the values by zeros, but that is not what I need, if not delete the row completely and re-dimension the < em> array .

    
asked by Alberto 22.11.2018 в 09:26
source

2 answers

1

You can call a method that through a for travel the two-dimensional array (also called "matrix") and create a new cloned matrix of yours on each round, check the row you want to delete and do not add row in the clone of your matrix.

I have created a functional example that you can execute from this link (click on Execute at the top left)

link

In my example, the matrix is 3 rows and 3 columns, because I was too lazy to put all your data.

Greetings

    
answered by 22.11.2018 / 10:12
source
2

Unable to resize an array, the size is fixed once created. The normal thing in these cases is to use a data structure that allows that flexibility (For example, use lists (the interface java.util.List , with the classes java.util.ArrayList or java.util.LinkedList ).

Another option is to have a separate variable that indicates the number of rows used and to re-arrange them in the array as needed.

To delete from an array like the one you present, you could do something like the following:

String [][] info = new Object[][] {
    {"Laura,""9","8","10","10","10","10","20}",
    {"Pedro","10","50","10","35","10","10","90"},
    {"Luis","10","60","10","70","10","41","17"},
    {"Mario","10","10","10","43","10","10","23"},
    {"Blanca","10","89","41","10","10","85","17"}
}

//eliminemos la fila 2

static void eliminarFila(String[][] info,int f) {
    for (int i=f; i < info.length; i++) {
        info[i]=info[i+1]; //movemos el resto de filas a la posición anterior
    }
    info[info.length - 1] =null; //la última posición queda vacía
}

Another option would be to create a new array, discarding the old one:

static String[][] eliminarFila(String[][] info,int f) {
    if (info.length==0) {
        return info; //ya está vacío
    } 

    String [][] resultado= new String[info.length-1][];
    for (int i=0; i < f; i++) {
        resultado[i]=info[i]; //simplemente copiamos las filas anteriores
    }
    for (int i=f; i < info.length; i++) {
        resultado[i]=info[i+1]; //ahora copiamos las posteriores
    }
    return resultado
}
    
answered by 22.11.2018 в 10:07