StevieV StevieV - 2 years ago 149
Java Question

Swapping Two Objects In An ArrayList

I am trying to swap 2 objects within an ArrayList. I am creating a new list where the objects are in their new and swapped places and then just overwrite the old list entirely with this new, "swapped list." However, I am having trouble adding the objects of one list to a new list.

About the program: The program takes input from a text file somewhere on the drive, translates the data within said file into objects (circles and rectangles, which are extensions of GeometricObject) and then adds those objects to an ArrayList called "objectList" which can be seen used throughout this program.

The problem is here:

public static <E extends Comparable<E>> void swapCells
(ArrayList<E> objectList, int left, int right) {

/* The user may enter the two indices, "left,"
* and, "right," in any order which they desire.
* Because of this it will be necessary to determine
* which is larger or "right" index, and which is
* the smaller or "left" index

int temp;
ArrayList<GeometricObject> swappedList = new ArrayList<GeometricObject>();

if (left > right) {

// Exchanges left and right
temp = left;
left = right;
right = temp;


for (int i = 0; i < objectList.size(); i++) {

if (i == left) {
System.out.println( swappedList.get(i).getArea());
} else {
swappedList.add((E) objectList.get(i));



} // End of swapCells

I get the error:

The method
in the type
is not applicable for the arguments

The error is specifically at,
and also
wappedList.add((E) objectList.get(i));

Answer Source

I do not believe this is exactly the answer your looking for, but it may help.

If you typecast with GeomtricObject you will get a functioning code, however, this defeats the purpose of using a generic if your just going to force it into a Geometric Object.

You also need to add else if to get the left object swapped to right position

You may also want to print out the swappedList to confirm that the action has been completed.

        for (int i = 0; i < objectList.size(); i++) 
                if (i == left) {
                    swappedList.add((GeometricObject) objectList.get(right));
                }else if (i == right)
                    swappedList.add((GeometricObject) objectList.get(left));
                else {
                    swappedList.add((GeometricObject) objectList.get(i));

EDIT 2: The following will aid you in the operation you were looking for in generics.

You will need to make a temp and cast it to E. You will also need to use the following code as well in its correct arguments and/or form / notation.

        E temp
        List.set(____ , _____)
        List.get(____ )

If your still having trouble with this swap function look at one that is not Generic.

EDIT 3: You most likely have the same problem as I do, and you also need to sort the Generic. You can use the selectionSort Method below to help you on the assignment. You will need to change the method so that it works for an ArrayList instead of a Array. This means you will need to make use of the suggestions in EDIT 2 to modify the code below. You may also need to use the compareTo Method.

        private static void selectionSort(int[] list, int low, int high) { 
        if (low < high) { 
          int posMax = high; 
          int theMax = list[high]; 
          for (int i = 0; i < high; i++) { 
            if (list[i] > theMax) { 
              theMax = list[i]; 
              posMax = i; 
            }// if 
          }// for 
        list[posMax] = list[high]; 
        list[high] = theMax; 
        selectionSort(list, low, high - 1); 
        }// if 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download