compareto()を使って名前、身長、体重を「身長昇順」拡張for文でだしたいのですが・・・ (1)returnを使って何を返せばいいのか? (2)System.out.println()でなにをすればいいのか? (3)他の文で間違っているのか? この3点が分かりません。JAVAに詳しい方よろしくお願いします。。。 package class; public class NewClass { public static void main(String[] args) { NewClass2 A = new NewClass2("aaa", 160, 40); NewClass2 B = new NewClass2("bbb", 170, 60); NewClass2 C = new NewClass2("ccc", 150, 70); System.out.println("---Comparable---"); Set ts3 = new TreeSet(); ts3.add(A); ts3.add(B); ts3.add(C); for(Comparable cm : ts3) { System.out.println(?????????????); } package class; public class NewClass2 implements Comparable< NewClass2> { private String name; private int Weight; private int Height; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public int getWeight() { return this.Weight; } public void setWeight(int Weight) { this.Weight = Weight; } public int getHeight() { return this.Height; } public void setHeight(int Height) { this.Height = Height; } public StudentBean(String name, int Height, int Weight) { this.name = name; this.Weight = Weight; this.Height = Height; } @Override public int compareTo(Object arg0) { StudentBean sb = (StudentBean)arg0; String name1 = this.name; int Height1 = this.Height; int Weight1 = this.Weight; int returnval = 0; if(Height1 > sb.getHeight()) { returnval = 1; } else if(Height1 < sb.getHeight()) { returnval = -1; } else returnval = 0; } return ???????; } 文は以上です 出力は aaa 160 40 ・ ・ ・ なかんじでお願いします。
↧