Sam Ghanem
Sam Ghanem

Reputation: 49

java programming Exception in thread "main" what is this error?

I wrote this program to find the item and then to remove all elements which are smaller than the item. The are no compilation errors, but when I run the program, the following message appears.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.remove(ArrayList.java:492)
    at bb.deleteValues(bb.java:15)
    at bb.main(bb.java:33)

Process completed. "

Code:

import java.util.ArrayList;
import java.util.Scanner;


public class bb {
public static   boolean deleteValues(ArrayList<Integer> list, int item)
    { int p =list.indexOf(item);
        if(!list.contains(item))
            return false;
            else 
                for(int i=p; i<list.size();i++ )
                {int n=list.get(i);
                if (n>list.get(i+1))
                list.remove(p);
                list.remove(i+1);

                }
                return true; 

    }

    public static void main(String[] args) {
        ArrayList<Integer> a= new ArrayList<Integer>(8);
        a.add(3);
        a.add(10);
            a.add(4);
            a.add(5);
            a.add(3);
            a.add(2);
            a.add(8);
            a.add(6);
            a.add(4);
        if(deleteValues(a,4))
            for(int x : a)
                System.out.print(x+ " ");



    }
}

Upvotes: 1

Views: 4319

Answers (4)

Harish N
Harish N

Reputation: 19

import java.util.ArrayList;
import java.util.Scanner;

    public class Ex {
    public static   boolean deleteValues(ArrayList<Integer> list)
        {

                    for(int i=0; i<list.size();i++ )
                    {int n=list.get(i);
                    if (n>list.get(i+1))
                  //  list.remove(i);
                    list.remove(i+1);

                    }
                    return true; 

        }

        public static void main(String[] args) {
            ArrayList<Integer> a= new ArrayList<Integer>(8);
            a.add(3);
            a.add(10);
               a.add(4);
                a.add(5);
               a.add(3);
                a.add(2);
              a.add(8);
               a.add(6);
              // a.add(4);
            if(deleteValues(a))
                for(int x : a)
                    System.out.print(x+ " ");
        }
    }

you are adding one extra object to the arraylist with itz size specified and you are using index values wrongly in the deleteValues method so plz check with the both codes.

import java.util.ArrayList;
import java.util.Scanner;


public class Ex {
public static   boolean deleteValues(ArrayList<Integer> list, int item)
    { int p =list.indexOf(item);
        if(!list.contains(item))
            return false;
            else 
                for(int i=0; i<list.size();i++ )
                {int n=list.get(i);
                if (n>list.get(i+1))
                list.remove(p);
                //list.remove(i+1);

                }
                return true; 
    }

    public static void main(String[] args) {
        ArrayList<Integer> a= new ArrayList<Integer>(8);
        a.add(3);
        a.add(10);
            a.add(4);
            a.add(5);
            a.add(3);
            a.add(2);
            a.add(8);
            //a.add(6);
            a.add(4);
        if(deleteValues(a,4))
            for(int x : a)
                System.out.print(x+ " ");
    }
}

Upvotes: 0

Eran
Eran

Reputation: 393946

Your deleteValues method loops i from p to list.size()-1, but the loop's body contains list.get(i+1) and list.remove(i+1), so when i==list.size()-1, list.get(i+1) and list.remove(i+1) will attempt to access an item from an index not present in the list, which is the cause of the exception.

Removing the elements smaller than the passed item requires iterating over all the elements in the list, and comparing each one to the passed item. Note that when you remove the i'th element from the list, the (i+1)'th element becomes the new i'th element. That's why i should be incremented only if you don't remove an element from the list.

public static boolean deleteValues(ArrayList<Integer> list, int item)
{
    int p = list.indexOf(item);
    if(p<0)
        return false;
    for(int i=0; i<list.size();) {
        if (list.get(i) < item) {
            list.remove(i);
        } else {
            i++;
        }
    }
    return true;   
}

Upvotes: 2

Shadow
Shadow

Reputation: 4016

The problem is in your deleteValues function, you loop while i is less than the size of the ArrayList, but add 1 when you call the get function, which can and will cause an IndexOutOfBoundsException, because it can make i equal to the size of the ArrayList, which is invalid. Change your for loop, and remove the cases where you have i+1, like so:

for(int i=p; i<list.size();i++ )
{
    int n=list.get(i);

    if (n>list.get(i))
        list.remove(p);

    list.remove(i);

}

Also, if your function is meant to remove all copies of a certain value, the code can be much, much simpler, and clearer:

public static boolean deleteValues(ArrayList<Integer> list, int item)
{
    if (list.contains(item))
    {
        for (int i = 0; i < list.size(); i++)
            if (list.get(i) == item)
                list.remove(i);
        return true;
    }
    return false;
}

Upvotes: 0

Santhosh
Santhosh

Reputation: 8207

list.remove(i+1); will exceed the list size. For ex, when you are passing the 4th element, it will search for the element at 5th postion

Upvotes: 0

Related Questions