0

Can someone please suggest, how to get the sum of first few list elements (integers)?

Note: My Java version: "1.7.0_07".

Below is the code:

    Integer sum=new Integer(0);
    for(int i=0;i<3;i++)
    sum=sum+list.get(i);

Error: The operator + is undefined for the argument type(s) Integer, Object

Srikanth Nakka
  • 758
  • 4
  • 16
  • 35

4 Answers4

2

i believe you have declared your list as a raw type, i.e.

List list = new List();

what you need to do is:

List<Integer> list = new List<Integer>();

or else you would have to typecast whenever you do list.get() like

sum=sum+(Integer)list.get(i);

EDIT: regarding your other problem

If i define like --> 'List list', seeing error suggestion like "Syntax error, parameterized types are only available if source level is 1.5"

Answer:

my guess would be that while you run eclipse itself with jdk 1.6, it's actually configured with a different default jre. see window->preferences->java->Installed JREs and make sure that the checked JRE is 1.6. (i'm hoping that you are getting this error while working in some IDE) if the default JRE is indeed 1.6, chances are that it's a project specific setting. see that the project is configured to use the right jre.

gaurav5430
  • 12,934
  • 6
  • 54
  • 111
  • If i define like --> 'List list', seeing error suggestion like "Syntax error, parameterized types are only available if source level is 1.5".. – Srikanth Nakka Dec 25 '13 at 09:13
  • 1
    my guess would be that while you run eclipse itself with jdk 1.6, it's actually configured with a different default jre. see window->preferences->java->Installed JREs and make sure that the checked JRE is 1.6. (i'm hoping that you are getting this error while working in some IDE) if the default JRE is indeed 1.6, chances are that it's a project specific setting. see that the project is configured to use the right jre. – gaurav5430 Dec 25 '13 at 09:14
  • 1
    Yes, exactly. I changed my Compiler Compliance level to 1.6 and it got solved. Thank you so much. :) – Srikanth Nakka Dec 25 '13 at 09:18
  • if it's solved, please accept as answer, so others can stop posting duplicate answer. or do you want some more explanation about anything regarding your question? – gaurav5430 Dec 25 '13 at 09:19
  • Wait.. :) "You can accept an answer in 58 seconds" – Srikanth Nakka Dec 25 '13 at 09:20
2

Your list should be defined as :

List<Integer> list = whateverCreatesYourList();

then you can:

Integer sum = Integer.valueOf(0);
for(Integer i: list){
    sum += i;
}

The important part is not using the raw List type but the fully defined generic List<Integer>.

le-doude
  • 3,345
  • 2
  • 25
  • 55
  • +1 I would start with `int sum = 0;` – Peter Lawrey Dec 25 '13 at 10:09
  • just wanted to show him the good practice of `Integer.valueOf(0)` rather than the `new Integer(0)` initialization. Plus maybe he did need an Integer object... – le-doude Dec 25 '13 at 11:27
  • Do you prefer this to `Integer sum = 0;` ? – Peter Lawrey Dec 25 '13 at 21:55
  • 1
    Well as an answer yes ... since I would have to explain what is auto-wrapping, or were you planning to leave it at that and not explain him how an `int` expression (0) was assigned to an `Integer` reference? But in my code ... don't expect me to type 1 character I do not have to. – le-doude Dec 25 '13 at 22:29
0
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(5);
    list.add(5);
    list.add(5);

    Integer sum = new Integer(0);
    for (int i = 0; i < 3; i++) {
        sum = sum + list.get(i);
    }

    System.out.println(sum);
Burhan ARAS
  • 2,517
  • 25
  • 19
0

Check this sample

List<Integer> list = new ArrayList<Integer>();
list.add(10);
list.add(20);
list.add(30);
Integer sum = 0;
int listSize = list.size();
for (int i = 0; i < 3; i++) {
    if (i < listSize) {
       sum += list.get(i);
    }
 }
Ronak Jain
  • 2,402
  • 2
  • 24
  • 38