INsérer un élémént dans un tableau2D
guipe
-
GUIPE Messages postés 98 Statut Membre -
GUIPE Messages postés 98 Statut Membre -
Bonjour, j'ai un tableau 2D dans lequel je voudrais insérer des objets de type Element !
Voici ce que j'ai fait mais ça ne marche pas !
while(i<nb_hopitaux)
{
x_aleatoire=generator.nextInt(20)+1;
y_aleatoire=generator.nextInt(20)+1;
if(elements[x_aleatoire][y_aleatoire]==null)
{
[COLOR="Red"]elements[x_aleatoire][y_aleatoire]=new Hopital(super(x,y),hop.getRepresentation())[/COLOR];
}
else if(elements[x_aleatoire][y_aleatoire]!=null)
{
x_aleatoire=generator.nextInt(20)+1;
y_aleatoire=generator.nextInt(20)+1;
[COLOR="Red"]elements[x_aleatoire][y_aleatoire]=new Hopital(x_aleatoire,y_aleatoire,'H');[/COLOR]
}
}i=0;
Voici ce que j'ai fait mais ça ne marche pas !
while(i<nb_hopitaux)
{
x_aleatoire=generator.nextInt(20)+1;
y_aleatoire=generator.nextInt(20)+1;
if(elements[x_aleatoire][y_aleatoire]==null)
{
[COLOR="Red"]elements[x_aleatoire][y_aleatoire]=new Hopital(super(x,y),hop.getRepresentation())[/COLOR];
}
else if(elements[x_aleatoire][y_aleatoire]!=null)
{
x_aleatoire=generator.nextInt(20)+1;
y_aleatoire=generator.nextInt(20)+1;
[COLOR="Red"]elements[x_aleatoire][y_aleatoire]=new Hopital(x_aleatoire,y_aleatoire,'H');[/COLOR]
}
}i=0;
Configuration: Linux Firefox 2.0.0.17
3 réponses
-
Bonjour, j'ai un tableau 2D dans lequel je voudrais insérer des objets de type Element !
Voici ce que j'ai fait mais ça ne marche pas !
while(i<nb_hopitaux)
{
x_aleatoire=generator.nextInt(20)+1;
y_aleatoire=generator.nextInt(20)+1;
if(elements[x_aleatoire][y_aleatoire]==null)
{
elements[x_aleatoire][y_aleatoire]=new Hopital(super(x,y),hop.getRepresentation());
}
else if(elements[x_aleatoire][y_aleatoire]!=null)
{
x_aleatoire=generator.nextInt(20)+1;
y_aleatoire=generator.nextInt(20)+1;
elements[x_aleatoire][y_aleatoire]=new Hopital(x_aleatoire,y_aleatoire,'H');
}
}i=0; -
Hopital est une sous classe de Element, le type de mon tableau :
Element elements=new Element[][];
Dans la premiere ligne hop est une référence à Hopital j'ai fait Hopital hop;
Je nsais pas comment faire sa ne marche pas !!! -
En fait je crée un tableau d'objets comme ceci :
Object[][] elements=new Object[largeur][hauteur];
Et maintenant je veux par exemple créer un objet hopital(défini dans le constructeur de la classe Hopital), comment puis-je faire ?