aspose file tools*
The moose likes Beginning Java and the fly likes Stumped (Binary Search?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stumped (Binary Search?)" Watch "Stumped (Binary Search?)" New topic
Author

Stumped (Binary Search?)

John Molloy
Greenhorn

Joined: Apr 26, 2009
Posts: 7
Hey guys, I've just started programming at university and I'm on an assignment that has me stumped. Now I just started programming a few weeks ago so excuse me if this is a noob question. I have an array of Strings with names of a few fruits that can be harvested. I have the harvest method as you can see below which has to check if the parameter entered matches one of the elements in the array. Now hours of googling has bought me to think that maybe a binary search would work but really I have no idea. I've played around with the binary search but haven't managed to get it to work so far.

Here is the relevant code anyway:



This wont compile at all which I'm sure is obvious to you guys. Now I assume that I need a rtValue = true or something similar under the search but I'm a little unsure. Oh also they want us to add another element to the array via a method. Now I thought once arrays were made they were fixed and to have a variable size array you need a arraylist?

Cheers
John





Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Welcome to JavaRanch

We expect newbies' questions here. That's why we have a beginner's forum in the first place

I think your real problem is, you are trying to mix two things. You appear to be sorting your harvest, and seeing how happy you are with the quality of fruit. Don't try to do both things simultaneously.

Are you obliged to implement your own sort methods, or can you simply use a linear search through your array? Are you supposed to implement this sort of method?If you think you have got it to work, try this sort of thingThat should show whether that method works.

Get that bit working, and don't try to do the whole lot in one fell swoop.
John Molloy
Greenhorn

Joined: Apr 26, 2009
Posts: 7
Sorry I didn't explain it very well. This assignment doesn't have much real life context. It's based on a very very simplistic country that can only harvest the crops that are in the array. First I have added an array in the constructor like so:



Now first of all I am wanting to search the array to check if any of the elements in the array match the string that has been entered into the parameter (newCrop). If there is a match it will carry out the rest of the method. If there is no match it will do nothing and go to the end. I'll show you what I have now without the array.



I pretty much want exactly the same functionality but using an array instead.
Thanks for your help mate, hope this clears it up a bit.
Prabz Bhatia
Greenhorn

Joined: Apr 14, 2009
Posts: 19
An easier and more efficient method would be to use an ArrayList.
Then you could use its inbuilt method to check for existence of an element:
public boolean contains(Object)




John Molloy
Greenhorn

Joined: Apr 26, 2009
Posts: 7
Prabz Bhatia wrote:An easier and more efficient method would be to use an ArrayList.
Then you could use its inbuilt method to check for existence of an element:
public boolean contains(Object)






Works perfectly, thanks a bunch guys.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
The ArrayList<T> class implements List#contains() with a linear search.
You should check that your assignment doesn't say "use an array," otherwise you would lose lots of marks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stumped (Binary Search?)
 
Similar Threads
Alphabetic Sorting in various data structures.
doubleValue++
SCJA Prep
Find string in binary file
Casting Object[] into String[]