简体   繁体   English

自动将构造的每个Class对象添加到ArrayList中

[英]Automatically adding every Class object constructed into an ArrayList

I'm still fairly new in learning Java and I'm in need of a way to put every object that is constructed from a certain class into an ArrayList that I can access later. 我在学习Java方面还很陌生,我需要一种方法来将从某个类构造的每个对象放入一个ArrayList中,以便以后使用。

Here is my Item class: 这是我的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;
    }
}

How can I add an Item object to allItems every time one is constructed? 如何在每次构造一个Item对象时将allItems添加到allItems

First, the arraylis must be static so it is shared between all the instances. 首先,数组必须是静态的,以便在所有实例之间共享。 Otherwise, you would have a different variable per instance. 否则,每个实例将具有不同的变量。

More info on instance/class members here . 有关实例/类成员的更多信息,请参见此处

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

Then, you can add the created instance in the constructor, refering to it as 'this'. 然后,您可以将创建的实例添加到构造函数中,将其称为“ this”。

About the 'this' keyword . 关于“ this”关键字

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

You certainly want one list for all items, and not every item to carry around and maintain its own copy list of all items. 您当然希望所有项目都有一个列表,而不是每个项目都随身携带并维护自己的所有项目副本列表。 When that's your intention, you should define the list as static : 如果您打算这样做,则应将列表定义为static

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

A static variable is shared by all instances of the class. 该类的所有实例共享一个静态变量。

In the constructor of Item, just add this to the list of all items. 在项目的构造,只需添加this对所有项目的清单。

allItems.add(this);

One possible solution would be to declare the items list as static like this: 一种可能的解决方案是像这样将项目列表声明为静态:

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

afterwards you could access it using the following snippet: 之后,您可以使用以下代码段进行访问:

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

Additionally within your constructor you will need the following code: 此外,在构造函数中,您将需要以下代码:

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

BUT use this approach with caution, since it will add to list every single item created, what could lead to a possible memory leak. 但是请谨慎使用此方法,因为它将添加到创建的每个项目的列表中,这可能导致内存泄漏。

You need a static List and then you can use the class constructor to add this object into the list. 您需要一个静态列表,然后可以使用类构造函数将此对象添加到列表中。

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;
    }
}

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

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