Pass Bidimensional Array element to an ArrayList [closed]

0

I am trying to do an exercise in which I am asked for Array Bi 5x5 and fill them with Car objects that I have created in which a Speed and a March are passed.

Then what I have to do is pass the elements of that Array Bi that contain cars that go faster from 60Km/h to ArrayList .

My Array creation code and automatic filling is the following (I do not put where I do the Car class and its methods):

Coche tabla[][] = new Coche[5][5];          //Creamos Array Bidimensional de 5x5
    ArrayList <Coche> Rapidos = new ArrayList <Coche>();            //Creamos ArrayList para coches que tengan velocidad mayor a 60.
    ArrayList <Coche> Lentos = new ArrayList <Coche>();             //Creamos ArrayList para coches que tengan velocidad menor o  igul 40.


    for (int i = 0; i < 5; i++) {           //Recorremos parte " i " de la matriz
        for (int j = 0; j < 5; j++) {           //Recorremos parte " j " de la matriz
            tabla[i][j] = new Coche();              //En cada posición de la matriz se crea un instancia de objeto " Coche"
            tabla[i][j].setVelocidad(Math.round((Math.random() * 110 + 10)));           // En posicion [i][j] se da velocidad entre 20 -120
            tabla[i][j].setMarcha((int) Math.round(Math.random() * 6));         // En posicion [i][j] se da marcha entre 0 - 6

            if ((tabla[i][j].getVelocidad()) > 60) {

                Rapidos.add(new Coche());

            }

        }
    }
    
asked by Ivan Vieites Lores 02.03.2017 в 12:35
source

1 answer

0

Well seeing what you need I think the simplest and most understandable (although inefficient) would be to create a for structure in another for to fill it and then move on to analyze if it is fast or slow with another for-for. p>

It could be something like this:

Coche tabla[][] = new Coche[5][5];          //Creamos Array Bidimensional de 5x5
ArrayList <Coche> Rapidos = new ArrayList <Coche>();            //Creamos ArrayList para coches que tengan velocidad mayor a 60.
ArrayList <Coche> Lentos = new ArrayList <Coche>();             //Creamos ArrayList para coches que tengan velocidad menor o  igul 40.

for (int i = 0; i < 5; i++) {           //Recorremos parte " i " de la matriz
    for (int j = 0; j < 5; j++) {           //Recorremos parte " j " de la matriz
        tabla[i][j] = new Coche();              //En cada posición de la matriz se crea un instancia de objeto " Coche"
        tabla[i][j].setVelocidad(Math.round((Math.random() * 110 + 10)));           // En posicion [i][j] se da velocidad entre 20 -120
        tabla[i][j].setMarcha((int) Math.round(Math.random() * 6));         // En posicion [i][j] se da marcha entre 0 - 6
    }
}

for (int i = 0; i < 5; i++) {           //Recorremos parte " i " de la matriz
    for (int j = 0; j < 5; j++) {           //Recorremos parte " j " de la matriz
        if(tabla[i][j].getVelocidad() > 60) {
            Rapidos.add(tabla[i][j]);
        } else {
            Lentos.add(tabla[i][j]);
        }
    }
}

This would be a way to solve the problem. Obviously ifs you can put them in the first for-for to be more efficient but I think that it is more differentiated what each one does and is more understandable.

    
answered by 02.03.2017 / 12:44
source