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!