2016-12-09 11 views
1

我有點困惑:我想ArrayList的-S的數組,但我看不出有什麼方法來指定的ArrayList項目類型:如何爲新的ArrayList指定項目類型<V> [N]?

class Test<V> { 
    final static int N = 512; 
    ArrayList<V> list1 = new ArrayList<>(); // ok 
    ArrayList<V> list2 = new ArrayList<V>(); // ok 

    ArrayList<V>[] lists1 = new ArrayList[N]; // <== warning: 
    // Type safety: The expression of type ArrayList[] needs 
    // unchecked conversion to conform to ArrayList<V>[] 
    // Note that this line works because it's only a warning. 

    ArrayList<V>[] lists2 = new ArrayList<V>[N]; // <== error: 
    // Cannot create a generic array of ArrayList<V> 

    ArrayList<V>[] lists3 = new ArrayList<>[N]; // <== error: 
    // Incorrect number of arguments for type ArrayList<E>; 
    // it cannot be parameterized with arguments <> 

    ArrayList<V>[] lists4 = (ArrayList<V>[])new ArrayList[N]; // <== warning: 
    //Type safety: Unchecked cast from ArrayList[] to ArrayList<V>[] 

} 

我看到有

ArrayList<V>[] lists1 = new ArrayList[N]; 

的問題是,物品的類型未指定,但我該如何指定它?

編輯:答案是參數化類型的they disallowed生陣列。他們可能希望程序員明確地對類型安全負責。

void f() { 
     lists1[0] = new ArrayList<V>(); // ok 

     //lists1[1] = new ArrayList<String>(); // <== error: 
     // Type mismatch: cannot convert from ArrayList<String> to ArrayList<V> 

     Object[] x = lists1; 
     x[0] = new ArrayList<Integer>(); // ok 
    } 

他們可能會認爲,在一些勉強相關的地方警告總比沒有警告要好:或者是因爲Test.f()編譯沒有任何警告,也許他們感到不安。

+5

http://stackoverflow.com/questions/8559092/create-an-array-of-arraylists – Jerry06

+4

http://stackoverflow.com/questions/2927391/whats-the-reason-i-cant-create-generic -array-types-in-java – nullpointer

+0

http://stackoverflow.com/questions/1817524/generic-arrays-in-java –

回答

-1

您將在實例化Test類時指定它。雖然,這樣的一種結構應該放在一個特定的類,所以你的泛型參數將通過新類來處理,並在測試中使用它時

編輯你不會有任何麻煩: 這就是爲什麼我腦子裏想的是像

public class ArrayOfList<T> { 

    private ArrayList<T>[] arrayLists; 

    public ArrayList<T>[] getArrayLists() { 
     return arrayLists; 
    } 


    public void setArrayLists(ArrayList<T>[] arrayLists) { 
     this.arrayLists = arrayLists; 
    } 

} 

public class Test { 

    public void testArrayLists(){ 
     ArrayOfList<String> lists = new ArrayOfList<>(); 
     // lists contains a usable ArrayList<String>[] 
    } 
} 
+0

我真的不喜歡的是當他們downvote沒有解釋。 '測試'將把這條線轉換爲'ArrayList [] lists1 = new ArrayList [N];'並且在'new ArrayList [N]'中,項目類型仍未被指定。 – 18446744073709551615

+0

查看上面的編輯... – DamCx

0

這樣做沒有警告的唯一方法是根本的ArrayList秒的ArrayList

ArrayList<ArrayList<V>> list_of_lists = new ArrayList<>(); 
ArrayList<V> list = new ArrayList<>(); 
list_of_lists.add(list); 
+0

請解釋你的downvotes ... – Michael

+0

downvote不是我的(我太意味着我的分數在downvotes;),但我可以解釋:在我的代碼列表中,列表位於性能觀點最重要的循環內。數組不能增長,但速度更快,BTW空指針比空ArrayLists更快。 – 18446744073709551615

+0

在這種情況下,如果您確定'ArrayList'的'ArrayList'會對性能產生嚴重影響(我很懷疑,但我願意接受這種可能性),我會使用您的'lists1'版本,但是會抑制警告。 「確定」是指通過分析獲得的經驗證據。 – Michael

0

療法e是數組和泛型之間的一些根本區別。他們混合不好。

數組是通用,這意味着它們在運行時強制執行它的約束。另一方面,泛型只有在編譯時才強制執行其約束。他們在運行時擦除類型信息。

由於這些差異,他們通常不會很好地混合。例如,下面的表達式是無效的:

ArrayList<V>[] lists2 = new ArrayList<V>[N]; 

ArrayList<V>[] lists3 = new ArrayList<>[N]; 

ArrayList<V>[] lists3 = new ArrayList<String>[N]; 

這枚:

ArrayList<V>[] lists1 = new ArrayList[N]; 

是合法的,但不是類型安全的。這意味着在運行時,它很可能會因爲ClassCastException而失敗。

+0

這些細節已經在問題中說明,任何解釋將不勝感激 – nullpointer

0

按照甲骨文documentation

「不能創建參數化類型的數組」

相反,你可以這樣做:

List<List<ItemType>> var = new ArrayList<List<ItemType>>(size); 
+0

可以重定向OP到http://stackoverflow.com/a/8559102/1746118 – nullpointer

+1

nah複製更好(諷刺!) – Sikorski

+0

@Sikorski只要upvote,如果你同意,不需要snark。 – Michael

0

只要你不能做它。您可以創建一個列表或一組列表的列表,但不能創建一個列表的列表。

相關問題