Java 如何将数组字段重构为ArrayList
这是一个允许用户向系统添加数据的程序。这是程序的invetory类,我们的任务是将对象清单转换为数组列表的对象。。。我是数组列表的新手,我想得到一些帮助来解决这个问题 我试过几次,但到目前为止运气不好,有什么办法吗Java 如何将数组字段重构为ArrayList,java,Java,这是一个允许用户向系统添加数据的程序。这是程序的invetory类,我们的任务是将对象清单转换为数组列表的对象。。。我是数组列表的新手,我想得到一些帮助来解决这个问题 我试过几次,但到目前为止运气不好,有什么办法吗 public class Inventory { /** * List of FoodItems that represents our inventory */ private FoodItem[] inventory; /**
public class Inventory {
/**
* List of FoodItems that represents our inventory
*/
private FoodItem[] inventory;
/**
* Number of items that a user has entered
*/
private int numItems;
/**
* Default Constructor
*/
public Inventory() {
inventory = new FoodItem[20];
}
/**
* Reads from the Scanner object passed in and fills the data member fields of the class with valid data.
* @param scanner - Scanner to use for input
* @return <code>true</code> if all data members were successfully populated, <code>false</code> otherwise
*/
public boolean addItem(Scanner scanner) {
if(numItems == 20)
{
System.out.println("Inventory full");
return false;
}
boolean valid = false;
FoodItem item = null;
while(!valid)
{
System.out.print("Do you wish to add a fruit(f), vegetable(v) or a preserve(p)? ");
if(scanner.hasNext(Pattern.compile("[fFvVpP]")))
{
String choice = scanner.next();
switch(choice.toLowerCase())
{
case "f":
item = new Fruit();
break;
case "v":
item = new Vegetable();
break;
case "p":
item = new Preserve();
break;
default: // Should not get here.
item = new FoodItem();
break;
}
valid = true;
}
else
{
System.out.println("Invalid entry");
scanner.next();
valid = false;
}
}
if(item.inputCode(scanner))
{
if(alreadyExists(item)<0)
{
if(item.addItem(scanner))
{
inventory[numItems] = item;
numItems++;
return true;
}
return false;
}
else
{
System.out.println("Item code already exists");
return false;
}
}
return true;
}
/**
* Search for a food item and see if it is already stored in the inventory
* @param item - FoodItem to look for
* @return - The index of item if it is found, -1 otherwise
*/
public int alreadyExists(FoodItem item) {
for(int i=0;i<numItems;i++)
{
if(inventory[i].isEqual(item))
return i;
}
return -1;
}
/**
* Update the quanity stored in the food item
* @param scanner - Input device to use
* @param buyOrSell - If we are to add to quantity (<code>true</code>) or remove (<code>false</code>)
* @return
*/
public boolean updateQuantity(Scanner scanner, boolean buyOrSell) {
// If there are no items then we can't update, return
if(numItems == 0)
return false;
FoodItem temp = new FoodItem();
temp.inputCode(scanner);
int index = alreadyExists(temp);
if(index != -1)
{
String buySell = buyOrSell?"buy":"sell";
System.out.print("Enter valid quantity to "+buySell+": ");
if(scanner.hasNextInt())
{
int amount = scanner.nextInt();
if(amount > 0)
{
return inventory[index].updateItem(buyOrSell? amount: amount*-1);
}
else
{
System.out.println("Invalid quantity...");
}
}
else
{
System.out.println("Invalid quantity...");
}
}
return false;
}
@Override
public String toString() {
String returnString = "Inventory:\n";
for(int i=0;i<numItems;i++)
returnString += inventory[i].toString() + "\n";
return returnString;
}
}
我可能误解了这个问题,但我猜您希望将库存转换为使用ArrayList,而不是将其转换为ArrayList。对吗 如果我正确理解了这个问题,那么您要做的第一件事就是将
inventory
字段的声明更改为ArrayList:
私有清单清单;
请注意,我将其声明为列表接口,而不是具体的ArrayList实现。除非您需要的ArrayList实现中有一个在通用接口中不可用的特定功能,否则通常最好在通用接口级别进行操作,以获得最大的灵活性
但是,在构造函数中,您将实际实例化具体的ArrayList:
public Inventory(){
存货=新阵列列表(20);
}
通过从裸数组切换到ArrayList,您将发现使用集合的第一个优点,即不再需要alreadyExists()
方法。List及其超级接口Collection支持contains()
方法
这:
if(alreadyExists(item)我可能误解了这个问题,但我猜您希望将库存转换为使用ArrayList,而不是将其转换为ArrayList。对吗
如果我正确理解了这个问题,那么您要做的第一件事就是将inventory
字段的声明更改为ArrayList:
私有清单清单;
请注意,我已将其声明为列表接口,而不是具体的ArrayList实现。除非您需要的ArrayList实现中有一个在通用接口中不可用的特定功能,否则通常最好在通用接口级别进行操作,以提供最大的灵活性
但是,在构造函数中,您将实际实例化具体的ArrayList:
public Inventory(){
存货=新阵列列表(20);
}
通过从裸数组切换到ArrayList,您将发现使用集合的第一个优点,即不再需要alreadyExists()
方法。List及其超级接口Collection支持contains()
方法
这:
if(已存在(项)什么是…项目
,水果
,蔬菜
,保存
,和食品数据项
?这些是程序中的独立类,它们都扩展了本程序中的抽象类FoodItem。然而,项目是从FoodItem类引用的对象。什么是…项目
,水果
、Planet
、Preserve
、和FoodItem
?这些都是程序中的独立类,它们都扩展了FoodItem,这是程序中的抽象类。但是Item是从FoodItem类引用的对象。啊,谢谢你!但是我在第115行和第105行仍然收到一些错误。115:退货库存[索引]。更新项(买方销售?金额:金额*-1);105:int索引=alreadyExists(临时);是的,这是因为列表和数组相似,但不同的数据结构具有不同的语法。列表接口的实现是带有字段和方法的普通对象,数组是基本类型。因此,您需要调用inventory[index]
语法,而不是inventory.get(int-index)第115行的
方法。在第105行,您将用inventory.contains(temp)替换您的alreadyExists()
如我在回答中所示。这是列表的javadoc:Awsome,谢谢!但是我在第115行和第105行仍然收到一些错误。115:退货库存[index]。更新项(buyOrSell?金额:金额*-1);105:int index=alreadyExists(临时);是的,这是因为列表和数组相似,但不同的数据结构具有不同的语法。列表接口的实现是带有字段和方法的普通对象,数组是基本类型。因此,您需要调用inventory[index]
语法,而不是inventory.get(int-index)第115行的
方法。在第105行,您将用inventory.contains(temp)
替换您的alreadyExists()
,如我在回答中所示。以下是列表的javadoc: