1

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

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

我还有 3 节课:

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

我在 Item 类中有集合和获取,并且在 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)

下面是项目类的代码

 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

下面是客户端类。它接收用户关于 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
 }

任何帮助将不胜感激。

4

1 回答 1

5

你有一些错误...

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

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);
    }
}
于 2013-10-03T19:18:43.947 回答