2012-12-06 92 views
1

如何創建通用清單列表?我有一個Boxcar類,它需要一個通用參數和一個應該創建Boxcars列表的Train類。我們應該在一個單獨的主類中指定將在Boxcar中使用的類型,因此在此之前boxcar必須保持通用。以下是我寫的代碼。它編譯,但在一個單獨的驅動程序類調用加載方法我得到的錯誤The method load(capture#1-of ?) in the type Boxcar<capture#1-of ?> is not applicable for the arguments (Person)創建通用列表清單

package proj5; 

import java.util.ArrayList; 
import java.util.List; 

public class Train { 

private List<Boxcar<?>> train; 
private int maxSpeed; 
private int minSpeed; 
private String position; 
private int numBoxcars; 
private int maxNumBoxcars; 
private int speed; 
private String destination; 
private boolean stopped = true; 

public Train(int maxSpeed, int minSpeed, int maxNumBoxcars, String position){ 
    train = new ArrayList<Boxcar<?>>(); 
    this.maxSpeed = maxSpeed; 
    this.minSpeed = minSpeed; 
    this.maxNumBoxcars = maxNumBoxcars; 
    this.position = position; 
} 

public int getMaxNumBoxcars(){ 
    return maxNumBoxcars; 
} 

public int getSpeed(){ 
    return speed; 
} 

public String getPosition(){ 
    return position; 
} 

public int getMaxSpeed(){ 
    return maxSpeed; 
} 

public int getNumBoxcars(){ 
    return numBoxcars; 
} 

public List<Boxcar<?>> getTrain(){ 
    return train; 
} 

public void depart(String destination){ 
    this.destination = destination; 
    speed = minSpeed; 
    stopped = false; 
} 

public void arrive(){ 
    stopped = true; 
    position = destination; 
} 

public void addCar(Boxcar<?> boxcar, int i){ 
    if(stopped){ 
     boxcar.setMaxItems(i); 
     train.add(boxcar); 
    } 
} 

public void removeCar(int i){ 
    if(stopped){ 
     train.remove(i); 
    } 
} 

}

package proj5; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Collections; 

public class Boxcar<T extends Comparable<T>> { 

private List<T> boxcar; 
private int maxItems; 

public Boxcar(){ 
    boxcar = new ArrayList<T>(); 
} 

public void load(T thing){ 
    if(!boxcar.contains(thing) && boxcar.size() < maxItems){ 
     boxcar.add(thing); 
     Collections.sort(boxcar); 
    }else{ 

    } 
} 

public int getMaxItems(){ 
    return maxItems; 
} 

public void setMaxItems(int i){ 
    maxItems = i; 
} 

public void unload(T thing){ 
    boxcar.remove(thing); 
} 

public List<T> getBoxcar(){ 
    return boxcar; 
} 

}

我希望這更好的時候傳達什麼,我試圖完成

+4

目前還不清楚你試圖達到什麼目標,但上面的代碼將定義不能編譯。 – amit

+1

我認爲你應該定義使用列表的類,而不是列表。 – Bohemian

回答

1

創建通用列表時,您需要提供類型來代替T?。例如包含字符串棚車的列表如下所示:

List<Boxcar<String>> train = new ArrayList<Boxcar<String>>(); 

?wildcard的一個例子,而一個T代表被的List源內引用的類型。如果沒有對泛型的深入理解,這一點可能會非常棘手,但我想確保完整性。請查看this page以獲取有關如何在代碼中使用泛型的更多信息。

看着你修改的問題,我會引導您進入這行代碼:

public class Boxcar<T extends Comparable<T>> { 

,然後它下面這一行:

private List<T> boxcar; 

這意味着你傳遞給new Boxcar<type>()任何類型將轉入內部列表(以及其他期望類型爲T的對象的方法)。

+0

Alrighty感謝。那麼我將如何創建Boxcars列表?用通配符?如果我試圖做其他任何事,它會告訴我參數化訓練,我不認爲訓練課應該採用一般的參數 – somtingwong

+0

您可以將泛型添加到'Train'類,改變'public class Train {'to'public class火車 {'然後將所有出現的'Boxcar '改變爲'Boxcar '。當你實例化一個類型爲'Train'的新對象時,你需要傳遞一個類似'Train t = new Train ();' –

4

棚車這是一個通用類:

具有棚車列表
class BoxCar<T>{ 


} 

列車等級:

class Train { 
List<BoxCar<PassTheTypeHere>> = new ArrayList<BoxCar<PassTheTypeHere>>(); 

} 
+1

我發現你的答案是更通用的:) +1 –

+1

不需要成爲'new ArrayList >()'? – Dunes

+0

@dunes謝謝:)編輯它 – PermGenError

0

根據您的原始問題的措辭聽起來像你想創建一個boxcars列表。

以下是您需要做的全部事情。

private List<Boxcar> boxcarList = new ArrayList<Boxcar>(); 
+0

忽略我,我回答你原來的,不太詳細的問題。有了你的代碼,現在對我來說很明顯,其他兩張海報正在回答你試圖提出的問題。 – Hardy