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

Java 将arraylist与字符串、int、double一起使用,java,netbeans,arraylist,Java,Netbeans,Arraylist,我刚开始使用ArrayList,所以请容忍我。我正在尝试在索引0处创建一个条目,其中包含项目名称、项目编号、库存金额和项目价格。我想我已经构建了使用InventoryItem类的数组列表,但是我不太确定我是否已经这样做了。这是我的InventoryItem类 class InventoryItem { //Delcare variables below String ItemName; int ItemNumber; int InStock; double UnitPrice; double

我刚开始使用ArrayList,所以请容忍我。我正在尝试在索引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点)

导入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();
publicstaticvoidmain(字符串[]args){
ArrayList InventoryItem=新的ArrayList();
添加(0,“铅笔”);
增加(0,“123456789”);
添加(0,“1”);
增加(0,“1.25”);
对于(int i=0;i
我的问题是,我想让这个列表在一行上输入信息。我可以这样做吗

ArrayList<String int int double> Stock = new ArrayList<String int int double>();
InventoryItem.add(0, "Pencil", 123456789, 1, 1.25);
ArrayList Stock=new ArrayList();
增加(0,“铅笔”,123456789,1,1.25);
这是否会使我的0条目符合我的预期? 我尝试过这样做,但它不喜欢。我也尝试过在每种类型之间加一个逗号,但这似乎也不起作用。也许我的显示不适合这种情况?任何帮助都会很好。这确实适用于多维类型数组,其中每行都有这些特征。

试试这个

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

由于您已经有了InventoryItem Cals,请创建它的对象,并将值设置为该对象,然后将该对象添加到ArrayList中,如下所示

   InventoryItem item = new InventoryItem("Pencil", 123456789, 1, 1.25);
   ArrayList<InventoryItem> itemList = new ArrayList<InventoryItem>();
   itemList.add(item);
InventoryItem=新的InventoryItem(“铅笔”,123456789,1,1.25);
ArrayList itemList=新建ArrayList();
项目列表。添加(项目);

在检索时,您将获得item对象,其中包含您先前设置的所有数据

谢谢大家的帮助。如果没有它,我将无法到达现在的位置。我已将代码修改为如下所示:

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);
publicstaticvoidmain(字符串[]args){
ArrayList inventory=新的ArrayList();
添加(新的库存项目(“铅笔”,1111,10,25));
添加(新的库存项目(“Pen”,98762222.99));
增加(新的库存项目(“罐装空气”,3333,5,2.00));
增加(新的库存项目(“笔记本”,4444,10,2.50);
增加(新的存货项目(“史泰博”,5555,5,1.00));
添加(新的库存项目(“回形针”,666610000.05));
双倍合计=0;
对于(int i=0;i
这允许我使用inventory.add添加并分配它到arraylist。接下来我要做的是按字母顺序对项目排序,但我认为我可以处理


再次感谢您的帮助!

您误解了封装。您已经有了一个名为
InventoryItem
的类,为什么不创建它的实例并将它们添加到
ArrayList
中呢?听起来不错,但我对如何做这样的事情一无所知。我想这是我最初认为我应该做的可以,但我是这里的新手。
ArrayList yourList=new ArrayList();yourList.add(new inventory项目(“铅笔”,123456789,1,1.25))
也许你应该像刚才建议的那样先复习一下基础知识。代码风格要点:Java程序员对局部变量和非静态成员使用小写首字母,例如
inventoryItem
itemNumber
,等等。这不起作用,因为它将0视为整数,而不是索引号。我尝试了这两种方法上面的示例。尝试像这样获取InventoryItem item=inventory.get(0);然后您应该能够获取item.itemNumber,…现在的问题是我需要对输出进行排序。我已经阅读了说明,但似乎无法使其工作。关于如何对此列表进行排序,有什么想法吗?
   InventoryItem item = new InventoryItem("Pencil", 123456789, 1, 1.25);
   ArrayList<InventoryItem> itemList = new ArrayList<InventoryItem>();
   itemList.add(item);
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);