2015-04-01 103 views
0
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,它不起作用。任何意見,將不勝感激。比較器無法工作

+3

你可以發佈'TestComparator'的代碼嗎?另外,試着指定你現在得到的輸出是什麼以及期望的輸出是什麼。 – 2015-04-01 03:17:31

+0

'Test.compareTo()'應該簡單地返回'name.compareTo(newTest.name)'。它當然不應該調用'name.compareTo()'兩次。 – dimo414 2015-04-01 04:29:05

+0

@aaa我剛剛更新了我的答案,包括按名稱排序和按年齡排序,都包含'Test'對象列表。看一看,讓我知道它是否適合你。 – 2015-04-01 05:53:40

回答

0

Collections.sort()List<Integer>一起使用,而不是數組。

Arrays.sort()只適用於int[]如果你想升序。

將陣列更改爲Integer[]類型,並且您應該能夠使用使用Comparator的版本Arrays.sort()

0

以上程序。你只需要使用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)

0

看起來你實際想要做的是以不同的方式對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); 

     } 
    } 
} 
0

一行代碼

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

應該拋出編譯錯誤,因爲Collections類中沒有這樣的排序方法。

改爲使用Arrays.sort(testArray);它將按升序對數組進行排序。

順便說一句,你想通過傳遞一個TestComparator爲一組元素做什麼?