I am attempting to use generics that will sort an ArrayList in ascending order as extra practice for my upcoming mid-term exam. I am given the main method body, and the method header for the sorting method, and am asked to write the method body for the sort method. However, I am getting errors on a couple of lines(which I will denote with an asterisk and question mark). Where am I going wrong(I used a similar program in attempting to help me build the method body):

import java.util.ArrayList; //imports ArrayList class for method usage
public class ProblemTwo {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>(); //creates an ArrayList that can only contain Integer type values
        list.add(14);
        list.add(24);
        list.add(4);
        list.add(42);
        list.add(5);

        ProblemTwo.<Integer>sort(list); //calls the sort method to sort only Integer types

        System.out.print(list);
    }

    public static <E extends Comparable<E>> void sort(ArrayList<E> list) { //invokes sort method, passing concrete generic types from main method to formal generic types
        E currentMinValue; //creates current minimum value based on generic type
        int currentMinIndex; //used to set index for current minimum value
        for (int i = 0; i < list.size()-1; i++){ //uses a loop to assign currentMinValue and currentMinIndex
            currentMinValue = list.get(i); //assigns value at i to currentMinValue
            currentMinIndex = i; // assigns current i as minimum index

            for (int j = i + 1; j < list.size(); j++) { //uses a new loop to swap values if list(j) is less than list(i)
            *?  if (currentMinValue.compareTo(list.get(j))) {
                    currentMinValue = list.get(j);
                    currentMinIndex = j;
                }
            }

            if (currentMinIndex != i) {
            *?  list.get(currentMinIndex) = list.get(i);
            *?  list.get(i) = currentMinValue;
            }
        }
    }
}

24: compareTo returns an int -1, 0, or +1, but not a boolean
31/32: you can't have method calls like that on the LHS of an assignment. You probably want to use set to change the contents of the list

I have managed to get rid of the errors in lines 24 and 31, but I'm still having trouble with line 32:

import java.util.ArrayList; //imports ArrayList class for method usage
public class ProblemTwo {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>(); //creates an ArrayList that can only contain Integer type values
        list.add(14);
        list.add(24);
        list.add(4);
        list.add(42);
        list.add(5);

        ProblemTwo.<Integer>sort(list); //calls the sort method to sort only Integer types

        System.out.print(list);
    }

    public static <E extends Comparable<E>> void sort(ArrayList<E> list) { //invokes sort method, passing concrete generic types from main method to formal generic types
        E currentMinValue; //creates current minimum value based on generic type
        int currentMinIndex; //used to set index for current minimum value
        for (int i = 0; i < list.size()-1; i++){ //uses a loop to assign currentMinValue and currentMinIndex
            currentMinValue = list.get(i); //assigns value at i to currentMinValue
            currentMinIndex = i; // assigns current i as minimum index

            for (int j = i + 1; j < list.size(); j++) { //uses a new loop to swap values if list(j) is less than list(i)
                if (currentMinValue.compareTo(list.get(j)) > 0) {
                    currentMinValue = list.get(j);
                    currentMinIndex = j;
                }       
            if (list.get(currentMinIndex) != list.get(i)) {
                currentMinIndex = i;
                list.get(i) = currentMinValue;
            }
        }
    }
}
  • correction to above post. I am having trouble still with line 30

If you want to set the ith element of a List to some value, the correct method call is like this:
myList.set(i, someValue);

I've got the code fixed so that it sets and displays the values in ascending order. However, instead of displaying [4, 5, 14, 24, 42], it instead displays [4. 4. 4. 5. 5] when I run the program. What line of the code is causing this? I looked through the code and everything seems correct.

import java.util.ArrayList; //imports ArrayList class for method usage
public class ProblemTwo {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>(); //creates an ArrayList that can only contain Integer type values
        list.add(14);
        list.add(24);
        list.add(4);
        list.add(42);
        list.add(5);

        ProblemTwo.<Integer>sort(list); //calls the sort method to sort only Integer types

        System.out.print(list);
    }

    public static <E extends Comparable<E>> void sort(ArrayList<E> list) { //invokes sort method, passing concrete generic types from main method to formal generic types
        E currentMinValue; //creates current minimum value based on generic type
        int currentMinIndex; //used to set index for current minimum value
        for (int i = 0; i < list.size()-1; i++){ //uses a loop to assign currentMinValue and currentMinIndex
            currentMinValue = list.get(i); //assigns value at i to currentMinValue
            currentMinIndex = i; // assigns current i as minimum index

            for (int j = i + 1; j < list.size(); j++) { //uses a new loop to swap values if list(j) is less than list(i)
                if (currentMinValue.compareTo(list.get(j)) > 0) {
                    currentMinValue = list.get(j);
                    currentMinIndex = j;
                }       
            if (list.get(currentMinIndex) != list.get(i)) {
                currentMinIndex = i;
                list.set(i, currentMinValue);
            }
        }
    }
}

}

You need to start debugging the code (which is pretty small IMO). As a starting point, what's the smallest sample with which you can reproduce this problem? What happens when you run this with inputs [5, 4]?

Which IDE are you using to write code? Can you try out the "debug" functionality of that IDE to step through your code and check the values at every step? If not using an IDE, can you try inserting some println statements to dump the arraylist values after each step?

I tried running the code using only list.add(5) and list.add(4). When I run it, my display is:
[4, 4].
My code is still the same. As far as I can see, I can't find any error, and my sort method is designed similar to selection sort. Is the problem located in the if(currentMinIndex != i) loop?

If you are supoosed to be swapping elements in the list then I would expect to see two calls to set(...)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.