import java.util.*; // File: SimpleSearcher.java // A Java application to illustrate the serial search of an array. // Additional javadoc documentation is available at: // http://www.cs.colorado.edu/~main/docs/SimpleSearcher.html /****************************************************************************** * The SimpleSearcher Java application runs a small test on the * search method that searches for a specified number in an array. * *

*

Java Source Code for this class: *
* * http://www.cs.colorado.edu/~main/applications/SimpleSearcher.java * * @author Michael Main (main@colorado.edu) * * * @version Jun 12, 1998 ******************************************************************************/ public class SimpleSearcher { /** * The main method prints a table of test results, searching for numbers in * an array that contains 2, 4, 6, 8, 10, 12, and 14. The * String arguments (args) are not used in this * implementation. **/ public static void main(String[] args) { final double[] DATA = { 2, 14, 6, 8, 10, 12, 4 }; final double[] EMPTY = new double[0]; final int MINIMUM = -1; final int MAXIMUM = 16; int location; int target; System.out.println("Searching for numbers in an array."); for (target = MINIMUM; target <= MAXIMUM; target++) { System.out.print("Is " + target + " in the array? "); System.out.println(search(DATA, target)); } System.out.println(); System.out.println("Searching2 for numbers in an array."); for (target = MINIMUM; target <= MAXIMUM; target++) { System.out.print("Is " + target + " in the array? "); System.out.println(search2(DATA, target)); } System.out.println(); System.out.println("DATA: " + Arrays.toString(DATA)); System.out.println(); System.out.print("Searching for 0 in an empty array: "); System.out.println(search2(EMPTY, 0)); System.out.println(); System.out.println("Searching3 for numbers in an array."); for (target = MINIMUM; target <= MAXIMUM; target++) { location = search3(DATA, target); if (location != -1) { System.out.println(target + " is in the array."); } else { System.out.println(target + " not found."); } } System.out.println("End of searching."); } /** * Search an array for a specified number. * * @param data an array of double numbers * @param target a particular number that we are searching for * @return true (to indicate that target occurs * somewhere in the array) or false (to indicate that * target is not in the array) **/ public static boolean search(double[] data, double target) { int i; for (i = 0; i < data.length; i++) { // Check whether the target is at // data[i]. if (data[i] == target) return true; } // The loop finished without finding the target. return false; } //------------------------------------------------- /** * Search an array for a specified number. This implementation uses the new * form of a for-loop to iterate through the elements of an array. * * @param data an array of double numbers * @param target a particular number that we are searching for * @return true (to indicate that target occurs * somewhere in the array) or false (to indicate that * target is not in the array) **/ public static boolean search2(double[] data, double target) { for (double d : data) { // Check whether the target is at data[i]. if (d == target) return true; else d = 0; } // The loop finished without finding the target. return false; } //-------------------------------------------------------- public static int search3(double[] data, double target) { int i; for (i = 0; i < data.length; i++) { // Check whether the target is at // data[i]. if (data[i] == target) return i; } // The loop finished without finding the target. return -1; } }