• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sum of non numeric elements in an ArrayList

 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is it that the sum of all elements in the ArrayList list (code below) is 10 ?
there are non numeric elements in the arraylist

 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are taking a "sum" of the count from zero to the size of the list.... ie 0 + 1 + 2 + 3 + 4 ==> 10.


Henry
 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when I change it to

sum=sum+list.get(i);

I get error

TestList.java:22: operator + cannot be applied to double,java.lang.Object
sum=sum+list.get(i);
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is telling you that you can't add objects of different types.

"Dog" + 345678 causes an error.

Hunter
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're trying to add together a double and an Object, which you can't do in Java.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:"Dog" + 345678 causes an error.

Actually that would work, if the right references are used. Remember that + is used for string concatenations. However, in this case all references are Object references, and + definitely does not work for objects.

Vijay, print out the classes of the elements. Then find out if the numeric values have a common super class (they do, and it's not Object). If so, find a method that returns something you can add together (hint: it ends with "Value"). You can use the instanceof operator and casting to give you an instance of that common super class.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Hunter McMillen wrote:"Dog" + 345678 causes an error.

Actually that would work, if the right references are used. Remember that + is used for string concatenations. However, in this case all references are Object references, and + definitely does not work for objects.


Sorry I should have said: in this case, this will cause an error.

Hunter
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Integer (package java.lang) class to parse int from a String[Integer.parseInt(...)].

This will return an exception if the argument cannot be converted into int data type.
 
samuel lenk
Greenhorn
Posts: 3
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

If you only want to allow integers in your array list then with generics you can create your list this way. That brings the advantage that java will only let integers by added to the list and you can be sure sum will be calculated correctly. See the example:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic