【问题标题】:How can I get a single value from an Array list?如何从数组列表中获取单个值?
【发布时间】:2013-08-02 00:11:54
【问题描述】:

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

我的数组列表如下所示:

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

现在要评估总数,我尝试将其放入构造函数类中:

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

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

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

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

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

哦!这是在 Netbeans for Java 中。

谢谢!

【问题讨论】:

  • 你必须在循环时创建一个局部变量并存储以前的值
  • 在问题的第一部分中,您正在谈论 ArrayListInventory 对象。然后......您正在谈论对它们的数组进行排序。这最终与总计无关。我真的不确定你在问什么。
  • @BrianRoach 好吧,我不知道如何解释,我是新手。我在找出如何对项目列表进行排序时遇到了很多麻烦,我用其他方式写了它。我之前能够得到这个列表的总价格,现在我整理它,我不知道如何从这个新列表中获得总价格。我不知道这是否清楚,如果没有,那么请告诉我我该如何解释。我真的需要帮助。

标签: java arrays loops return-value


【解决方案1】:

这个

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

可以通过这个实现

Collections.sort(a1)

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

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因为这段代码,你得到了双倍的输出

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

【讨论】:

  • 这确实有效。虽然现在我的列表输出翻了一番。我的意思是它出现了两次,因为我删除了这两行; "Inventory[] array = new Inventory[a1.size()]" and Arrays.sort(a1.toArray(array));"。我仍然有相同的 for 循环来输出项目列表。我会继续尝试,直到我得到它,我会告诉你的。:)
  • 你得到双倍的输出,因为你在列表中循环了两次!见编辑
  • 哦!我明白了,非常感谢!我已经尝试了一个多星期,我终于解决了所有问题,直到我卡在这个小部分上。谢谢!
  • 很高兴为您提供帮助。您应该将此答案设为已接受,以便将来找到此帖子的人知道它是如何解决的。
  • 好的,我以后可能会发布更多问题。我将学习更多 Java、C 和 C++ 的基础知识。我正在自学,因为我没有人可以讨论这个问题,所以即使是编程的词汇,有时我也会觉得有点混乱,但希望我能通过时间和练习得到它。再次感谢^w^
【解决方案2】:

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

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

【讨论】:

  • 不客气,当您对答案感到满意时,应将其标记为已接受。您应该在所有问题中都这样做,这样人们就会知道您感兴趣,这是参与社区的一种方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多