比较器无法工作
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;
}
public int compareTo(Test newTest){
if (this.name.compareTo(newTest.name) > 0){
return 1;
} else if(this.name.compareTo(newTest.name) < 0){
return -1;
} else{
return 0;
}
}
public static void main(String[] args){
Test Albert = new Test("Albert", 19, 100);
Test James = new Test("James", 16, 50);
if (Albert.compareTo(James) == -1){
System.out.println("Albert Comes first");
} else if (Albert.compareTo(James) == 1){
System.out.println("James Comes first");
} else{
System.out.println("It's a tie");
}
int[] testArray = new int[2];
testArray[0] = Albert.age;
testArray[1] = James.age;
Collections.sort(testArray, new TestComparator());
}
}
我创建了一个名为TestComparator的比较器,由于某种原因它不适用于Collections.sort。我不知道为什么它不工作。有任何想法吗?我也尝试了Array.sort,它不起作用。任何意见,将不胜感激。比较器无法工作
Collections.sort()
与List<Integer>
一起使用,而不是数组。
Arrays.sort()
只适用于int[]
如果你想升序。
将阵列更改为Integer[]
类型,并且您应该能够使用使用Comparator
的版本Arrays.sort()
。
以上程序。你只需要使用Arrays.sort(testArray);
public static void main(String[] args){
Test Albert = new Test("Albert", 19, 100);
Test James = new Test("James", 16, 50);
if (Albert.compareTo(James) == -1){
System.out.println("Albert Comes first");
} else if (Albert.compareTo(James) == 1){
System.out.println("James Comes first");
} else{
System.out.println("It's a tie");
}
int[] testArray = new int[2];
testArray[0] = Albert.age;
testArray[1] = James.age;
Arrays.sort(testArray); //You are just using int array so no need of Collections.sort
for(int temp: testArray){
System.out.println("Age :: " + temp);
}
//Collections.sort(testArray, new);
}
}
如果你想对对象进行排序,那么你必须使用相当的接口,这将根据您所覆盖compareTo方法内部实现什么样的。
你可以从下面的链接有更好的想法可比较和comparaor。 Java Object Sorting Example (Comparable And Comparator)
看起来你实际想要做的是以不同的方式对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);
}
}
}
一行代码
Collections.sort(testArray, new TestComparator());
应该抛出编译错误,因为Collections类中没有这样的排序方法。
改为使用Arrays.sort(testArray)
;它将按升序对数组进行排序。
顺便说一句,你想通过传递一个TestComparator为一组元素做什么?
你可以发布'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