Java 将 arraylist 与字符串、int、double 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19921419/
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 21:32:09  来源:igfitidea点击:

Using arraylist with strings, int, double

javanetbeansarraylist

提问by kerinr

I am just starting out with arraylists so please bear with me. I am trying to create an entry at index 0 with an item name, item number, amount in stock, and the price of the item. I think that I have built my array list to use my InventoryItem class, but I am not quite sure that I have done so. Here is my InventoryItem class.

我刚开始使用数组列表,所以请耐心等待。我正在尝试在索引 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

}

As you can see there are strings, integers, and doubles listed in this class. Now I have created my arraylist as shown here. (I have tried to insert them all to the 0 spot)

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

package inventory;

import java.util.ArrayList;

导入 java.util.ArrayList;

public class Inventory {

公共类库存{

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();

My problem is that I wanted this list to take an input on a single line. Can I do something like

我的问题是我希望这个列表在一行上输入。我可以做类似的事情吗

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

Will this make my 0 entry as I have intended? I have tried to do this but it doesn't like it. I have also tried to put a comma in between each type, but that doesn't seem to work either. Maybe my display is not right for this situation? Any help would be great. This is really for a multidimensional type array where each line has these characteristics.

这会让我的 0 条目如我所愿吗?我试图这样做,但它不喜欢它。我也试过在每种类型之间加一个逗号,但这似乎也不起作用。也许我的显示器不适合这种情况?任何帮助都会很棒。这确实适用于多维类型数组,其中每行都具有这些特征。

采纳答案by Siva

As you already have the InventoryItem calss, create the object of it and set the values to the object and add this object to ArrayList something like this

由于您已经拥有 InventoryItem calss,因此请创建它的对象并将值设置为该对象并将此对象添加到 ArrayList 中,如下所示

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

While Retriving You will get the item Object, which has all the data you have set earlier

检索时您将获得项目对象,其中包含您之前设置的所有数据

回答by subash

try this

尝试这个

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

or in single line

或单行

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

回答by kerinr

Thank you guys for your assistance. Without it I wouldn't have gotten to where I am right now. I have modified my code to look like this:

谢谢你们的帮助。没有它,我就不会走到现在的位置。我已经修改了我的代码,如下所示:

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);

This allows me to add to the arraylist using inventory.add and assigning it. The next thing I now have to do is sort the items alphabetically, but I think I can handle that.

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

Thanks again for all your help!

再次感谢你的帮助!