0

In the following method, I encounter "java.lang.IndexOutOfBoundsException: Index: 0, Size: 0" error at result.set. There is no compilation error, but after running I get this error. What is wrong with this arraylist assignment? I want to set first and second list variables.

static List<Integer> compare(List<Integer> a, List<Integer> b) {
    int ca=0;
    int cb=0;
    List<Integer> result = new ArrayList<Integer>();

    for(int i=0; i<a.size(); i++){
        if(a.get(i) > b.get(i)){
            ca++;
        }
        else if(a.get(i) < b.get(i)){
            cb++;
        }
    }
    result.set(0, ca);
    result.set(1, cb);
    return result;
}
  • 2
    change `set()` to `add()` – Eran Jul 05 '20 at 11:21
  • @Eran Thanks a lot. But what about updating the first element's value in the arraylist? Assume I set it 0 and then update with 3, how can I do this? –  Jul 05 '20 at 11:34
  • @Eran On the other hand, should I define the arraylist size when defining it? If so, can I set it like `List result = new ArrayList(2);` ? –  Jul 05 '20 at 11:35
  • You can use `set(index,value)` if the size of the List > index, but that wasn't the case in your example. – Eran Jul 05 '20 at 11:38
  • Thanks, but I already used `set(index,value)` as `result.set(0, ca)`, but throws error. So, how can I set the first index and what is wrong with my implementation? –  Jul 05 '20 at 11:45
  • If the List is empty, you should either use `add(value)` or `add(0,value)`. – Eran Jul 05 '20 at 11:51
  • After adding value it will point the next index and then adding new element will add value to that index, is that true? In this case, we cannot use add method for updating list value? For example, I want to set 1st element of the list by 3, should I use `result.set(0, 3);`? If so, why the above code is not work? I know first I initialize, but how? –  Jul 05 '20 at 11:55

2 Answers2

1

The set method is used to replace existing object in list.

For reference take a look at set docs.

"Replaces the element at the specified position in this list with the specified element (optional operation)."

In this situation you should use add(ca) instead of set(0, ca)

mslowiak
  • 1,688
  • 12
  • 25
  • Thanks, I know it and I need to replace / update value sometimes. So, in that case how can I use set method? –  Jul 05 '20 at 11:33
  • In your code you are initializing empty list `result` and add to it two objects. There is no reason to use `set` with that use case. Just play with `add()` :) – mslowiak Jul 05 '20 at 11:34
  • I know, but I just want to know how to set the first and second list variables. Should I set the size etc. like `List result = new ArrayList(2);` (by using 2 in the paranthesis)? –  Jul 05 '20 at 11:37
  • By using `add()` for the first time `List` is aware that you want to put data in the first index of list. When you call `add()` second time the next item will be placed on the second position. The number `2` that you mentioned is the capacity of array. It is handled by default in Java and you don't need to worry about it. It will recalculate the capacity when needed (it will double its capacity). – mslowiak Jul 05 '20 at 11:42
  • Thanks, but I already used `set(index,value)` as `result.set(0, ca)`, but throws error. So, how can I set the first index and what is wrong with my implementation? –  Jul 05 '20 at 11:45
  • You simply cannot do this. The list is empty, you cannot exchange/replace the object on position 0 because there is nothing there. – mslowiak Jul 05 '20 at 11:47
  • Is it possible to set size of the list without using add method i.e. `List result = new ArrayList(2);` (by using 2 in the paranthesis)? –  Jul 05 '20 at 11:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217246/discussion-between-mslowiak-and-jason). – mslowiak Jul 05 '20 at 11:49
0

The java.util.ArrayList.set(int index, E element) replaces the element at the specified position in this list with the specified element.

this size of the result arrayList is zero. so, replacing element at 0 index throws error. use result.add(ca); result.add(cb); or use HashMap to store the ca and cb count, later its easy to retrieve respective count using key.

  • Is it possible to set size of the list without using add method i.e. `List result = new ArrayList(2);` (by using 2 in the paranthesis)? –  Jul 05 '20 at 11:46
  • 1
    You're confusing the size of the array list with its capacity: the size is the number of elements in the list; the capacity is how many elements the list can potentially accommodate without reallocating its internal structures. When you call new ArrayList(10), you are setting the list's initial capacity, not its size. In other words, when constructed in this manner, the array list starts its life empty. One way to add ten elements to the array list is by using a loop: for (int i = 0; i < 10; i++) { arr.add(0); } Having done this, you can now modify elements at indices 0..9. – NagararajaPALLA Jul 06 '20 at 06:33
  • Wonderful explanations, now I am clarified regarding to this issue. Thanks a lot. –  Jul 06 '20 at 18:42