How to pass the objects of two arraylists to one a

2019-09-09 00:47发布

问题:

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!

回答1:

The List interface defines the set(int index, E element) (E = GeometricObject in the present case). Therefore, the four lines you're having trouble with should be rewritten as follows:

a.set(i, data.get(i));
b.set(i - a.size(), data.get(i));
data.set(ai + bi, a.get(ai));
data.set(ai + bi, b.get(bi));

Hope this helps...

Jeff



回答2:

You don't have to implement sort method to sort an Arraylist with your custom objects. You can make use of Collections.sort(arraylist) to do the same.

To make use of the same you need to either use Comparator or Comparable interface as per your need.

If you use Comparable interface, you code will look like :

public class GeometricObject implements Comparable<GeometricObject>
{
   // member variables

   // other methods

    @Override
    public int compareTo(GeometricObject comparesToObject) 
    {
        // wil sort in ascending order. 
        return this.getArea()-comparesToObject.getArea();

        // Use the commented line for descending order
        // return comparesToObject.getArea() - this.getArea();

        // Use return Float.compare(area1, area2) if area is of type float.
   }
}

// This will now sort your data Arraylist.
Collections.sort(data);