【问题标题】:Comparator Won't Work比较器不工作
【发布时间】:2015-04-01 03:06:30
【问题描述】:
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 ,但它不起作用。任何建议将不胜感激。

【问题讨论】:

  • 你能把TestComparator的代码贴出来吗?另外,请尝试指定您现在获得的输出是什么以及预期的输出是什么。
  • Test.compareTo() 应该简单地返回name.compareTo(newTest.name)。它当然不应该两次调用name.compareTo()
  • @aaa 我刚刚更新了我的答案,包括按名称排序和按年龄排序,两者都包含Test 对象列表。看看,让我知道它是否适合你。

标签: java arrays collections comparator


【解决方案1】:

Collections.sort()List&lt;Integer&gt; 一起使用,而不是数组。

Arrays.sort() 仅适用于int[],如果您想要升序。

将数组更改为Integer[] 类型,您应该能够使用使用ComparatorArrays.sort() 版本。

【讨论】:

    【解决方案2】:

    对于上述程序。你只需要使用 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 );
    }
    }
    

    如果你想对对象进行排序,那么你必须使用可比较的接口,它将根据你在 override compareTo 方法中实现的内容进行排序。

    您可以从下面的链接中更好地了解可比较和比较。 Java Object Sorting Example (Comparable And Comparator)

    【讨论】:

      【解决方案3】:

      代码行

      Collections.sort(testArray, new TestComparator()); 
      

      应该会抛出编译错误,因为 Collections 类中没有这样的排序方法。

      请改用Arrays.sort(testArray);它将按升序对数组进行排序。

      顺便说一句,你想通过传递一个 TestComparator 为一组 premitives 做什么?

      【讨论】:

        猜你喜欢
        • 2014-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-07
        • 2014-06-25
        • 1970-01-01
        相关资源
        最近更新 更多