无法解析方法添加ArrayList

时间:2016-01-29 18:45:23

标签: java arrays arraylist

我有这段代码,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

1 个答案:

答案 0 :(得分:5)

我认为你把它弄得一团糟。变化

rubyNecklace.add(inventoryItems);

inventoryItems.add(rubyNecklace);

inventoryItemsArrayList,&amp; rubyNecklace是要添加的项目。