• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stumped (Binary Search?)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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





 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic