0

i got below mentioned error when run code:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.LinkedList.checkPositionIndex(Unknown Source)
    at java.util.LinkedList.addAll(Unknown Source)
    at Collection.Dynamycmaasiv.Collecktionaddlist.main(Collecktionaddlist.java:36)

code

public static void main(String[] args) {
    LinkedList<Integer> num = new LinkedList<Integer>();
    LinkedList<Integer> numodd = new LinkedList<Integer>();
    LinkedList<Integer> numeven = new LinkedList<Integer>();
    LinkedList<Integer> sumoffevenandodd = new LinkedList<Integer>();// help
                                                                        // me
                                                                        // to
                                                                        // solve

    for (double i = 0; i < 50; i++) {
        num.add((int) i);
        if (i % 2 == 0) {
            numeven.add((int) i);
        } else {
            numodd.add((int) i);

        }
    }

    System.out.println(num);
    System.out.println("-----------------");
    System.out.println(numodd);
    System.out.println("-----------------");
    System.out.println(numeven);

    for (int i =0; i<numeven.size(); i++){

        sumoffevenandodd.addAll(numeven.get(i)+ numodd.get(i), null);
    }

    System.out.println(sumoffevenandodd);
}

}

1
  • sumoffevenandodd.addAll(numeven.get(i)+ numodd.get(i), null); this is where the exception occurs. addAll takes a Collection as parameter, not an Integer. Commented Sep 11, 2016 at 13:00

2 Answers 2

2

addAll() is not about adding up numbers. It is about adding all the elements of the method parameter to the collection itself.

So, you need to loop, like

int sum = 0;
for (Integer numberFromList : numeven) {
  sum = sum + numberFromList;

Or, if you have Java8, you can use streams:

int sumEven = numeven.stream().sum();

Sum, done.

And for the record: the real lesson to be learned here: read the javadoc. Don't assume that method called addAll() does what you suppose it does. Turn to the javadoc and inform yourself what reality thinks about your assumptions.

But just to be clear; as I got carried away with your question, too.

In your code, if you change

sumoffevenandodd.addAll(numeven.get(i)+ numodd.get(i), null);

to

sumoffevenandodd.add(numeven.get(i)+ numodd.get(i));

it should work, too.

Long story short: if you intended to really have a list with 50 sums within, then my first paragraphs do not really help with your problem. But it isn't exactly clear what you wanted to do; so I leave my answer as is - to address both possible explanations what is "wrong" in your logic.

Sign up to request clarification or add additional context in comments.

3 Comments

One can also say "read the question". I am not so sure any more if I am really correct with my assumptions either.
Dear GhostCat, how find difference , put minus but get no result: sumoffevenandodd.add(Collections.singletonList(numeven.get(i) - numodd.get(i)));
Sorry, that part was wrong: when you really want the sum or delta, you don't need that Collections call. Just use add(numeven.get(i) OP numodd.get(i)) where OP is +, -, *, whatever. And side note: names like evenNumbers, or oddNumbers are much better to read!
1

if the intention of the question is

num odd

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]

num even

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]

sum of odd and even

[1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77, 81, 85, 89, 93, 97]

then

for (int i =0; i< numeven.size(); i++){

        sumoffevenandodd.add(numeven.get(i)+ numodd.get(i));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.