Estou com uma duvida e não estou conseguindo achar a resposta.
Eu estava querendo criar uma matriz e cada posição da matriz[x][y] colocar um objeto do tipo"nave"(a classe nave já criei com os seus atributos,métodos e construtores),entretanto não estou conseguindo fazer.
Não tem segredo. Você declara e cria um array bidimensional da seguinte forma:
Nave[][] matrix = new Nave[3][3];
Depois só é preciso criar os objetos.
matrix[0][0] = new Nave();
matrix[0][1] = new Nave();
matrix[0][2] = new Nave();
/* e assim por diante */
Fiz um exemplo funcional, veja:
class Nave {
private int x, y;
Nave(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return String.format("[%d, %d]", this.x, this.y);
}
}
class Main {
public static void main(String[] args) {
int MAX_X = 3, MAX_Y = 3;
Nave[][] matrix = new Nave[MAX_X][MAX_Y];
for (int x = 0; x < MAX_X; x++) {
for (int y = 0; y < MAX_Y; y++) {
matrix[x][y] = new Nave(x, y);
}
}
for (int x = 0; x < MAX_X; x++) {
for (int y = 0; y < MAX_Y; y++) {
System.out.println(matrix[x][y]);
}
}
}
}