aspose file tools*
The moose likes Beginning Java and the fly likes Searching an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Searching an array" Watch "Searching an array" New topic
Author

Searching an array

Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
Suppose i have a variable int x. Does anyone know how to SEARCH AN UNSORTED ARRAY to see whether this item is in it.

Cheers


Moosey knows best
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Hi Sam,

there are many ways to search an array/list of elements (numbers for example).

I'd like to begin with a small note on searching:
If your supposed to search for a number in the list, it might be a good idea
to build a datastructure from the array/list of elements, and then search
for the number in that datastructure.

Ok, so how do you find x?

I'll show you in pseudo code (which might give you a pointer)


Hope it helped.. feel free to reply back if your still having problems.

/Svend Rost
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

SEARCH AN UNSORTED ARRAY

Yes, you do this with a linear search, as opposed to performing a binary search in the case where the array is sorted. Doesn't your text book already tell you all of this? It is very fundamental.


Tony Morris
Java Q&A (FAQ, Trivia)
Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
I see what your saying, but how do i use these true values? I know its returning true if found, but how do i use this?

Thanks
Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
Tony Morris,

may i remind u this forum page is for beginners. Maybe you should try the intermediate forum page if u find these questions too fundamental for your liking
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18720
    
  40

Originally posted by Sam Bluesman:
I see what your saying, but how do i use these true values? I know its returning true if found, but how do i use this?

Thanks


Staying in pseudo code, because it better learning to code it yourself...

Basically, that is what the conditionals are for. When you need to actually use it, you'll do something like this:



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Sam Bluesman:
I see what your saying, but how do i use these true values? I know its returning true if found, but how do i use this?

Thanks


Well, it depends on what you want to do. Can you describe the problem you are trying to solve? I assume this is for a homework assignment. Is the homework to implement a search algorithm? Or are you supposed to actually use it for something bigger? It will help a lot if you provide some more details about what you are trying to accomplish here.

Layne


Java API Documentation
The Java Tutorial
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Hi Sam,

Originally posted by Sam Bluesman:
Does anyone know how to SEARCH AN UNSORTED ARRAY to see whether this item is in it.


Avoid using uppercase, it appears as if you're shouting. Use bold or italics to catch attention instead.

Originally posted by Sam Bluesman:
I see what your saying, but how do i use these true values? I know its returning true if found, but how do i use this?


Well, in the first place, why are you checking to see if X is in the array? There ought to be a reason for it. With that reason, you should know what you need to do if the value is in it right? It could be simply just telling that the value is in the array, or it could be part of a bigger problem.

Originally posted by Sam Bluesman:
may i remind u this forum page is for beginners. Maybe you should try the intermediate forum page if u find these questions too fundamental for your liking


I suppose Tony was trying to help you to perform self-help here. I don't think he meant to be offensive. Honestly speaking, any decent computer science textbooks would certainly tells you what search to use in an unsorted or sorted array, and they may contain the pseudo-codes as well. Maybe he was trying to get you to read the textbook better.

If you'd converted the pseudo-codes to Java codes & encountered problem compiling or some bugs here & there, feel free to post it here for help.



SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Sam Bluesman:
Tony Morris,

may i remind u this forum page is for beginners. Maybe you should try the intermediate forum page if u find these questions too fundamental for your liking


I didn't say whether or not I liked anything, but since you asked I like it when those who are learning can demonstrate initiative in respect to the topic at hand. I am merely pointing you in the right direction - you do have a text book don't you?

The one I used for my education is obsolete (many will disagree here) - I'm thinking of putting it on ebay for two loaves of bread and a fish, but the examples are in C.

<spam>
Want it? I'll mail it anywhere in Australia for AU$5.
http://www.amazon.com/gp/product/0201498405/103-0263553-5951812?v=glance&n=283155
</spam>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Searching an array