4種類の専用オブジェクトArrayList<String>
を持つ「items
」というクラスを作成しようとしていますが、アイテムの種類に応じてArrayListsにアイテムを追加するメソッドがあります。リストに項目を追加する方法は、次のコードを持っているArrayList内の新しいオブジェクトのパラメータをチェックする方法は?
private ArrayList<String> itemlistweapons = new ArrayList<String>();
private ArrayList<String> itemlistapparel = new ArrayList<String>();
private ArrayList<String> itemlistaid = new ArrayList<String>();
private ArrayList<String> itemlistmisc = new ArrayList<String>();
:4つのArrayListがあり
public void additem(String name, String type){
itemlistweapons.add(new Item(name, type).toString());
}
それは追加項目オブジェクトは、名前を取るコンストラクタとItem
と呼ばれる別のクラスから来ていますアイテムの種類と種類。
だから、私が知りたいのですが何を、私は、これは言うことができる方法です。
public void additem(String name, String type){
if //the item added has the type "weapon"
itemlistweapons.add(new Item(name, type).toString());
else if //the item added has the type "apparel"
itemlistapparel.add(new Item(name, type).toString());
else if //the item added has the type "aid"
itemlistaid.add(new Item(name, type).toString());
else if //the item added has the type "misc"
itemlistmisc.add(new Item(name, type).toString());
私はそれらのコメントの代わりに何を入れますか?
'場合(タイプ== "武器")' ... – theblindprophet
場合( "武器" .equals(タイプ)) –