简体   繁体   English

Java-ArrayList返回空

[英]Java - ArrayList is returning Empty

I am creating an array list in a class called Inventory. 我在一个名为Inventory的类中创建一个数组列表。 Here there are methods to add to, delete and get the list. 这里有添加,删除和获取列表的方法。 I create a new array list in a new class called combat and set this array list to the one in the Inventory class with a get method. 我在一个名为warbat的新类中创建了一个新的数组列表,并使用get方法将此数组列表设置为Inventory类中的数组列表。

This is the inventory class part: 这是库存类的一部分:

private ArrayList<String> inventory;

public Inventory() {
    this.inventory = new ArrayList<>();
}

public void addToInventory(String item) {
    inventory.add(item);
    System.out.println("A " + item + " has been added to your inventory");
    printInventory();
}

public void removeFromInventory(String item) {
    inventory.remove(item);
}

public ArrayList<String> getInventory() {
    return inventory;
}

This is in the Combat Class: 这是在战斗舱中:

private Inventory inventory = new Inventory();
private ArrayList<String> playerInventory = inventory.getInventory();

public Combat() {

}

@Override
public void setPlayerHit() {
    System.out.println(playerInventory);

When I call the print playerInventory line, the arrayList returns []. 当我调用print playerInventory行时,arrayList返回[]。 Yet in the inventory class it has something in as I add an item to it shown in my launcher class: 但是,在清单类中有一些东西,因为我向其中添加了一个在启动器类中显示的项目:

playerInventory.addToInventory(item.getItem(9));

I can get this working using public fields but wanted to make use of encapsulation and use getters and setters. 我可以使用公共字段来完成此工作,但希望利用封装并使用getter和setter。 Could I possibly get some pointers please? 我可以得到一些指示吗?

Just guessing here, but it sounds like you are using separate Inventory objects in different parts of the code. 只是在这里猜测,但这听起来像您在代码的不同部分中使用了单独的Inventory对象。 They won't share the same internal inventory array list and items added to one Inventory object won't appear in the other. 它们将不会共享相同的内部inventory阵列列表,并且添加到一个“ Inventory对象中的项目将不会出现在另一个对象中。 Make sure that the Inventory object you are printing is the same one to which you have added items. 确保要打印的Inventory对象与添加项目的对象相同。

If you want a single global Inventory object, you can use a singleton: 如果要单个全局Inventory对象,则可以使用单个对象:

public class Inventory {
    private static Inventory instance = new Inventory();
    private ArrayList<String> inventory;

    public static Inventory getInstance() {
        return instance;
    }

    // private constructor to prevent extra copies of the inventory
    private Inventory() {
        inventory = new ArrayList<>();
    }

    ...
}

When I call the print playerInventory line, the arrayList returns [] 当我调用print playerInventory行时,arrayList返回[]

you are not inserting any string in the Inventory... thus the empty list... 您没有在清单中插入任何字符串...因此为空列表...

by the way, this is redundant: 顺便说一句,这是多余的:

private ArrayList<String> playerInventory = inventory.getInventory();

since you are using by composition an inventory object wich has the ArrayList 由于按组成使用库存清单对象具有ArrayList

try again but before doing 再试一次,然后再做

public Combat() {
    inventory.addToInventory("f1");
    inventory.addToInventory("f2");
}

now your inventory object has 2 elements 现在您的库存对象有2个元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM