简体   繁体   English

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

[英]Using arraylist with strings, int, double

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. 我试图在索引0处创建一个条目,其中包含商品名称,商品编号,库存数量和商品价格。 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. 我认为我已经建立了数组列表来使用我的InventoryItem类,但是我不太确定自己这样做了。 Here is my InventoryItem class. 这是我的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. 现在,我创建了我的arraylist,如下所示。 (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? 这将使我的0条目符合预期吗? 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. 这实际上是针对多维类型的数组,其中每一行都具有这些特征。

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

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,请创建它的对象并将值设置为该对象,然后将此对象添加到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 检索时,您将获得对象对象,其中包含您之前设置的所有数据

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. 这使我可以使用ventory.add将其添加到arraylist并进行分配。 The next thing I now have to do is sort the items alphabetically, but I think I can handle that. 我现在要做的下一件事是按字母顺序对项目进行排序,但是我认为我可以处理。

Thanks again for all your help! 再次感谢你的帮助!

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

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