1

So I'm receiving the error above in the following code (my goal is to recursively sort through my objects and arrange them by area).

private static void recursionSort(ArrayList<GeometricObject> data)
        {


            ArrayList<GeometricObject> a = new ArrayList<GeometricObject>(data.size() / 2);
            ArrayList<GeometricObject> b = new ArrayList<GeometricObject>(data.size() - a.size());     // Split array into two
            //   halves, a and b
            for(int i = 0; i < data.size(); i++) 
            {
                if(i < a.size())
                    a.set(i,data.get(i));
                else             
                    b.set(i - a.size(),data.get(i));
            }

            recursionSort(a);                              // Recursively sort first
            recursionSort(b);                              //   and second half.

            int ai = 0;                                // Merge halves: ai, bi
            int bi = 0;                                //   track position in
            while(ai + bi < data.size()) {             //   in each half.
                if(bi >= b.size() || (ai < a.size() && a.get(ai).getArea() < b.get(bi).getArea())) {
                    data.set(ai + bi,a.get(ai)); // (copy element of first array over)
                    ai++;
                } else {
                    data.set(ai + bi,b.get(bi)); // (copy element of second array over)
                    bi++;
                }
            }
            System.out.println(data);
        }

Now what confuses me is my index starts at 0 (correct?) So why is my index at 0 size 0, the first object in my list (index 0) surely isnt empty? Any help or ideas? Thanks!

1
  • 1
    In your recursion code, have you considered what is the base case, what is the termination condition? Commented Oct 14, 2015 at 5:31

2 Answers 2

1

Take a look at JavaDoc for ArrayList constructor. It's said that:

Constructs an empty list with the specified initial capacity.

And now at the ArrayList#size() desctiption:

Returns the number of elements in this list.

The exception you get is caused by misunderstanding of this two methods behaviour. Even in your first iteration, you create 2 ArrayList instances via constructor with initialCapacity and you suppose, that your ArrayList is initialized with somethig, bu NO. Your lists are still empty, and call to ArrayList#size() will return you 0 even in your first iteration.

Initial capacity is used, to define internal array size to make your ArrayList more efficient, because it don't have to repeatedly reallocate the internal array, while your ArrayList grows. It doesn't initialize your list with something and you can't do any manipulations over elements if you don't add them manually.

You get the java.lang.IndexOutOfBoundsException because you are trying to call ArrayList#set(int index, E element) method for some element, which doesn't exist (your list are empty and not initialized yet).

Sign up to request clarification or add additional context in comments.

Comments

0

The issue is : You are dividing the list reclusively and when the size of list becomes 1 ,i.e data.size() = 1. The data.size() / 2 value becomes Zero.

Now your ArrayList<GeometricObject> a = new ArrayList<GeometricObject>(data.size() / 2); is of size Zero.

Hence while setting the value in Arraylist a, it is giving

Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Recursion always need one exit path otherwise it would recur indefinitely.

I guess you would need a base and termination case to solve your problem. Hint: Try using some if else condition.

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.