看起来你实际想要做的是以不同的方式对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