1

我在学习 Java 方面还是个新手,我需要一种方法来将每个从某个类构造的对象放入我以后可以访问的 ArrayList 中。

这是我的Item课:

import java.util.ArrayList;
import java.util.*;
public class Item
{
    private String name;
    private int quantity;
    private ArrayList<Item> allItems = new ArrayList<Item>(); //the ArrayList I'm trying to fill with every Item object

    /**
     * Constructs an item of a given name and quantity
     * @param name Item name
     * @param quantity How much of an item the player has
     */
    public Item(String name, int quantity)
    {
        this.name = name;
        this.quantity = quantity;
    }

    /**
     * @return Item name
     */
    public String getItemName()
    {
        return name;
    }

    /**
     * @return Quantity of the item
     */
    public int getQuantity()
    {
        return quantity;
    }

    /**
     * @return A list of items that have a quantity > 0
     */
    public ArrayList<Item> getInventory()
    {
        ArrayList<Item> inventory = new ArrayList<Item>();
        for(Item i : allItems)
        {
            if(i.getQuantity() > 0)
                inventory.add(i);
        }
        return inventory;
    }
}

如何在每次构造对象时添加一个Item对象?allItems

4

4 回答 4

4

首先,arraylis 必须是静态的,以便在所有实例之间共享。否则,每个实例都会有不同的变量。

有关实例/类成员的更多信息在这里

private String name;
private int quantity;
private static ArrayList<Item> allItems = new ArrayList<Item>();

然后,您可以在构造函数中添加创建的实例,将其称为“this”。

关于“this”关键字

public Item(String name, int quantity)
{
    this.name = name;
    this.quantity = quantity;
    allItems.add(this);
}
于 2013-04-20T10:15:02.157 回答
1

您当然希望所有项目都有一个列表,而不是每个项目都随身携带并维护自己的所有项目的副本列表。当这是您的意图时,您应该将列表定义为static

private static ArrayList<Item> allItems = new ArrayList<Item>()

静态变量由类的所有实例共享。

在 Item 的构造函数中,只需添加this到所有项目的列表中。

allItems.add(this);
于 2013-04-20T10:14:59.633 回答
0

一种可能的解决方案是将项目列表声明为静态,如下所示:

public static List<Item> allItems = new ArrayList<Item>();

之后,您可以使用以下代码段访问它:

Item.allItems // example would be System.out.println(Item.allItems)

此外,在您的构造函数中,您将需要以下代码:

public Item(String name, int quantity) {
    this.name = name;
    this.quantity = quantity;
    this.allItems.add(this);
}

但是请谨慎使用这种方法,因为它会将创建的每个项目添加到列表中,这可能导致内存泄漏。

于 2013-04-20T10:14:57.040 回答
0

您需要一个静态列表,然后您可以使用类构造函数将此对象添加到列表中。

import java.util.ArrayList;
import java.util.*;
public class Item
{
    private String name;
    private int quantity;
    private static ArrayList<Item> allItems = new ArrayList<Item>(); //the ArrayList I'm trying to fill with every Item object

    /**
     * Constructs an item of a given name and quantity
     * @param name Item name
     * @param quantity How much of an item the player has
     */
    public Item(String name, int quantity)
    {
        this.name = name;
        this.quantity = quantity;
        allItems.add(this);
    }

    /**
     * @return Item name
     */
    public String getItemName()
    {
        return name;
    }

    /**
     * @return Quantity of the item
     */
    public int getQuantity()
    {
        return quantity;
    }

    /**
     * @return A list of items that have a quantity > 0
     */
    public static ArrayList<Item> getInventory()
    {
        ArrayList<Item> inventory = new ArrayList<Item>();
        for(Item i : allItems)
        {
            if(i.getQuantity() > 0)
                inventory.add(i);
        }
        return inventory;
    }
}
于 2013-04-20T10:15:36.077 回答