我目前正在制作店面模拟器,并且我将嵌套的arraylists(ArrayList<ArrayList<Item>>
)用作商店库存的数据结构,其中每个内部Arraylist都包含Item类型的对象,这是我的虚拟类使得商店的产品对象继承自
我正在尝试编写代码以验证要添加新产品以避免错误的较大产品列表中是否存在内部Arraylist,但是我在内部arraylist中使用ArrayList的contains()方法遇到麻烦。 / p>
我认为即时通讯无法正确解释自己,所以我会放下代码并希望它更有意义
public class Stock {
private ArrayList<ArrayList<Item>> items;
...
public void addItem(Item item){
verifyData(item);
/* code to add an item to the ArrayList */
}
...
/**
* Verifies that the specific item has an arrayList for its kind
* inside the items arraylist. If not, then it will create one
* @param item the item to be put into the arraylist
*/
private void verifyData(Item item){
if (items.contains(new ArrayList<item.getClass>))
return;
// If the Map contains the required arrayList, then it shouldn't
// make it to this point
items.add(new ArrayList<item.getClass()>());
}
...
此代码不起作用,但我希望它能传达我想要的东西。具体来说,item.getClass()
在添加新的ArrayList时不起作用,所以我的问题是为什么不起作用,以及如何实现(如果可以实现)。