微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何判断Arraylist是否包含Java中特定对象的arraylist

如何解决如何判断Arraylist是否包含Java中特定对象的arraylist

我目前正在制作店面模拟器,并且我将嵌套的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时不起作用,所以我的问题是为什么不起作用,以及如何实现(如果可以实现)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。