2010-09-08 120 views
2

我創建了一個TreeMap像這樣:java對象的層次結構,並通過對象的功能

TreeMap<Integer, ArrayList<MyClass>> wrap = new TreeMap<Integer, ArrayList<MyClass>>(); 

我創建了一個構造函數,像這樣:

public foo (TreeMap<Integer, Collection<Spot> > objects) { 
    this.field = objects; 
} 

然而,日食給了我一個紅色squigly當我使用的構造,與我wrap變量作爲一個參數:

The constructor foo(TreeMap<Integer,ArrayList<Spot>>) is undefined 

一個ArrayList是TY收集...是的?那麼,爲什麼這不起作用?

回答

2

泛型不像你認爲的那樣工作在這種情況下。

你需要的是類似於:

public foo (TreeMap<Integer, ? extends Collection<Spot> > objects) { 
    this.field = objects; 
} 

?被稱爲外卡。它可以讓你通過Collection,或任何延伸/實現Collection

? extends Collection<Spot>倒像是:擴展集合

東西。

相關問題