Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type.
Implement the following generic linear search method and write a client program to display results:
(Here is the header)
public static <E extends Comparable<E>> int search(E[] list, E key) {
// add codes here
}
Write an overloaded search method to process the collection type.
The following code is included for the problem:
//This program runs in finding the location (index) of a
name and number.
//For this question, convert the arrays to an ArrayList and write
another overloaded search method to do the same.
public class MainClient {
public static void main(String[] args) {
Integer[] list = {2, 13, 41, 17,
-12, 43, -21, 99, 0};
String[] names = {"Corey", "Zeal",
"Hannah", "Ain", "Star", "Larry", "who"};
// use enhanced for to print the list and names
for (int i =0; i < list.length;
i++) {
System.out.print(list[i] + ", ");
}
System.out.println();
System.out.println("looking for 99
index = " + search(list, 99));
System.out.println("looking for
\"who \" index= " + search(names, "who"));
}
// use the method provided to create two ArrayList
from the above array list and names
public static <E extends
Comparable<E>> int search(E[] list, E key) {
for (int i = 0; i < list.length;
i++)
if
(list[i].equals(key))
return i;
return -1;
}
// write a generic search method that takes the
generic collection parameters instead of a generic array
// inside this search method use an enhance for-loop
to find the key
public static <T> void fromArrayToCollection(T
a[], Collection<T> c) {
for (T x: a)
c.add(x);
}
}
The search(….) method returns the location (array index loc) for the <E> key when a match found,
and -1 if there is no match.
Result Sample: Search for 199, and “who” for the array search; -12 and “where” for the collection type search.
2 13 41 17 -12 43 -21 199 -12 0
Corey Zeal Hannah Ain Star Larry who where
----- using generic/array search -------------
looking for the index of 199 = 7
looking for the index of "who": 6
----- using generic/collection search -------------
[Corey, Zeal, Hannah, Ain, Star, Larry, who, where]
looking for the index of "who " index = 6
[2, 13, 41, 17, -12, 43, -21, 199, -12, 0]
looking for the index of -12 = 4
PROGRAM :
package arraylist;
import java.util.*;
// Defines class A6Q5
public class A6Q5
{
// main method definition
public static void main(String []s)
{
// Creates an Integer type array
Integer [] list = {2, 13, 41, 17, -12, 43, -21, 99, 0};
// Creates an String type array
String [] names = {"Austin", "Max", "Spencer", "Mattew",
"Blake",
"Dakota", "who", "where"};
// Creates an array list of type Integer
ArrayList<Integer> listInt = new
ArrayList<Integer>();
// Creates an array list of type String
ArrayList<String> listNames = new
ArrayList<String>();
// Displays integer array
System.out.print("\n Integer Array: ");
// Loops till end of the list
for(int i = 0; i < list.length; i++)
System.out.print(list[i] + ", ");
// Displays integer array
System.out.print("\n String Array: ");
// Loops till end of the names
for(int i = 0; i < names.length; i++)
System.out.print(names[i] + ", ");
System.out.println("\n\n ------ Using generic / Array search
------");
// Calls the method to search 99 in list, and displays the returned
result
System.out.println("Looking for 99 index = " + search(list,
99));
// Calls the method to search "who" in list, and displays the
returned result
System.out.println("Looking for \"who\" index = " + search(names,
"who"));
// Calls the method to convert integer array to array list of
type integer
fromArrayToColection(list, listInt);
// Calls the method to convert string array to array list of type
string
fromArrayToColection(names, listNames);
// Displays the integer type array list
System.out.println("\n Array list of Integer: " + listInt);
// Displays the string type array list
System.out.println("\n Array list of String: " + listNames);
System.out.println("\n ------ Using generic / Collection search
------");
// Calls the method to search -12 in array list of type
integer
// and displays the returned result
System.out.println("Looking for -12 index = " + search(listInt,
-12));
// Calls the method to search "where" in array list of type
integer
// and displays the returned result
System.out.println("Looking for \"where\" index = " +
search(listNames, "where"));
}// End of main method
// Generic method to search parameter key in the array
public static <E extends Comparable<E>> int search(E
list[], E key)
{
// Loops till number of elements in the array
for(int i = 0; i < list.length; i++)
// Checks if current index position data is equals to parameter
key
if (list[i].equals(key))
// Returns loop variable as found index
return i;
// Returns -1 for not found
return -1;
}// End of method
// Generic method to search parameter key in the array
list
public static <M extends Comparable<M>> int
search(ArrayList<M> list,
M key)
{
// Loops till number of elements in the array list
for(int i = 0; i < list.size(); i++)
// Checks if current index position data is equals to parameter
key
if (list.get(i).equals(key))
// Returns loop variable as found index
return i;
// Returns -1 for not found
return -1;
}// End of method
// Generic method to convert first parameter array to second
parameter array list
public static <T extends Comparable<T>> void
fromArrayToColection(T a[], Collection<T> c)
{
// Enhanced for loop to extract each element from the array
for (T x : a)
// Adds the current element to array list
c.add(x);
}// End of method
}// End of class
Sample Output:
Integer Array: 2, 13, 41, 17, -12, 43, -21, 99, 0,
String Array: Austin, Max, Spencer, Mattew, Blake, Dakota, who,
where,
------ Using generic / Array search ------
Looking for 99 index = 7
Looking for "who" index = 6
Array list of Integer: [2, 13, 41, 17, -12, 43, -21, 99, 0]
Array list of String: [Austin, Max, Spencer, Mattew, Blake, Dakota, who, where]
------ Using generic / Collection search ------
Looking for -12 index = 4
Looking for "where" index = 7
Get Answers For Free
Most questions answered within 1 hours.