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"

This program is similar to the Case Study in the book: Sorting an Array of Objects

the language is in java

Solutions

Expert Solution

Code:

public static <E extends Comparable<E>> void sort(ArrayList<E> list) {
       //read all the values from the list one by one and in inner loop compare with other elements in the list
for (int i = 0; i < list.size() - 1; i++) {
E MinValue = list.get(i); //creates minimum value based for the selected type of variable
int min = i; // store the index into an integer variable assigned aboev,which we use to swap in next steps
           //inner oop to compare
for (int j = i + 1; j < list.size(); j++) {
               //if the number is less than the selected then change the imin value to least value ,i.e swap values if list(j) is less than list(i)
if (list.get(j).compareTo(MinValue) < 0) {
MinValue = list.get(j);
min = j;
}
}
           //after comparing the list elements,set the values
if (min != i) {
list.set(min, list.get(i));
list.set(i, MinValue);
}
}
}

O/P:

Doubles:

Strings:


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"
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...
In Java, write the method public static void insertUnique(List l, T e), user of the ADT...
In Java, write the method public static void insertUnique(List l, T e), user of the ADT List. The method takes a list l and an element e and inserts the element at the end of the list only if it is not already there. Example 0.2. If l : A → B → C, then after calling insertUnique(l, "C"), the list does not change. Calling insertUnique(l, "D") will make l be : A → B → C → D.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT