1

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+ " ");



    }
}

4 Answers 4

2

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;   
}
Sign up to request clarification or add additional context in comments.

1 Comment

I agree,I don't know where I was lost while posting that comment! +1 for you.
0

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

1 Comment

The initial capacity of 8 is irrelevant, since the size of an ArrayList is increased automatically when required.
0

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;
}

Comments

0
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+ " ");
    }
}

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.