next up previous
Next: Example: search of the Up: Unit 07 Previous: Example: sum of the

Example: exhaustive search of an element in an array

We develop a static predicate, searchArray, that takes as parameters an array of strings and a string, and returns true, if the string is present in the array, and false otherwise.

public static boolean searchArray(String[] v, String e) {
  for (int i = 0; i < v.length; i++)
    if (e.equals(v[i]))
      return true;
  return false;
}

Example of usage:

public static void main(String[] args) {
  // creation of an array x of 3 strings
  String[] x = { "one", "two", "three" };

  // search the string "two" in the array x
  if (searchArray(x, "two"))
    System.out.println("present");
  else
    System.out.println("not present");  // this will not be printed out
}


next up previous
Next: Example: search of the Up: Unit 07 Previous: Example: sum of the