简体   繁体   English

如何将新对象添加到ArrayList?

[英]How do I add a new Object to an ArrayList?

I'm trying to add a new object to an ArrayList. 我正在尝试将新对象添加到ArrayList。 Each Item object will have 3 attributes: 每个Item对象将具有3个属性:

  1. itemNum itemNum
  2. info 信息
  3. cost 成本

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. 目录类维护Item对象的列表。
  3. Client class w/main method. 带main方法的客户端类。

I have the sets and gets in Item class and I have the ArrayList in Catalog. 我在Set类中具有设置并获取,在Catalog中具有ArrayList。 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?

I get the Item class to compile fine, but the Catalog and Client classes are not compiling. 我可以很好地编译Item类,但未编译Catalog和Client类。 Here's the error I get with Catalog class: 这是我在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 它从用户接收有关项目编号,信息和成本的输入

 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. 任何帮助将不胜感激。

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. 在Client类中,您没有引用newItem或listOfObjects,因此无法在该类中使用它们。 If you want to add elements to listOfObjects from your Client's main method, you can do something like this: 如果要从客户端的main方法向listOfObjects添加元素,则可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM