如何从数组列表中获取单个值?

时间:2021-09-03 15:53:53

So I'm working on this program in where I need to sort the array list which contains items, the number of the item, total unit in stock, and the price. I already did this, the only problem I'm having is that I cannot find a way to get the total of the Array list by multiplying the total unit with the price.

所以我正在研究这个程序,我需要对数组列表进行排序,其中包含项目,项目编号,库存总量和价格。我已经这样做了,我遇到的唯一问题是我找不到通过将总单位乘以价格来获得数组列表总数的方法。

My array list look something like this:

我的数组列表看起来像这样:

ArrayList< Inventory > a1 = new ArrayList<  >();

    a1.add(new Inventory("shoes", 1345, 134, 20.50f));
    a1.add(new Inventory("pies", 1732, 150, 3.35f));
    a1.add(new Inventory("cherries", 1143, 200, 4.40f));
    a1.add(new Inventory("shirt", 1575, 99, 10.60f));
    a1.add(new Inventory("beer", 1004, 120, 8.50f));

Now to evaluate the total, I tried putting it in my constructor class:

现在评估总数,我尝试将它放在我的构造函数类中:

public float getTotal()
  {
      float total = 0.0f;

      return total += getUnit() * getPrice();  
  }

When I tried to use it in the main class, this is what I wrote:

当我尝试在主类中使用它时,这就是我写的:

Inventory[] array = new Inventory[a1.size()];
    Arrays.sort(a1.toArray(array));
    for (Inventory p : array)
        System.out.println(p);


    for(Inventory total: array)

    System.out.printf("The total of the Inventory is:$%.2f ", total.getTotal());

My problem is, the evaluation is going through every line and is outputting every single value of each line. I tried many other different for loops and I cannot get it to a single total value, which evaluates the entire array list. Can someone explain me how can I get it to a single value? I did it before, but when I changed the array list to be able to sort it, now I cannot get the single value again.

我的问题是,评估是通过每一行,并输出每一行的每一个值。我尝试了许多其他不同的for循环,我无法将其转换为单个总值,它会评估整个数组列表。有人可以解释我怎样才能把它变成单一价值?我之前做过,但是当我更改数组列表以便能够对它进行排序时,现在我再也无法获得单个值。

Oh! and this is in Netbeans for Java.

哦!这是在Netbeans for Java中。

Thanks!

2 个解决方案

#1


3  

This

Inventory[] array = new Inventory[a1.size()];
Arrays.sort(a1.toArray(array));

Can be acheived by this

可以通过这个实现

Collections.sort(a1)

This will sort your list then you can just loop once to get total (I'm Assuming this is what you want)

这将对您的列表进行排序,然后您可以循环一次以获得总计(我假设这是您想要的)

float total = 0;
for(Inventory i: a1)
{
     total += i.getTotal();
     //if you want to print for every item, otherwise remove below line
     System.out.println(i)
}
System.out.println("The total is " + total); // This will just print one line 
                                             // with the total of the entrie list

EDIT You get doubled output because of this code

编辑由于此代码,您获得了双倍的输出

for (Inventory p : array)
    System.out.println(p);  // This prints every item in the list once

for(Inventory total: array) // And this is looping and printing again!
                            // This empty line does not matter, line below is looped
    System.out.printf("The total of the Inventory is:$%.2f ", total.getTotal()); 

#2


1  

You are not adding the total for each "Inventory" in the loop:

您没有为循环中的每个“库存”添加总计:

Inventory[] array = new Inventory[a1.size()];
Arrays.sort(a1.toArray(array));
float total = 0;
for (Inventory p : array)
{
    total += p.getTotal()
}
System.out.printf("The total of the Inventory is:$%.2f ", total);

#1


3  

This

Inventory[] array = new Inventory[a1.size()];
Arrays.sort(a1.toArray(array));

Can be acheived by this

可以通过这个实现

Collections.sort(a1)

This will sort your list then you can just loop once to get total (I'm Assuming this is what you want)

这将对您的列表进行排序,然后您可以循环一次以获得总计(我假设这是您想要的)

float total = 0;
for(Inventory i: a1)
{
     total += i.getTotal();
     //if you want to print for every item, otherwise remove below line
     System.out.println(i)
}
System.out.println("The total is " + total); // This will just print one line 
                                             // with the total of the entrie list

EDIT You get doubled output because of this code

编辑由于此代码,您获得了双倍的输出

for (Inventory p : array)
    System.out.println(p);  // This prints every item in the list once

for(Inventory total: array) // And this is looping and printing again!
                            // This empty line does not matter, line below is looped
    System.out.printf("The total of the Inventory is:$%.2f ", total.getTotal()); 

#2


1  

You are not adding the total for each "Inventory" in the loop:

您没有为循环中的每个“库存”添加总计:

Inventory[] array = new Inventory[a1.size()];
Arrays.sort(a1.toArray(array));
float total = 0;
for (Inventory p : array)
{
    total += p.getTotal()
}
System.out.printf("The total of the Inventory is:$%.2f ", total);