Question
Need help Generic Methods Use of generic parameters gives us type erasure, that is, the symbol 'E', 'T', 'K', 'V' (or whichever may be used),
Need help
Generic Methods
Use of generic parameters gives us "type erasure," that is, the symbol 'E', 'T', 'K', 'V' (or whichever may be used), is replaced at compile time with the data type indicated within diamond syntax. Generics can be applied to individual methods in addition to classes. In this lab, our methods, but not our class, will use generic parameters.
Instructions for GenMethods.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 = new Random(340L); ), and it should do 30 of these swaps (this number was chosen arbitrarily by us for testing purposes).
public static void shuffle(ArrayList list)
(3) Write the following method that returns the largest element in an ArrayList:
public static > E max(ArrayList list)
(4) Implement the following generic method for linear search.
public static > int linearSearch(E[] list, E key)
(5) Implement the following method that returns the maximum element in an array:
public static > E max(E[] list)
(6) Implement a generic method that returns the maximum element in a two-dimensional array.
public static > E max(E[][] list)
(7) Write the following main() method that tests the above methods following this guide:
Read in a number n that represents the number of elements in the lists. Read in n elements to initialize and fill an array of Integers named 'list' while simultaneously initializing a linked list of Integers named 'linked' from the same input. Print 'list'. (use Arrays.toString(array)) Print 'linked' (just put 'linked' into print statement) Read in k key value to search for in list. Call linearSearch(list, k) and print the result: Key k was found at position result, or Key k was not found. Call max(list) and print the result: 'Result' is the max element Read in an integer m for first dimension of a 2-D array. Read in an integer p for second dimension of a 2-D array. Initialize a 2-D array using m and p named 'list2' Read in m x p elements to fill 'list2'. Print 'list2'. You can not just use a single print statement, but will instead need to implement nested for loops. (Format: rows of data on separate lines with a space in between each data) Example: 1 2 3 4 2 3 4 5 3 4 5 6 Call max(list2) and print the result: 'Result' is the max element Instantiate an ArrayList of type Integer named 'alist' from 'linked' (meaning 'alist' is a copy of 'linked') Print out 'alist' using System.out.println(alist); Call removeDuplicates using 'alist' as the parameter Print the now unique 'alist' using System.out.println(alist); Call shuffle using 'alist' as the parameter. Print 'alist' again using System.out.println(alist); Find the max element of 'alist' and print: 'Result' is the max element
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started