File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Binary Search for Ints & Strings

 
Luke Stamper
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found out my program was supposed to be generic and not two separate searches. Ugh. Can anyone help me get started or point me in the right direction?

Thanks in advance.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you declare your method something like


then whatever is passed in, you can call without ever having to know what the actual class is.
 
Winston Gutkowski
Bartender
Pie
Posts: 9484
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luke Stamper wrote:I found out my program was supposed to be generic and not two separate searches. Ugh. Can anyone help me get started or point me in the right direction?

Just to add to what Jeff said, you will need to make your numeric array an Integer[], not an int[]. You could provide a converter though.

Winston
 
Luke Stamper
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i'm lost and in over my head. I think i'm on the right track but I can't figure out how to take my generic arrays and pass them into the search method. Also I can't figure out how to create a for loop similar to lines 15-23 that will work with my generic arrays.
Am I doing anything right?



 
Luke Stamper
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me with this program? I am so lost.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the fact that you are supposed to say
<T extends Comparable<? super T>>,
what is going wrong?

I think you are making the mistake of trying too much all at once. Forget all about ints for the time being, but create an Integer[], like thisYou don’t need to sort anything. If you are learning searching, then stick to the searching and leave out the sorting. You can always add the sorting later.

I think you need to write out the algorithm with a pencil, remembering there are 6 variables, not the 5 you have:
  • low
  • high
  • mid
  • array
  • sought
  • index
  • You need to pass the array as a parameter to that method, which should read like thisNote
  • 1: It is the method which should be generic, not the class
  • 2: The method does not use any instance variables, so ought to be static.
  • 3: You don’t want any instances of this class, hence the private constructor, which you never call.
  • 4: index = mid; will appear somewhere inside that loop.
  • 5: You can use index < 0 or similar as part of your loop continuation condition
  • 7: You might use a do loop instead of the while loop.
  • [edit]Add /** comment */ to binary search method[/edit]
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47286
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is an alternative, that you can put index = -mid - 1; at the end of each iteration of the loop. Then rather than returning -1 for not found, it returns a negative number hinting at the nearest index where you would have found the element sought.
     
    Luke Stamper
    Ranch Hand
    Posts: 64
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I took a different direction. I am so close to being done. The only thing I have to correct is the testing section L32-45. I am getting this error message.

    Integer test array contains...
    -4 -2 0 2 4 6

    String test array contains...
    Alpha Bravo Charlie Delta Echo Foxtrot 3
    Searcher@1e5e2c3is at index 3 in the array
    Searcher@18a992f is at index 3 in the array




    Is it possible to do something like this in Generics for this program?

    String[] searchFruits = new String[] {"lemon", "apple", "banana", "grapes", "blueberry","papaya"};
    System.out.println("");
    System.out.println("\nChecking fruits...");
    int results;
    for (int key = 0; key < searchFruits.length; key++)
    {
    results = BinarySearchString.searchString(names, searchFruits[key]);

    if (results >= 0) {

    System.out.println(searchFruits[key] + " is at index " + results);
    }
    else {
    System.out.println(searchFruits[key] + " is not in the array.");
    }
    }
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47286
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Apart from the fact that you ought to say ? super T as I told you yesterday, what else is there?
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic