我有这段代码,add
标记为红色并显示错误:
"Cannot resolve method 'add(java.util.ArrayList<com.example.InventoryItems>)'"
public class Player {
private String name;
private int lives;
private int score;
private int level;
private Weapon weapon;
private ArrayList<InventoryItems> inventoryItems;
public Player(String name) {
this.name = name;
this.lives = 3;
this.score = 0;
this.level = 1;
inventoryItems = new ArrayList<InventoryItems>();
setDefaultWeapon();
}
public void setDefaultWeapon() {
this.weapon = new Weapon("Dagger", 3, WeaponType.Wooden);
}
public void setDefaultInventoryItems() {
InventoryItems rubyNecklace = new InventoryItems("Ruby Necklace", ItemType.Amulet);
rubyNecklace.add(inventoryItems);
}
这是InventoryItems.java
package com.example;
import java.util.ArrayList;
/**
* Created by Pawel on 1/21/16.
*/
enum ItemType { Armor, Ring, Amulet, Junk, Weapon }
public class InventoryItems {
private String name;
private ItemType itemtype;
public InventoryItems(String name, com.example.ItemType itemtype) {
super();
this.name = name;
this.itemtype = itemtype;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public com.example.ItemType getItemtype() {
return itemtype;
}
public void setItemtype(com.example.ItemType itemtype) {
this.itemtype = itemtype;
}
}
我应该将add
方法添加到InventoryItems吗?
为什么不能将rubyNecklace
添加到ArrayList
?
答案 0 :(得分:5)
我认为你把它弄得一团糟。变化
rubyNecklace.add(inventoryItems);
到
inventoryItems.add(rubyNecklace);
inventoryItems
是ArrayList
,&amp; rubyNecklace
是要添加的项目。