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());
}
}
}