簡體   English   中英

自動將構造的每個Class對象添加到ArrayList中

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

我在學習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添加到allItems

首先,數組必須是靜態的,以便在所有實例之間共享。 否則,每個實例將具有不同的變量。

有關實例/類成員的更多信息,請參見此處

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

您當然希望所有項目都有一個列表,而不是每個項目都隨身攜帶並維護自己的所有項目副本列表。 如果您打算這樣做,則應將列表定義為static

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

該類的所有實例共享一個靜態變量。

在項目的構造,只需添加this對所有項目的清單。

allItems.add(this);

一種可能的解決方案是像這樣將項目列表聲明為靜態:

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

但是請謹慎使用此方法,因為它將添加到創建的每個項目的列表中,這可能導致內存泄漏。

您需要一個靜態列表,然后可以使用類構造函數將此對象添加到列表中。

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