Array required but arraylist int [] found

0

I do not understand the reason for this error:

// Método para obtener las propiedades del jugador 
    public ArrayList<int[]> obtenerPropiedadesJugador (){ // MIRAR
       int i = 0;
       boolean pertenece = false;
       ArrayList <int[]> propiedades_JA = new ArrayList ();

       for (Casilla casilla: tablero.getCasillas()){
            for(TituloPropiedad propiedad: jugadorActual.getPropiedades()){
                if(casilla.getTituloPropiedad() == propiedad)
                    pertenece = true;
            }
          if (pertenece){
              propiedades_JA [i]= casilla.getNumeroCasilla();
          }
       }
       return propiedades_JA;
    }
    
asked by Miguel Campos 11.11.2018 в 18:16
source

1 answer

1

Despite its name, ArrayList is not an array and does not work as such, it's just an implementation of List that internally uses an array.

Thus, the operator [] that is applied to the arrays is not applicable to propiedades_JA as defined; you can use ArrayList methods.

When defining

ArrayList <int[]> propiedades_JA = new ArrayList<>(); //supongo que en el código estará el <>, porque si no no compila.

What you say is that propiedades_JA is a ArrayList , and that each element of ArrayList will be an array of ints ( int[] ).

So probably what you want is

int propiedades_JA[] = new int[X]; // con X siendo el total de elementos de tablero.getCasillas().

Naturally, you'll also have to change the type you return.

In another order of things, I guess you do not increase i within the loop because you have forgotten.

    
answered by 11.11.2018 в 20:03