Question

"Java question" Implement a generic method that returns the minimum element in an array.        public...

"Java question"

Implement a generic method that returns the minimum element in an array.

       public static<EextendsComparable<E>> E min(E[ ] list)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class SelectionSortGenerics implements Comparable<E> {

    private <E> void swap(E[] a, int i, int j) {
        if (i != j) {
            E temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }

    public <E> void selectionSort(E[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            // find index of smallest element
            int smallest = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j].compareTo(a[smallest])<=0) {
                    smallest = j;
                }
            }

            swap(a, i, smallest);  // swap smallest to front
        }
    }

    public static void main(String[] args){
        SelectionSortGenerics firstsort = new SelectionSortGenerics();

        Integer[] arr = {3,4,1,5};
        System.out.println("before sorting int: "+ Arrays.toString(arr));
        firstsort.selectionSort(arr);
        System.out.println("After sorting int : "+Arrays.toString(arr));
         String[] arr1= {"acd","ded","dal","bad","cle"};
         System.out.println("before sorting String: "+ Arrays.toString(arr1));
         firstsort.selectionSort(arr1);
         System.out.println("After sorting String : "+Arrays.toString(arr1));
         Character[] arr2= {'c','e','a','d','c'};
         System.out.println("before sorting char: "+ Arrays.toString(arr2));
         firstsort.selectionSort(arr2);
         System.out.println("After sorting char : "+Arrays.toString(arr2));
    }
}


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
"Java question" Implement a generic method that returns the minimum element in an array.        public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a short answer question Implement the following method that returns the maximum element in...

    This is a short answer question Implement the following method that returns the maximum element in an array. (6 pts) public static <T extends Comparable<E>> E MaxInArray(T[] array) { … }

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • this is a generic class in java how implement sort method   public class ArrayBag<T> {   ...

    this is a generic class in java how implement sort method   public class ArrayBag<T> {    private T[] data;    private int manyItems; // TODO change and implement this method to use the generic type    // Sort the elements based on the comparator passed to this method    // You must use Selection Sort algorithm    public void sort(Comparator<T> comp) {                     } }

  • 1. Implement generic insertion sort public static <E extends Comparable<E>> void insertionSort(E[] list){ //implement body }...

    1. Implement generic insertion sort public static <E extends Comparable<E>> void insertionSort(E[] list){ //implement body } 2. Implement generic bubble sort public static <E extends Comparable<E>> void bubbleSort(E[] list){ //implement body } 3. Implement generic merge sort public static <E extends Comparable<E>> void mergeSort(E[] list){ //implement body } 4. Implement generic heap sort public static <E extends Comparable<E>> void heapSort(E[] list){ //implement body }

  • JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a...

    JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a Utils class that takes as parameter a collection of Pair<K,V> objects and returns a new collection object (use ArrayList<...>) with the pair elements from collection col sorted in ascending order. For comparing pairs, the K type must implement Comparable<....> Use the proper type constraints. b) Write a main() function in Utils.java that tests the sortPairCollection() with K=String and V=Integer.

  • java Write a generic method that takes an ArrayList of objects (of a valid concrete object...

    java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • Java Programming Exercise 32.13 Follow the instructions in the textbook, also shown here: 32.13 (Generic parallel merge...

    Java Programming Exercise 32.13 Follow the instructions in the textbook, also shown here: 32.13 (Generic parallel merge sort) Revise Listing 30.10, ParallelMergeSort.java, to define a generic parallelMergeSort method as follows: public static <E extends Comparable<E>> void parallel MergeSort(E list) In the main) method, create an array of integers, print the array of integers, then use the parallelMergeSort() on the array, and print the sorted array. Then, repeat the steps for an array of strings Exercise 32.13 Follow the instructions in...

  • Implement a generic method genericMax that finds the maximum object in a list. The generic constraints...

    Implement a generic method genericMax that finds the maximum object in a list. The generic constraints should make the method as flexible as possible. static public <TYPEVARIABLES/TYPEBOUNDS> TYPEVARIABLE/TYPEBOUNDS genericMax(List<TYPEVARIABLE/TYPEBOUNDS> lst, Comparator<TYPEVARIABLES/TYPEBOUNDS> comparator) { /* YOUR CODE */ } Your Answer:

  • public class SelectionSorter { //Returns the index of the largest element in the arrayOfIntegers, beginning from...

    public class SelectionSorter { //Returns the index of the largest element in the arrayOfIntegers, beginning from the fromIndex. public static Integer[] selectSort(Integer[] incoming) {        Integer[] ret = new Integer[incoming.length]; for (int i = 0; i < incoming.length; i++) {            ret[i] = incoming[i];        }        int temp = 0;        for (int i = 0; i < ret.length - 1; i++) {            if (ret[i] > ret[i + 1]) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT