In: Computer Science
(Sort ArrayList) Write the following method that sorts an ArrayList:
public static <E extends Comparable<E>>
void sort(ArrayList<E> list)
Write a program to test the method with three different arrays:
Integers: 2, 4, and 3;
Doubles: 3.4, 1.2, and -12.3;
Strings: "Bob", "Alice", "Ted", and "Carol"
SOURCE CODE: *Please follow the comments to better understand the code. **Please look at the Screenshot below and use this code to copy-paste. ***The code in the below screenshot is neatly indented for better understanding. import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; // Create the class public class TestGenerics { public static <E extends Comparable<E>> void sort(ArrayList<E> list) { // we can use the method in collections class Collections.sort(list); } // TEST THE METHOD public static void main(String[] args) { Integer[] integers={2,4,3}; Double[] doubles={3.4,1.2,-12.3}; String[] strings={"Bob", "Alice", "Ted","Carol"}; // sort integers and print ArrayList<Integer> numbers=new ArrayList<>(Arrays.asList(integers)); System.out.println("Before Sort: "+numbers); sort(numbers); System.out.println("After Sort: "+numbers); // sort doubles and print ArrayList<Double> doubleal=new ArrayList<>(Arrays.asList(doubles)); System.out.println("Before Sort: "+doubleal); sort(doubleal); System.out.println("After Sort: "+doubleal); // sort strings and print ArrayList<String> stringsal=new ArrayList<>(Arrays.asList(strings)); System.out.println("Before Sort: "+stringsal); sort(stringsal); System.out.println("After Sort: "+stringsal); } }
============