如何将新对象添加到ArrayList?

时间:2013-10-03 19:13:46

标签: java object arraylist add

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

  1. itemNum
  2. info
  3. 成本
  4. 我也有3个班级:

    1. 项目类定义目录中存储的单个项目。
    2. 目录类维护Item对象的列表。
    3. 客户端类w / main方法。
    4. 我有集合并获取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)
      

      以下是Item class

      的代码
       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
      

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

      非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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);
    }
}