Linear Search Specifics
- That loop does all the work, but you want some extra stuff
around it to make it more useable.
- A simple function to search an array of characters for
a character that is passed in as a parameter. It returns true
if it's there, false if not.
- Note, that you'd have to write a linear search for each data
type. You could use advanced features of C++ or Java to avoid
this, but it's probably not worth the confusion. That is, I write
the search from scratch each time like this:
private boolean isCharPresent(char searchChar) {
for (int i = 0; i < lastChar; i++) {
if (charArray[i] == searchChar) return true;
}
return false;
}
- This assumes the array is called charArray, and that there
is a class value called searchChar.
- I haven't compiled or run this, it's just off the top of my head.
- You'd call it (within the class) as a boolean expression
isCharPresent('b')
- What would change if you were searching an array of student records
for a student with a particular number?