Java 自动将构造的每个类对象添加到ArrayList中
我在学习Java方面还是个新手,我需要一种方法来将从某个类构造的每个对象放入ArrayList中,以便以后可以访问 这是我的Java 自动将构造的每个类对象添加到ArrayList中,java,class,object,arraylist,Java,Class,Object,Arraylist,我在学习Java方面还是个新手,我需要一种方法来将从某个类构造的每个对象放入ArrayList中,以便以后可以访问 这是我的项目课程: import java.util.ArrayList; import java.util.*; public class Item { private String name; private int quantity; private ArrayList<Item> allItems = new ArrayList<I
项目
课程:
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;
}
}
import java.util.ArrayList;
导入java.util.*;
公共类项目
{
私有字符串名称;
私人整数数量;
private ArrayList allItems=new ArrayList();//我试图用每个Item对象填充的ArrayList
/**
*构造具有给定名称和数量的项
*@param name项目名称
*@param数量玩家拥有的物品数量
*/
公共项(字符串名称、整数数量)
{
this.name=名称;
这个。数量=数量;
}
/**
*@返回项目名称
*/
公共字符串getItemName()
{
返回名称;
}
/**
*@该物品的退货数量
*/
公共整数getQuantity()
{
退货数量;
}
/**
*@返回数量大于0的项目列表
*/
公共ArrayList getInventory()
{
ArrayList inventory=新的ArrayList();
用于(项目i:所有项目)
{
如果(i.getQuantity()>0)
增加(一);
}
退货库存;
}
}
如何在每次构造
项时将项
对象添加到所有项
?一种可能的解决方案是将项列表声明为静态,如下所示:
public static List<Item> allItems = new ArrayList<Item>();
此外,在构造函数中,还需要以下代码:
public Item(String name, int quantity) {
this.name = name;
this.quantity = quantity;
this.allItems.add(this);
}
但是请谨慎使用此方法,因为它会将创建的每个项目添加到列表中,可能会导致内存泄漏。一种可能的解决方案是将项目列表声明为静态,如下所示:
public static List<Item> allItems = new ArrayList<Item>();
此外,在构造函数中,还需要以下代码:
public Item(String name, int quantity) {
this.name = name;
this.quantity = quantity;
this.allItems.add(this);
}
但是请谨慎使用此方法,因为它会将创建的每个项目添加到列表中,这可能会导致内存泄漏。您当然希望所有项目都有一个列表,而不是每个项目都有自己的所有项目副本列表。当您打算这样做时,您应该将列表定义为静态
:
private static ArrayList<Item> allItems = new ArrayList<Item>()
您当然希望所有项目都有一个列表,而不是每个项目都有自己的副本列表。当您打算这样做时,您应该将列表定义为静态
:
private static ArrayList<Item> allItems = new ArrayList<Item>()
首先,arraylis必须是静态的,以便在所有实例之间共享。
否则,每个实例将有不同的变量
有关实例/类成员的详细信息
首先,arraylis必须是静态的,以便在所有实例之间共享。
否则,每个实例将有不同的变量
有关实例/类成员的详细信息
您需要一个静态列表,然后可以使用类构造函数将此对象添加到列表中
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;
}
}
import java.util.ArrayList;
导入java.util.*;
公共类项目
{
私有字符串名称;
私人整数数量;
private static ArrayList allItems=new ArrayList();//我试图用每个Item对象填充的ArrayList
/**
*构造具有给定名称和数量的项
*@param name项目名称
*@param数量玩家拥有的物品数量
*/
公共项(字符串名称、整数数量)
{
this.name=名称;
这个。数量=数量;
添加(此);
}
/**
*@返回项目名称
*/
公共字符串getItemName()
{
返回名称;
}
/**
*@该物品的退货数量
*/
公共整数getQuantity()
{
退货数量;
}
/**
*@返回数量大于0的项目列表
*/
公共静态ArrayList getInventory()
{
ArrayList inventory=新的ArrayList();
用于(项目i:所有项目)
{
如果(i.getQuantity()>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;
}
}
import java.util.ArrayList;
导入java.util.*;
公共类项目
{
私有字符串名称;
私人整数数量;
private static ArrayList allItems=new ArrayList();//我试图用每个Item对象填充的ArrayList
/**
*构造具有给定名称和数量的项
*@param name项目名称
*@param数量玩家拥有的物品数量
*/
公共项(字符串名称、整数数量)
{
this.name=名称;
这个。数量=数量;
添加(此);
}
/**
*@返回项目名称
*/
公共字符串getItemName()
{
返回名称;
}
/**
*@该物品的退货数量
*/
公共整数getQuantity()
{
退货数量;
}
/**
*@返回数量大于0的项目列表
*/
公共静态ArrayList getInventory()
{
ArrayList inventory=新的ArrayList();
用于(项目i:所有项目)
{
如果(i.getQuantity()>0)
增加(一);
}
退货库存;
}
}
之所以投票支持这一点,是因为它是唯一一个(我的除外)解释了static
关键字的实际功能。之所以投票支持这一点,是因为它是唯一一个(我的除外)解释了static
关键字的实际功能。作为友好的评论,请始终尝试使用接口而不是实现,因此,不要返回ArrayList return List,而是在创建时,执行List myList=new ArrayList()。原因?如果将来使用不同的列表接口实现(如LinkedList),只需更改几行即可。作为友好的评论,请始终尝试使用接口而不是实现,因此不要返回ArrayList返回列表,在创建时,请