如何获得LinkedList的总和(LinkedList中的总和) [英] How to get the sum of an LinkedList (Sum up items in a LinkedList)
问题描述
我想做的是获取从数据库中检索到的所有整数项的总和,然后将其添加到LinkedList中,如下所示:
What I'm trying to do is get the sum of all integer items retrieved from the database then added into a LinkedList, like so:
ManageFeeNotePOJO selectedRecord = (ManageFeeNotePOJO) manageFeeNoteListTableView().getItems().get(selectdIndex);
List toFeeBillListTot = new LinkedList();
toFeeBillListTot.add(selectedRecord.getFeeNoteValue());
int sum = 0;
for (Object sumItem : toFeeBillListTot) {
sum += sumItem;
}
System.out.println(sum);
有了这个我得到错误:
bad operand types for binary operator '+'
first type: int
second type: Object
另一种方法是:
ManageFeeNotePOJO selectedRecord = (ManageFeeNotePOJO) manageFeeNoteListTableView().getItems().get(selectdIndex);
List toFeeBillListTot = new LinkedList();
toFeeBillListTot.add(selectedRecord.getFeeNoteValue());
int sum = 0;
for (int i = 0; i < toFeeBillListTot.size(); i++) {
sum += (int) toFeeBillListTot.get(i);
}
System.out.println(sum);
我得到 java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Integer
错误
拜托,我将为此提供任何可行的方法.预先谢谢你.
Please, I would be more than greatful for any working approach around this. Thank you in advance.
推荐答案
bad operand types for binary operator '+'
first type: int
second type: Object
这是说:您不能将 +
运算符与这两种类型之一一起使用"-我敢打赌,您可以猜出哪一个- Object
-哪一种,如果您考虑一下,这是有道理的:
This is saying: "You can't use the +
operator with one these two types" - and I bet you can guess which one - Object
- which, if you think about it, makes sense:
请记住,您将在Java中使用的每种类型的每个类都扩展了 Object
.因此,当您使用已强制转换为 Object
的对象时,Java几乎不知道该对象是什么.特别是,没有任何奇怪的线索对应用于一个 +
的内容该怎么做.
Remember that every class of every type you'll ever work with in Java extends Object
. So, when you're working with something that has been cast to Object
, Java knows almost nothing about what that something is. In particular, it would have no freaking clue what to do with a +
applied to one.
例如,假设您有一个 Person
类并且做了
For instance, imagine that you had a Person
class and did
Person a = new Person();
Person b = new Person();
a + b; //WTF??
那有意义吗?当然不是.
Would that make sense? Of course not.
现在,您可能会说,但是我没有将对象添加到列表中,而是添加了整数/双精度数/+运算符应与之配合使用的其他类型.您是正确的,但是 问题是您将它们添加到了对象列表 ,您在此处创建了这样的对象:
Now, you might say "but I didn't add Objects to my list, I added Integers/Doubles/some other type that the + operator should work with" - and I'm sure you would be right, but the problem is that you added them to a List of Objects, which you created as such right here:
List sum toFeeBillListTot = new LinkedList();
执行此操作时,您正在创建未键入的列表".这意味着您可以向其中添加任何类型的对象-听起来不错,但自然的结果是,您只能从中检索 Object
,这就是您现在正在做的.
When you do this, you're creating an "untyped list". This means you can add objects of any type you like to it - which might sound nice, but as a natural consequence, you can only retrieve Object
s from it, which is what you're doing now.
通过使用通用集合",@ Jeroen建议您改为执行此操作:
By "use a generic collection", @Jeroen is suggesting you do this instead:
List<Integer> toFeeBillListTot = new LinkedList<>();
< Integer>
而不是创建对象列表,这意味着您正在创建一个整数列表,这意味着您只能添加 Integer
,并且您从其中提取的每个元素都将是Integer,因此您可以执行以下操作:
Rather than creating a list of Objects, the <Integer>
means you're creating a list of integers, which means you can only add Integer
s to it, and also that every element you pull out of it will be an Integer, so you can do:
int sum = 0;
for (Integer sumItem : toFeeBillListTot) {
sum += sumItem;
}
这篇关于如何获得LinkedList的总和(LinkedList中的总和)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!