Java 如何将新对象添加到 ArrayList?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19167381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:40:51  来源:igfitidea点击:

How do I add a new Object to an ArrayList?

javaobjectarraylistadd

提问by Dan S.

I'm trying to add a new object to an ArrayList. Each Item object will have 3 attributes:

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

  1. itemNum
  2. info
  3. cost
  1. 项目编号
  2. 信息
  3. 成本

I also have 3 classes:

我也有3个班级:

  1. Item class defines the single items stored in the catalog.
  2. Catalog class maintains the list of the Item objects.
  3. Client class w/main method.
  1. 项目类定义存储在目录中的单个项目。
  2. Catalog 类维护 Item 对象的列表。
  3. 客户端类 w/main 方法。

I have the sets and gets in Item class and I have the ArrayList in Catalog. In Client, I will have options to add, remove, or edit the objects. How do I correctly add a new Item object to the ArrayList?

我在 Item 类中有集合和获取,并且在目录中有 ArrayList。在客户端中,我可以选择添加、删除或编辑对象。如何正确地向 ArrayList 添加新的 Item 对象?

I get the Item class to compile fine, but the Catalog and Client classes are not compiling. Here's the error I get with Catalog class:

我让 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)

Below is code for Item class

下面是 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

below are the set/gets for itemNum

下面是 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

//below is my Catalog 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

below is Client class. It receives input from the user regarding the itemNum, info, and cost

下面是客户端类。它接收用户关于 itemNum、info 和 cost 的输入

 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
 }

Any help would be greatly appreciated.

任何帮助将不胜感激。

采纳答案by angel_navarro

You have some errors...

你有一些错误...

In your class Catalog, this line is not correct:

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

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

you must do:

你必须这样做:

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

And in your class Client, you have not reference to newItem nor listOfObjects, so you cannot use them in that class. If you want to add elements to listOfObjects from your Client's main method, you can do something like this:

在您的类 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);
    }
}