Array out of bounds for array list comparison

1349 views java
-1

I get the following error Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 5

for(int i=0;i<list.size();i++)
        {
            if(cmp(volume.get(i),volume.get(i+1))>0)
            {
                sorted[i]=list.get(i);
            }
            else 
            {
                sorted[i]=list.get(i+1);
            }
            System.out.println(sorted[i].toString());   
        }

answered question

i+1 is out of bounds in the last iteration.

1 Answer

5

This will aware of the out of bounds exception for your work

for(int i=0; i < list.size()-1; i++) //<===list.size()-1
...

posted this

Have an answer?

JD

Please login first before posting an answer.