使用带字符串的arraylist,int,double

时间:2013-11-12 05:17:22

标签: java netbeans arraylist

我刚开始接受arraylists所以请耐心等待我。我正在尝试在索引0处创建一个条目,其中包含项目名称,项目编号,库存量和项目价格。我认为我已经构建了我的数组列表以使用我的InventoryItem类,但我不确定我是否已经这样做了。这是我的InventoryItem类。

class InventoryItem { //Delcare variables below

String ItemName;
int ItemNumber;
int InStock;
double UnitPrice;
double Value;

public InventoryItem(String ItemName, int ItemNumber, int InStock, double UnitPrice) { //declare variables for this class
    this.ItemName = ItemName;
    this.ItemNumber = ItemNumber;
    this.InStock = InStock;
    this.UnitPrice = UnitPrice;
    this.Value = UnitPrice * InStock; //calculate value of inventory

}

public void output() {
    System.out.println("Item Name = " + ItemName); //print out the item name
    System.out.println("Item Number = " + ItemNumber); //print out the item number
    System.out.println("In Stock = " + InStock); //print out how many of the item are in stock
    System.out.println("Item Price = $" + UnitPrice); //print out the price per item
    System.out.println("Value of inventory = $" + Value); //calculate how much the inventory is worth for this one item

}

正如您所看到的,此类中列出了字符串,整数和双精度数。 现在我创建了我的arraylist,如下所示。 (我试图将它们全部插入0点)

package inventory;

import java.util.ArrayList;

公共类库存{

public static void main(String[] args) {
    ArrayList InventoryItem = new ArrayList();
    InventoryItem.add(0, "Pencil ");
    InventoryItem.add(0, "123456789");
    InventoryItem.add(0, "1");
    InventoryItem.add(0, "1.25");

    for (int i = 0; i< InventoryItem.size(); i++)
        System.out.printf("%s", InventoryItem.get(i));
    System.out.println();

我的问题是我想让这个列表在一行上输入。我可以做点什么吗

ArrayList<String int int double> Stock = new ArrayList<String int int double>();
InventoryItem.add(0, "Pencil", 123456789, 1, 1.25);

这是否会像我预期的那样进入我的0? 我试过这样做,但它不喜欢它。我也尝试在每种类型之间加一个逗号,但这似乎也不起作用。也许我的显示器不适合这种情况?任何帮助都会很棒。这实际上适用于多维类型数组,其中每一行都具有这些特征。

3 个答案:

答案 0 :(得分:1)

试试这个

ArrayList<InventoryItem> inventory = new ArrayList<InventoryItem>();
InventoryItem in_1 = new InventoryItem(0, "Pencil", 123456789, 1, 1.25);
inventory.add(in_1);

或单行

inventory.add(new InventoryItem(0, "Pencil", 123456789, 1, 1.25));

答案 1 :(得分:0)

由于您已经拥有了InventoryItem calss,因此请创建它的对象并将值设置为该对象,并将此对象添加到ArrayList这样的内容

   InventoryItem item = new InventoryItem("Pencil", 123456789, 1, 1.25);
   ArrayList<InventoryItem> itemList = new ArrayList<InventoryItem>();
   itemList.add(item);

Retriving你将获得项目对象,它包含你之前设置的所有数据

答案 2 :(得分:0)

谢谢你们的帮助。没有它,我就不会到达现在的位置。我修改了我的代码看起来像这样:

public static void main(String[] args) {

    ArrayList<inventoryItem> inventory = new ArrayList<inventoryItem>();
    inventory.add(new inventoryItem("Pencil", 1111, 10, .25));
    inventory.add(new inventoryItem("Pen", 9876, 2222, .99));
    inventory.add(new inventoryItem("Canned Air", 3333, 5, 2.00));
    inventory.add(new inventoryItem("Notebook", 4444, 10, 2.50));
    inventory.add(new inventoryItem("Staples", 5555, 5, 1.00));
    inventory.add(new inventoryItem("Paper Clips", 6666, 10000, .05));



    double total = 0;

    for (int i = 0; i < inventory.size(); i++) {
        inventory.get(i).output();

    }
    inventoryTotal(inventory);


}

public static void inventoryTotal(ArrayList<inventoryItem> inventory) {
    double total = 0;

    for (int i = 0; i < inventory.size(); i++) {
        total = total + inventory.get(i).value;
    }
    System.out.printf("Total value of inventory $%.2f\n", + total);

这允许我使用inventory.add添加到arraylist并分配它。我现在要做的下一件事是按字母顺序对项目进行排序,但我认为我可以处理。

再次感谢您的帮助!