Ok, first and foremost I'm not a professional programmer by any means. That being said, I'm working on some -HOMEWORK- and don't expect anyone to hand feed me. But I am having trouble solving a problem using Generics to write a Binary Search method. The problem I have is that the code is function for an integer array, but I can't get it to work with a String array, which mostly defeats the purpose of using Generics to search arrays. I'm certain this is to do with the compareTo interface conflicting with the string as it doesn't apparently want to evaluate the strings. This method does work for an Integer Array. Included is both the code and the book in PDF that I'm using for class. Remember, I'm working with the BinarySearch. I cannot use an arraylist, the method is setup the way it needs to be setup, the code within it is what must work when calling the method binarySearch(stringArray);
Code:
import java.util.ArrayList;
import java.util.Arrays;
/**Sorting is chapter 23 which we have not covered?? I didn't know the difference between Binary and Linear Searches **/
public class Test {
public static void main (String args[]) {
//Create a String Array with 5 values
String[] stringArray = {"Jesse", "Jason", "Johnathon", "Jennifer", "SoAndSo"};
//Create an Integer Array with 8 values
Integer[] intArray = {23,45,21,55,234,1,34,90};
/**19.4:**/
//19.4: search the array stringArray for the key "Jesse" and output the index location to the console.
linearSearch(stringArray, "Jesse");
//search the array IntegerArray for the key 21 and output the index location to the console.
linearSearch(intArray, 21);
/**19.7**/
binarySearch(intArray, 234);
binarySearch(stringArray,"Jesse");
/**19.9**/
//Create an ArrayList of the type integer and add 3 unsorted int to the list 19.9
ArrayList<Integer> integer = new ArrayList<>();
integer.add(25);
integer.add(15);
integer.add(33);
//Create an ArrayList of the type double and add unsorted doubles to the list 19.9
ArrayList<Double> duble = new ArrayList<>(); //double is a keyword for java, used "duble" instead.
for (double i = 10; i >= 0; i = i - 0.5) {//first for loop I've ever made using double as an i.
if (i%1 == 0) {//if i is evenly divided by 1 do this.
duble.add(i*0.5);
} else {//else do this
duble.add(i*2.5);
}
}
//using sort() for an integer 19.9
System.out.println("ArrayList 'integer' before sort: " + integer);
sort(integer);//sort the list "integer"
System.out.println("ArrayList 'integer' after sort: " + integer + "\n");
//Using sort() for a double 19.9
System.out.println("ArrayList 'duble' before sort: " + duble);
sort(duble);//Sort the list "duble"
System.out.println("ArrayList 'duble' after sort: " + duble + "\n");
}
//19.4 - (Generic linear search) Implement the following generic method for linear search.
public static <E extends Comparable<E>> int linearSearch(E[] list, E key) {
for(int i = 0; i < list.length; i++){
if(key.equals(list[i])){
System.out.println("Key found at index: " + key + "\n");
return i;
}
}
System.out.println("Key not found\n");
return -1;
}
//19.7 - (Generic binary search) Implement the following method using binary search.
public static <E extends Comparable<E>> int binarySearch(E[] list, E key) {
Arrays.sort(list);
System.out.println("Array Sorted and is now: ");
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
System.out.println("");
if (list.length == 0) {
return -1;
}
int low = 0;//Start at 0
int high = list.length-1;//index's length including 0
while (low <= high ) {
int middle = (low+high) /2; //the middle index
if (list[middle].compareTo(key) == 0){
System.out.println("Binary search results - Integer found at Index: " + middle + "\n");
return middle;
} else if (list[middle].compareTo(key) == 1){
high = middle-1;
middle = (low+high)/2;
} else if (list[middle].compareTo(key) == -1) {
list[middle].compareTo(key);
low = middle+1;
middle = (low+high)/2;
}
}
System.out.println("Key not found\n");
return -1;
}
//19.9 - (Sort ArrayList) Write the following method that sorts an ArrayList:
//Code from Listing 19.4 Pg 745 modified for ArrayList.
public static <E extends Comparable<E>> void sort(ArrayList<E> list) {
E currentMin;
int currentMinIndex;
for (int i = 0; i < list.size() - 1; i++) {
// Find the minimum in the list[i+1..list.length-2]
currentMin = list.get(i);
currentMinIndex = i;
for (int j = i + 1; j < list.size(); j++) {
if (currentMin.compareTo(list.get(j)) > 0) {
currentMin = list.get(j);
currentMinIndex = j;
}
}
// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMinIndex != i) {
list.set(currentMinIndex, list.get(i));
list.set(i, currentMin);
}
}
}
}
Attachments
Last edited: