提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
顺序搜索:对数组的穷举搜索
public class Main { static int SequentialSearch(int[] array, int key) { /* 来自 *NowJava.com - 时代Java*/ int i; for (i = 0; i < array.length; i++) if (array[i] == key) return i; return -1; } public static void main(String[] args) { int[] v = {1, 6, 9, 12, 45, 67, 76, 80, 95}; System.out.println("Seeking for element 6: Position " + SequentialSearch(v, 6)); System.out.println("Seeking for element 80: Position " + SequentialSearch(v,