Home > Software engineering >  How to write a method that sorts an ArrayList in java using a generic Comparable type
How to write a method that sorts an ArrayList in java using a generic Comparable type

Time:04-02

I am trying to write a method to sort an array of integers.

The method must utilize the Comparable interface. I've designed a test case and attempted to write a solution.

Expected:

Enter 10 integers: 3 4 12 7 3 4 5 6 4 7
The sorted numbers are 3 3 4 4 4 5 6 7 7 12

Actual:

Enter 10 integers: 1
Enter 10 integers: 2
Enter 10 integers: 34
Enter 10 integers: 4
Enter 10 integers: 3
Enter 10 integers: 2
Enter 10 integers: 1
Enter 10 integers: 4
Enter 10 integers: 5
Enter 10 integers: 6
The sorted numbers are: 3 5 4 1 2 2 1 4 5 6

My attempt

public class Test2 {
    
    public static void main(String args[]){
        ArrayList<Integer> array = new ArrayList<>();
        Scanner input = new Scanner(System.in);
        for(int i = 0; i < 10; i  ){
            System.out.print("Enter 10 integers: ");
            int integer    = input.nextInt();
            array.add(integer);
        }
        sort(array);
        System.out.print("The sorted numbers are ");
        for(int i = 0; i <= array.size()-1; i  ){
            System.out.print(array.get(i) " ");
        }
    }

    public static <E extends Comparable <E>> void sort(ArrayList<E> list) {
        int n = list.size();
    
        for(int  i = 1; i < n-1; i  ) {
            for(int j = 0; j < n-i-1; j  ) {
                if(list.get(i).compareTo(list.get(i 1)) < 0) {
                    E temp = list.get(i);
                    list.set(j, list.get(j 1));
                    list.set(j   1, temp);
                }
            }
         }
     }
}

Any help with this would be greatly appreciated.

CodePudding user response:

It doesn't work in the same way, as you can assign the value to the element of an array.

With a list you have to use method set:

list.set(j   1, temp);

Also, avoid using concrete types like ArrayList, write your code against interfaces like List. Now your method sort() is limited to accept only an instance ArrayList and not able to deal with a LinkedList.

CodePudding user response:

import java.util.ArrayList;
import java.util.Scanner;


public class Exercise19_09 {
    
    public static void main(String args[]){
        ArrayList<Integer> array = new ArrayList<>();
        Scanner input = new Scanner(System.in);
        System.out.print("Enter 10 integers: ");
        for(int i = 0; i < 10; i  ){
            int integer    = input.nextInt();
            array.add(integer);
        }
        sort(array);
        System.out.print("The sorted numbers are ");
        for(int i = 0; i <= array.size()-1; i  ){
            System.out.print(array.get(i) " ");
        }
    }

    public static <E extends Comparable <E>> void sort(ArrayList<E> list){
        int n = list.size();
    
        for(int  i = 0; i < n-1; i  ){
            for(int j = 0; j < n-i-1; j  )
                if(list.get(j).compareTo(list.get(j 1)) > 0){
                    E temp = list.get(j);
                    list.set(j, list.get(j 1));
                    list.set(j   1, temp);

                }
        }
    }
}
  • Related