Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

java - How to get the sum of an LinkedList (Sum up items in a 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);

With this I get the error:

bad operand types for binary operator '+'
first type:  int
second type: Object

The other approach is:

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

I get java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer error with this

Please, I would be more than greatful for any working approach around this. Thank you in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
bad operand types for binary operator '+'
first type:  int
second type: 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:

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.

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

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 Objects from it, which is what you're doing now.

By "use a generic collection", @Jeroen is suggesting you do this instead:

List<Integer> toFeeBillListTot = new LinkedList<>();

Rather than creating a list of Objects, the <Integer> means you're creating a list of integers, which means you can only add Integers 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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...