lasnoufle La seule et unique! | Ben heu "le probleme subsiste", il faut que tu sois plus precis. Comme dit plus haut, ton code marche tel quel sous Eclipse:
public class Test implements Comparable<Test > { private Float number; private Integer weight; private String category; public Test(float number, int weight, String category) { this.number= number; this.weight = weight; this.category = category; } public int compareTo(Test compareTest) { if(this.number!= compareTest.number) { return this.number.compareTo(compareTest.number); } else if(this.weight!= compareTest.weight) { return this.weight.compareTo(compareTest.weight); } else if(this.category != compareTest.category) { return this.category.compareTo(compareTest.category); } else { return 0; } } public static Comparator<Test> comparator = new Comparator<Test>() { public int compare(Test test1, Test test2) { return test1.compareTo(test2); } }; public static void main(String[] args) { TreeSet<Test> tree= new TreeSet<Test>(Test.comparator); Test t1 = new Test(2f, 12,"1" ); Test t2 = new Test(3f, 11, "2" ); Test t3= new Test(4f, 10, "3" ); tree.add(t1); tree.add(t2); tree.add(t3); System.out.println(tree.size()); } } |
Le System.out.println m'affiche bien 3, et si je debugge dessus, je vois bien le TreeSet en memoire avec 3 elements tries (3f sur la racine, 2f a gauche de la racine, 4f a droite de la racine). Edit: et si tu affiches le contenu de l'arbre:
public static void main(String[] args) { TreeSet<Test> tree= new TreeSet<Test>(Test.comparator); Test t1 = new Test(2f, 12,"1" ); Test t2 = new Test(3f, 11, "2" ); Test t3= new Test(4f, 10, "3" ); tree.add(t1); tree.add(t2); tree.add(t3); for (Iterator<Test> it = tree.iterator(); it.hasNext();) { Test t = it.next(); System.out.println(t.number); } } |
Message édité par lasnoufle le 01-09-2014 à 18:52:16 ---------------
C'était vraiment très intéressant.
|