2013-02-28 16 views
0

的排名如果有這樣的代碼(來自https://stackoverflow.com/a/4859279/1178781截取):確定通用可比陣列

import java.util.Comparator; 

public class ArrayIndexComparator<T extends Comparable<? super T>> implements 
     Comparator<Integer> { 
    private final T[] array; 

    public ArrayIndexComparator(T[] array) { 
     this.array = array; 
    } 

    public Integer[] createIndexArray() { 
     Integer[] indexes = new Integer[array.length]; 
     for (int i = 0; i < array.length; i++) { 
      indexes[i] = i; // Autoboxing 
     } 
     return indexes; 
    } 

    @Override 
    public int compare(Integer index1, Integer index2) { 
     // Autounbox from Integer to int to use as array indexes 
     return array[index1].compareTo(array[index2]); 
    } 
} 

當運行該程序發生這種情況:

import java.util.Arrays; 
import java.util.Comparator; 

public class Launcher { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     String[] countries = { "A", "H", "E", "C", "D", "F", "G", "B" }; 
     ArrayIndexComparator<String> comparator = new ArrayIndexComparator<String>(
       countries); 
     Integer[] indexes = comparator.createIndexArray(); 
     Arrays.sort(indexes, comparator); 
     System.out.print("Array: "); 
     System.out.println(Arrays.toString(countries)); 
     System.out.print("Rankings: "); 
     System.out.println(Arrays.toString(indexes)); 
     System.out.println(""); 

     final Float[] scores2 = { 2.3f, 0.7f, 1.4f, 1.2f }; 
     ArrayIndexComparator<Float> comparator2 = new ArrayIndexComparator<Float>(
       scores2); 
     indexes = comparator2.createIndexArray(); 
     Arrays.sort(indexes, comparator2); 
     System.out.print("Array: "); 
     System.out.println(Arrays.toString(scores2)); 
     System.out.print("Rankings: "); 
     System.out.println(Arrays.toString(indexes)); 
     System.out.println(""); 

     final Integer[] scores3 = { 1, 2, 0, 3 }; 
     ArrayIndexComparator<Integer> comparator3 = new ArrayIndexComparator<Integer>(
       scores3); 
     indexes = comparator3.createIndexArray(); 
     Arrays.sort(indexes, comparator3); 
     System.out.print("Array: "); 
     System.out.println(Arrays.toString(scores3)); 
     System.out.print("Rankings: "); 
     System.out.println(Arrays.toString(indexes)); 
     System.out.println(""); 

     final Integer[] scores4 = { 1, 0, 2, 3 }; 
     ArrayIndexComparator<Integer> comparator4 = new ArrayIndexComparator<Integer>(
       scores4); 
     indexes = comparator4.createIndexArray(); 
     Arrays.sort(indexes, comparator4); 
     System.out.print("Array: "); 
     System.out.println(Arrays.toString(scores4)); 
     System.out.print("Rankings: "); 
     System.out.println(Arrays.toString(indexes)); 
     System.out.println(""); 

    } 

} 

輸出如下:

Array: [A, H, E, C, D, F, G, B] 
Rankings: [0, 7, 3, 4, 2, 5, 6, 1] 

Array: [2.3, 0.7, 1.4, 1.2] 
Rankings: [1, 3, 2, 0] 

Array: [1, 2, 0, 3] 
Rankings: [2, 0, 1, 3] 

Array: [1, 0, 2, 3] 
Rankings: [1, 0, 2, 3] 

我認爲這應該正確地給陣列中的對象的排名,但是cer tain配置不這樣做。我錯過了什麼嗎?或者,這不是實際排序給予排名,而是別的?

例如:

Array: [A, H, E, C, D, F, G, B] 
Rankings: [0, 7, 3, 4, 2, 5, 6, 1] 
Should be:[0, 7, 4, 2, 3, 5, 6, 1] 

Array: [2.3, 0.7, 1.4, 1.2] 
Rankings: [1, 3, 2, 0] 
Should be:[3, 0, 2, 1] 

Array: [1, 2, 0, 3] 
Rankings: [2, 0, 1, 3] 
Should be:[1, 2, 0, 3]  

Array: [1, 0, 2, 3] 
Rankings: [1, 0, 2, 3] 
Should be:[1, 0, 2, 3] 
+0

您的陣列是否有重複? – nattyddubbs 2013-02-28 21:15:35

+0

不,它不需要考慮重複 – justderb 2013-02-28 23:13:56

回答

0

基於這樣的事實,你不需要考慮你可以改變周圍的一切複製到這個樣子:

public static void main(String[] args) { 
    String[] countries = { "A", "H", "E", "C", "D", "F", "G", "B" }; 
    ArrayComparator<String> comparator = new ArrayComparator<String>(
      countries); 
    Integer[] indexes = comparator.createIndexArray(); 
    System.out.print("Array:  "); 
    System.out.println(Arrays.toString(countries)); 
    System.out.print("Rankings: "); 
    System.out.println(Arrays.toString(indexes)); 
    System.out.println(""); 

    final Float[] scores2 = { 2.3f, 0.7f, 1.4f, 1.2f }; 
    ArrayComparator<Float> comparator2 = new ArrayComparator<Float>(
      scores2); 
    indexes = comparator2.createIndexArray(); 
    System.out.print("Array:  "); 
    System.out.println(Arrays.toString(scores2)); 
    System.out.print("Rankings: "); 
    System.out.println(Arrays.toString(indexes)); 
    System.out.println(""); 

    final Integer[] scores3 = { 1, 2, 0, 3 }; 
    ArrayComparator<Integer> comparator3 = new ArrayComparator<Integer>(
      scores3); 
    indexes = comparator3.createIndexArray(); 
    System.out.print("Array:  "); 
    System.out.println(Arrays.toString(scores3)); 
    System.out.print("Rankings: "); 
    System.out.println(Arrays.toString(indexes)); 
    System.out.println(""); 

    final Integer[] scores4 = { 1, 0, 2, 3 }; 
    ArrayComparator<Integer> comparator4 = new ArrayComparator<Integer>(
      scores4); 
    indexes = comparator4.createIndexArray(); 
    System.out.print("Array:  "); 
    System.out.println(Arrays.toString(scores4)); 
    System.out.print("Rankings: "); 
    System.out.println(Arrays.toString(indexes)); 
    System.out.println(""); 

} 

public class ArrayComparator<T extends Comparable<? super T>>{ 
    private final T[] array; 
    private final SortedMap<T, Integer> sortedArray; 

    public ArrayComparator(T[] array){ 
     this.array = array; 
     sortedArray = new TreeMap<T, Integer>(); 
     for(int i = 0 ; i < array.length ; i ++){ 
      sortedArray.put(array[i], Integer.valueOf(i)); 
     } 
    } 

    public Integer[] createIndexArray(){ 
     Integer[] indexArray = new Integer[sortedArray.size()]; 
     int i = 0; 
     for(T key : sortedArray.keySet()){ 
      indexArray[sortedArray.get(key)] = i; 
      i++; 
     } 
     return indexArray; 
    } 
} 

然後運行它,你的輸出如下所示:

Array:  [A, H, E, C, D, F, G, B] 
Rankings: [0, 7, 4, 2, 3, 5, 6, 1] 

Array:  [2.3, 0.7, 1.4, 1.2] 
Rankings: [3, 0, 2, 1] 

Array:  [1, 2, 0, 3] 
Rankings: [1, 2, 0, 3] 

Array:  [1, 0, 2, 3] 
Rankings: [1, 0, 2, 3] 

這是我相信你在找的東西。

+0

你給出的輸出只是對'分數'進行排序,它不會給我每個'分數'的排名。它已經對'score'進行了「排序」,但在「int」數組中也是如此。 – justderb 2013-02-28 20:14:23

+0

所以你期待排序的'索引'數組直接映射到'scores4'數組中相同索引的項目的「排名」?如果是這樣,我不知道這是它的目的。原帖並未引用任何形式的「排名」。 – nattyddubbs 2013-02-28 20:35:00

+0

我在底部更新了我的問題。希望這應該有助於看到我想要做什麼... – justderb 2013-02-28 20:47:48