-3
while(list.size() != 0 )
{
    for(int i=0 ;i<list.size();i++)
    {
        //if current element is smaller than firat update first & second
        if(list.get(i) < first)
        {
            second = first;
            first = list.get(i);
            j = i;
        }
        else if(list.get(i)<second && list.get(i) != first)
        {
            second= list.get(i);
            k = i;
        }     
    }

    list.remove(j);
    list.remove(k);
    list.add(first+second);

    for(int inte :sortedArray)
    {
        sortedArray.add(first+second);
    }
}
Collections.sort(sortedArray);

Here in this code Im getting an error!!!

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index:2,Size: 2

Please anybody help me where it is going wrong! Thanks in advance.

rufer7
  • 3,369
  • 3
  • 22
  • 28
Lata ss
  • 11
  • 4
  • 2
    is there a question? The code is incomplete. Also it would be helpful if the line where the error occurs would be indicated. – felix the cat Dec 29 '17 at 11:28
  • 4
    Possible duplicate of [What is IndexOutOfBoundsException? how can i fix it?](https://stackoverflow.com/questions/40006317/what-is-indexoutofboundsexception-how-can-i-fix-it) – AxelH Dec 29 '17 at 11:29
  • 2
    Let met guess, a `List` ? `list.remove(int)` and `list.remove(Integer)` are messing with you – AxelH Dec 29 '17 at 11:31
  • explain what you want to do via this code – Rajat Dec 29 '17 at 11:36
  • Exception is clearly saying that size of list is 2. means you have only two elements in this list at index 0 and 1. but you are trying to access value from index 2. Please check list size before accessing it just above the line in which you are getting this error. – DhaRmvEEr siNgh Dec 29 '17 at 11:38

1 Answers1

1

Remove by index in the wrong order

You are removing items by index with j and k but don't check the order.

If you have :

  • 4 items
  • j = 2
  • k = 3

When you call

list.remove(j); //list.length() == 3
list.remove(k); //Exception

When you call remove(k), the list is shorter, so you are out of the list. Simply remove the bigger index first.

list.remove(Math.max(j,k));
list.remove(Math.min(j,k));

List to small

You are doing your check until the list is empty, you won't be able to remove two items if you only have one, simply update the condition to stop when you only have 1 item.

while (list.size() > 1) {

Older values

It is possible that j or k are not update since first and second are not reseted each time.

Example:

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

list.add(4);
list.add(3);
list.add(2);
list.add(1);

The second loop will failed because j = 3

`Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3`

Reset the values on each iteration :

while (list.size() > 1) {
    int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
AxelH
  • 14,325
  • 2
  • 25
  • 55
  • But here I want to take off two smallest values from the list and add the sum of those values onto the list. How could I achieve this? – Lata ss Dec 29 '17 at 17:43