ArrayList qui ne conserve pas les elements.
Bonjour,
Je suis un debutant en JAVA et je ne pige pas un truc.
J'ai une class qui contient les info d'un Polygon.
Code:
1 2 3 4 5 6 7 8 9 10
|
public class Hexagone {
private int _id;
private int _rayon ;
private Point _location;
private Polygon _pts;
private int[] _idHexaNear;
les constructeurs
et get et set
} |
J'ai une autre class Hexagones.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
public class Hexagones {
private int _rayon;
private int _nbHexaX;
private int _nbHexaY;
private Angles _angles;
private int _nbHexa;
private ArrayList<Hexagone> _tabHexa;
public Hexagones(){
this._angles = new Angles();
this._tabHexa = new ArrayList<Hexagone>();
}
public Hexagones(int NbX, int NbY, int r){
this._rayon = r;
this._nbHexaX = NbX;
this._nbHexaY = NbY;
this._nbHexa = NbX*NbY;
this._tabHexa = new ArrayList<Hexagone>();
this._angles = new Angles();
}
public int Creer(){
Point centreHaxa;
Point PremierPt;
float pasY, pasY2;
int nb = 0;
pasY = (float) (this._rayon * this._angles.posY(2));
pasY2 = (float) ((this._rayon * this._angles.posY(2)) * 2);
PremierPt = new Point(this._rayon,(int) pasY);
centreHaxa = new Point(PremierPt.x,PremierPt.y);
System.out.println("pas : " + pasY);
System.out.println("pas2 : " + pasY2);
for (int j=0;j<(this._nbHexaX);j++ ){
for (int i=0;i<(this._nbHexaY);i++){
centreHaxa.y=(int) (PremierPt.y + (i * pasY2));
Hexagone temp = new Hexagone(nb, centreHaxa, _angles, _rayon);
this._tabHexa.add(temp);
temp=null;
nb++;
}
PremierPt.x=(int) ((centreHaxa.x) + _rayon + (_rayon * _angles.posX(1)));
centreHaxa.x=(PremierPt.x);
if (PremierPt.y == pasY){
PremierPt.y = (int) pasY2;
}else{
PremierPt.y=(int) pasY;
}
centreHaxa.y=(PremierPt.y);
}
return nb++;
}
return (Hexagone) this._tabHexa.get(i);
}
public int nbHexagone(){
return this._tabHexa.size() ;
}
} |
Mon probleme, c'est que lorsque je veux recuperer tous les elements de la _tabHexa.
Il ne me donne que les valeurs du dernier element engeristrer dans l'ArrayList !!
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public static void main(String[] args) {
Hexagones H = new Hexagones(5, 5, 15);
Integer nb;
nb = H.Creer();
for (int i=0;i<H.nbHexagone();i++){
Hexagone p = H.getById(i);
if (p!=null){
System.out.println(p.get_location().x );
}
int s =p.get_id();
System.out.println(s);
}
} |
Je pige pas ?????
Merci pour vos reponses.