2016-12-15 36 views
3

我試着瞭解泛型如何工作。我創建了幾個實現排序算法和排序之前,我洗牌輸入數組由shuffleArray方法AbstractSorter.java:通用陣列上的隨機算法

public class AbstractSorter<T extends Comparable> { 

public void swap(final T[] input, int srcPos, int dstPos) { 
    if (dstPos != srcPos) { 
     T accum = input[dstPos]; 
     input[dstPos] = input[srcPos]; 
     input[srcPos] = accum; 

    } 
} 

public T[] shuffleArray(final T[] inputArray) { 
    //  E[] arr = (E[])new Object[INITIAL_ARRAY_LENGTH]; 
    T[] result = (T[]) new Comparable[inputArray.length]; 
    System.arraycopy(inputArray, 0, result, 0, inputArray.length); 
    int index; 
    Random random = new Random(); 
    for (int i = result.length - 1; i > 0; i--) { 
     index = random.nextInt(i + 1); 
     if (index != i) { 
      swap(result, i, index); 
     } 
    } 
    return result; 
} 

public boolean more(final T x, final T y) { 
    return (x.compareTo(y) > 0); 
} 

但是當我嘗試使用這樣的:

AbstractSorter<Integer> sortHelper = new AbstractSorter(); 
Integer[] expResult = new Integer[]{-30, -29, -28, -27 }; 
Integer[] shuffleArray = sortHelper.shuffleArray(expResult); 
System.out.println("Array ="+Arrays.toString(shuffleArray)); 

它我得到例外:

java.lang.ClassCastException: 
[Ljava.lang.Comparable; cannot be cast to [Ljava.lang.Integer; 

我不明白我該如何修復洗牌方法。是否有可能在陣列上實現此功能或者我應該遷移到集合?

在此先感謝!

+0

什麼是錯的內置的Shuffle方法爲什麼不使用它? https://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util。列表) – StackFlowed

+0

不應該是'T延伸可比較的'? – maraca

+0

@StackFlowed我使用數組並且不想遷移到列表。但是,如果沒有其他方式,我會。 –

回答

1

你得到異常的原因是因爲你試圖Comparable對象的數組轉換爲Integer對象的數組,這是違法的。你只能從Integer投到Comparable,而不是相反。

這就是你的問題:T[] result = (T[]) new Comparable[inputArray.length];。在這裏,你正在投射ComparableT,在這種情況下是Integer

更改線路T[] result = (T[]) new Comparable[inputArray.length];System.arraycopy(inputArray, 0, result, 0, inputArray.length);T[] result = Arrays.copyOf(inputArray, inputArray.length);所以你shuffleArray方法是這樣的:運行

public T[] shuffleArray(final T[] inputArray) { 
    T[] result = Arrays.copyOf(inputArray, inputArray.length); 
    int index; 
    Random random = new Random(); 
    for (int i = result.length - 1; i > 0; i--) { 
     index = random.nextInt(i + 1); 
     if (index != i) { 
      swap(result, i, index); 
     } 
    } 
    return result; 
} 

的例子有:

AbstractSorter<Integer> sortHelper = new AbstractSorter(); 
Integer[] expResult = new Integer[]{-30, -29, -28, -27}; 
Integer[] shuffleArray = sortHelper.shuffleArray(expResult); 

System.out.println ("Array =" + Arrays.toString(shuffleArray)); 

產生的結果爲:Array =[-30, -28, -29, -27]

1

您正在嘗試創建一個Comparable的數組,但預期的數組是Integer的數組。

即使包含的對象是父項,也不能將某個數組轉換爲其他數組的數組。

但正如你所說,我們不能輕易創建通用對象的數組,所以我refered到How to create a generic array in Java?

替換:

T[] result = (T[]) new Comparable[inputArray.length]; 

有:

final T t = inputArray[0]; 
T[] result = (T[]) Array.newInstance(t.getClass(), inputArray.length); 
+0

對不起,但我根本無法編譯我的類 - 通用數組創建 –

+0

@JohnBl:剛剛編輯答案 – Berger

0

你肯定想要以下內容:

public class AbstractSorter<T extends Comparable<T>> 

T[] result = (T[]) new Comparable<T>[inputArray.length]; 

AbstractSorter<Integer> sortHelper = new AbstractSorter<>();