Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-23-2007, 07:04 PM
Member
 
Join Date: Nov 2007
Posts: 20
Gilgamesh is on a distinguished road
Search
I have a string vector having strings in an alphabetical order. I use contains() to check if it contains specific strings. Obviously this search is linear, how can I convert it into string binary search?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-23-2007, 09:40 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
how can I convert it into string binary search
You can write a contains or search method in the class that is working with the Vector, or subclass Vector and: add a method to do binary search or override the contains method or do both, calling the binary search method from contains. You can set it up any way you like when doing this.
Code:
class Pseudo { Vector vector; /** instance method */ String contains(String target) { // do binary search for target in vector } } class MyVector extends Vector { /** override contains method */ public boolean contains(Object elem) { // do your binary search here // or call a local method below // to do the binary search } /** with or without overriding contains */ private_or_public boolean binaryContains(Type arg) { // binary search for arg } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-25-2007, 07:05 PM
Member
 
Join Date: Nov 2007
Posts: 20
Gilgamesh is on a distinguished road
int binarySearch (String word, Vector <String> AlphabetizedListVector){
int lefthand=0;
int righthand=AlphabetizedListVector.size() - 1;
while (lefthand <= righthand){
int mid =( lefthand + righthand ) / 2;
int cmp = word.compareToIgnoreCase(AlphabetizedListVector.ge t(mid));
if (cmp == 0) return mid;
if (cmp < 0){
righthand = mid-1;
}else{
lefthand = mid+1;
}
}
return -1;
// System.out.println("The word "+word+" does not exist.");
}

a) am i making a mistake on this?
Code:
(String word, Vector <String> AlphabetizedListVector)
? it says syntax errors on tokens regarding the parentheses and the comma, ';' expected.

b) how can I instead of 'return -1', print out that the word does not appear in the Vector? thanx
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-26-2007, 01:27 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
I tried your code and, after fixing the typo ("ge t") it works okay. The compile errors must be from something else. The error usually gives the line number.
Code:
public static void main(String[] args) { String s = "hello world we are here"; String[] strs = s.split("\\s"); Vector<String> v = new Vector<String>(); for(int j = 0; j < strs.length; j++) { v.add(strs[j]); } int n = binarySearch("we", v); System.out.println("n = " + n); } static int binarySearch (String word, Vector <String> AlphabetizedListVector){ int lefthand=0; int righthand=AlphabetizedListVector.size() - 1; while (lefthand <= righthand){ int mid =( lefthand + righthand ) / 2; int cmp = word.compareToIgnoreCase(AlphabetizedListVector.get(mid)); if (cmp == 0) return mid; if (cmp < 0){ righthand = mid-1; }else{ lefthand = mid+1; } } return -1; // System.out.println("The word "+word+" does not exist."); }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-26-2007, 02:12 AM
Member
 
Join Date: Nov 2007
Posts: 20
Gilgamesh is on a distinguished road
a) im sending you the pic to understand me better

b) how (and where exactly in the .java) can I printout ("The word "+word+" does not exist.") when the word is not found in the Vector?

1000 thanks
Attached Images
File Type: jpg hardwired11-27.jpg (14.7 KB, 1 views)
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-26-2007, 04:45 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
im sending you the pic to understand me better
I have no idea what that means, must be some ide thing. Try commenting the entire method out and commenting out the calls to it and see where it takes you.
where can I printout "The word "+word+" does not exist." when the word is not found in the Vector
Code:
} System.out.println("The word "+word+" does not exist."); return -1; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to search with a Comparator Java Tip java.lang 0 04-15-2008 08:39 PM
Search TextFile gsupriyarao@yahoo.com Advanced Java 5 04-11-2008 12:03 PM
Search Engine SSam Varghese Java Servlet 5 01-05-2008 09:26 AM
Web Search Marcus Enterprise JavaBeans 2 07-02-2007 07:51 AM
Search in hibernate Nick15 Database 4 05-09-2007 02:33 PM


All times are GMT +3. The time now is 02:27 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org