Question

In: Computer Science

(Sort ArrayList) Write the following method that sorts an ArrayList:   public static <E extends Comparable<E>> void...

(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"

Solutions

Expert Solution

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);

    }
}

============


Related Solutions

(Sort ArrayList) Write the following method that sorts an ArrayList: public static <E extends Comparable<E>> void...
(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" This program is similar to the Case Study in the book: Sorting an Array of Objects the language is in java
Question 17 (1 point) What is the return type of this method? public <E extends Comparable<E>>...
Question 17 (1 point) What is the return type of this method? public <E extends Comparable<E>> int theMethod(E arg) Question 17 options: E Comparable int arg Question 18 (1 point) What is the primary benefit of writing generic classes and methods? Question 18 options: Generic classes and methods are more type-safe Generic classes and methods are shorter Generic classes and methods are faster Generic classes and methods are backwards compatible Question 19 (1 point) What is wrong with the following...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[]...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[] args) { //Fixme(2) create an ArrayList of integers and name the ArrayList list. //Fixme(3) add the following numbers to the list: 10, 15, 7, -5, 73, -11, 100, 20, 5, -1    displayList(list); System.out.println(); displayListBackwards(list);       }    public static void displayList(ArrayList<Integer> list) { for(Integer i: list) System.out.print(i + " ");    } //Fixme(4) define a method displayListBackwards, which takes an ArrayList as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT