看起來你實際想要做的是以不同的方式對Test
對象進行排序。
我剛剛得到這個代碼用三個例子的工作:使用TestComparator
類按年齡排序,排序是由工資內嵌Comparator
,並通過默認Comparable
進行排序的名字。
下面是使用TestComparator
類按年齡排序的示例:
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
public class Test implements Comparable <Test>{
String name;
int age;
int salary;
public Test(String name, int age, int salary){
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public int compareTo(Test newTest){
return (this.name.compareTo(newTest.name));
}
public static void main(String[] args){
List<Test> testList = new ArrayList<Test>();
testList.add(new Test("Albert", 19, 100));
testList.add(new Test("James", 16, 50));
testList.add(new Test("Brian", 15, 50));
Collections.sort(testList, new TestComparator());
for (int i = 0; i < testList.size(); i++){
System.out.println(testList.get(i).name);
}
}
}
public class TestComparator implements Comparator<Test>{
@Override
public int compare(Test t1, Test t2) {
return t1.age-t2.age;
}
}
這裏是由薪金排序直列Comparator
:
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
public class Test implements Comparable <Test>{
String name;
int age;
int salary;
public Test(String name, int age, int salary){
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public int compareTo(Test newTest){
return (this.name.compareTo(newTest.name));
}
public static void main(String[] args){
List<Test> testList = new ArrayList<Test>();
testList.add(new Test("Albert", 19, 100));
testList.add(new Test("James", 16, 50));
testList.add(new Test("Brian", 15, 60));
Collections.sort(testList, new Comparator<Test>() {
@Override
public int compare(Test t1, Test t2) {
return t1.salary-t2.salary;
}
});
for (int i = 0; i < testList.size(); i++){
System.out.println(testList.get(i).name);
}
}
}
這裏是默認compareTo()
方法的例子按名稱排序:
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class Test implements Comparable <Test>{
String name;
int age;
int salary;
public Test(String name, int age, int salary){
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public int compareTo(Test newTest){
return (this.name.compareTo(newTest.name));
}
public static void main(String[] args){
Test Albert = new Test("Albert", 19, 100);
Test James = new Test("James", 16, 50);
if (Albert.compareTo(James) < 0){
System.out.println("Albert Comes first");
} else if (Albert.compareTo(James) > 0){
System.out.println("James Comes first");
} else{
System.out.println("It's a tie");
}
List<Test> testList = new ArrayList<Test>();
testList.add(James);
testList.add(Albert);
Collections.sort(testList);
for (int i = 0; i < testList.size(); i++){
System.out.println(testList.get(i).name);
}
}
}
你可以發佈'TestComparator'的代碼嗎?另外,試着指定你現在得到的輸出是什麼以及期望的輸出是什麼。 – 2015-04-01 03:17:31
'Test.compareTo()'應該簡單地返回'name.compareTo(newTest.name)'。它當然不應該調用'name.compareTo()'兩次。 – dimo414 2015-04-01 04:29:05
@aaa我剛剛更新了我的答案,包括按名稱排序和按年齡排序,都包含'Test'對象列表。看一看,讓我知道它是否適合你。 – 2015-04-01 05:53:40