Sequentially searching an array for an item. - Java Data Structure

Java examples for Data Structure:Search

Description

Sequentially searching an array for an item.

Demo Code

import java.security.SecureRandom;
import java.util.Arrays;

public class Main {
  // perform a linear search on the data
  public static int linearSearch(int data[], int searchKey) {
    // loop through array sequentially
    for (int index = 0; index < data.length; index++)
      if (data[index] == searchKey)
        return index; // return index of integer

    return -1; // integer was not found
  }/*from w  ww.ja  va2s .  c  o  m*/

  public static void main(String[] args) {
    SecureRandom generator = new SecureRandom();

    int[] data = new int[10]; // create array

    for (int i = 0; i < data.length; i++)
      // populate array
      data[i] = 10 + generator.nextInt(90);

    System.out.printf("%s%n%n", Arrays.toString(data)); // display array

    int searchInt = 123;

    // repeatedly input an integer; -1 terminates the program
    while (searchInt != -1) {
      int position = linearSearch(data, searchInt); // perform search

      if (position == -1) // not found
        System.out.printf("%d was not found%n%n", searchInt);
      else
        // found
        System.out.printf("%d was found in position %d%n%n", searchInt,
            position);

      break;
    }
  }
}

Result


Related Tutorials