This book is now obsolete Please use CSAwesome instead.
13.2. Sequential Search¶
Sequential search is the only method that can be used to find a value in unsorted data. It usually starts at the first element and walks through the array or list until it finds the value it is looking for and returns the index it found it at, or it loops until the end of the array or list and then it returns a -1 to show that it didn’t find the value in the array or list.
The code for
sequentialSearch below is from the AP CS A course description.
To see this executing using the Java Visualizer click on the following link SequentialSearch
Of course you can also look for a string in an array or list. But, when you look for a string be sure to use
equals rather than
==. Remember that
== is only true when the two references refer to the same object, while
equals returns true if the characters in the two objects are the same.
To see this executing using the Java Visualizer click on this String-SeqSearch