gpt4 book ai didi

java - 如何将新对象添加到 ArrayList?

转载 作者:行者123 更新时间:2023-11-29 07:54:03 26 4
gpt4 key购买 nike

我正在尝试向 ArrayList 添加一个新对象。每个 Item 对象都有 3 个属性:

  1. 项目数
  2. 信息
  3. 费用

我还有 3 个类:

  1. 项目类定义存储在目录中的单个项目。
  2. Catalog 类维护 Item 对象的列表。
  3. 带 main 方法的客户端类。

我在 Item 类中有 sets 和 gets,在 Catalog 中有 ArrayList。在客户端中,我可以选择添加、删除或编辑对象。如何正确地将新的 Item 对象添加到 ArrayList?

我让 Item 类可以正常编译,但 Catalog 和 Client 类没有编译。这是我在 Catalog 类中遇到的错误:

Catalog.java:35: error: no suitable method found for add(int,String,double)listOfObjects.add(newItemId, newDescription, newCost); 
method ArrayList.add(int,Item) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.add(Item) is not applicable
(actual and formal argument lists differ in length)

下面是Item类的代码

 Public class Item 
{
private int itemNum;
private String info;
private double cost;

public Item()
{ //start constructor
itemNum = 0; //default values
info = "x";
cost = 0;
} //end constructor

public CatalogItem(int newItemNum, String newInfo, double newCost)
{ //start overload constructor
this.itemNum = newItemNum;
this.info = newInfo;
this.cost = newCost;
} //end overload constructor

下面是 itemNum 的设置/获取

     public int getItemNum()
{ //start itemNum accessor
return itemNum;
} //end getItemNum

public void setItemNum(int newItemNum)
{ //start itemNum mutator
this.itemNum = newItemNum;
} //end setItemNum
} //end Item class

//下面是我的目录类

 import java.util.*;

public class Catalog
{ //start class
private ArrayList<CatalogItem> listOfObjects = new ArrayList<CatalogItem>(100); //creates ArrayList
Item newItem = new Item(newItemNum, newInfo, newCost); //instantiates Item class

/*
public Catalog()
{ //start constructor

} //end constructor
*/

public void add(CatalogItem newItem) //method adds a new Item object to the array list
{ //start add
listOfObjects.add(newItem);

} //end add

public void add(int itemNum, String info, double cost) //accepts parameters from main method to add to new object
{ //start add
int newItemNum = itemNum;
String newInfo = info;
double newCost = cost;

newItem.setItemNum(newItemNum);
newItem.setInfo(newInfo);
newItem.setCost(newCost);

listOfObjects.add(newItemNum, newInfo, newCost);

} //end add
} //end class

下面是客户端类。它接收用户关于商品编号、信息和成本的输入

 import java.util.*;   //allows use of Scanner class

public class Client
{ //start client class

public static void main(String[] args)
{ //start main
Catalog serv = new Catalog(); //creates instance of Catalog class
Scanner scan = new Scanner(System.in); //creates instance of Scanner class called scan

public void add(int itemNum, String info, double cost) //accepts parameters from main method to add to new object
{ //start add
int newItemNum = itemNum;
String newInfo = info;
double newCost = cost;

newItem.setItemNum(newItemNum);
newItem.setInfo(newInfo);
newItem.setCost(newCost);

listOfObjects.add(newItemNum, newInfo, newCost); //adds the object to the ArrayList

} //end add
}

如有任何帮助,我们将不胜感激。

最佳答案

你有一些错误...

在您的类目录中,这一行不正确:

listOfObjects.add(newItemNum, newInfo, newCost);   //adds the object to the ArrayList

你必须做的:

CatalogItem cat = new CatalogItem(newItemNum, newInfo, newCost);
listOfObjects.add(cat); //adds the object to the ArrayList

并且在您的类 Client 中,您没有引用 newItem 或 listOfObjects,因此您不能在该类中使用它们。如果您想从客户端的 main 方法向 listOfObjects 添加元素,您可以这样做:

public class Client {
public static void main(String[] args) { //start main
Catalog serv = new Catalog();
......
serv.add(1, "", 1.0d);
}
}

关于java - 如何将新对象添加到 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19167381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com