Return an index of a 2d array in java

0

I have the question of how to return a coordinate value of an index String array [] [] in java how could i do it I have the following function and it does not work for me.

public String [][] getCasillaVacia() {
    for (int i=0; i < estado[0].length; i++) {

        for (int j=0; j < estado[i].length; j++) {

             if(estado[i][j].replace(" ", "").equals("0")) {
                 System.out.println("Casilla encontrada en la posicion: " + i + "," + j);
                 return estado[i][j];
             }
        }
     }
}
    
asked by Eduardo Tolentino 14.09.2018 в 00:17
source

2 answers

0

The solution I did was the following:

public int[] getCasillaVacia() {

        int posicion [] = null;

        for (int i=0; i < estado[0].length; i++) {

            for (int j=0; j < estado[i].length; j++) {

                 if(estado[i][j].replace(" ", "").equals("0")) {
                     System.out.println("Casilla encontrada en la posicion: " + i + "," + j);
                     posicion = new int []{i, j};  
                 }
            }
         }
        return posicion;
    }
    
answered by 14.09.2018 / 01:31
source
0

Change the return type "String [] []" to "String". You are returning only one string, not an array.

public String getCasillaVacia() {
for (int i=0; i < estado[0].length; i++) {

    for (int j=0; j < estado[i].length; j++) {

         if(estado[i][j].replace(" ", "").equals("0")) {
             System.out.println("Casilla encontrada en la posicion: " + i + "," + j);
             return estado[i][j];
         }
    }
 }
}
    
answered by 14.09.2018 в 00:34