So, I am trying to print out my arraylists in order of the objects area. I cannot however seem to figure out how to pass the values of objects to one another at an index. (I must do it recursively). Here is my code thusfar
private static void recursionSort(ArrayList<GeometricObject> data)
{
if(data.size() <= 1) return; // Base case: just 1 elt
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.indexOf(i) = data.get(i);
else
b.get(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.get(ai + bi) = a.get(ai); // (copy element of first array over)
ai++;
} else {
data.get(ai + bi) = b.get(bi); // (copy element of second array over)
bi++;
}
}
}
My issue is with the lines
a.indexOf(i) = data.get(i);
b.get(i - a.size()) = data.get(i);
data.get(ai + bi) = a.get(ai);
data.get(ai + bi) = b.get(bi);
For example I can't figure out how to get say the index of a at 0 to equal my list's (data) index of 0. If these were arrrays, i would know what to do, so let me use that as an example to show you what I'm trying to accomplish just via arraylists
a[i] = data[i]; // First line in block above
data[ai + bi] = b[bi]; // Last line in block above
Any help would be greatly appreciated. I've went through almsot every method found in my book's list of ArrayList Class methods and none have the desired effect I'm looking for. Thanks!