Java中的线性搜索。
以下是必需的程序。
示例
public class Tester {
public static int linearSearch(int[] arr, int element) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == element) {
return i;
}
}
return -1;
}
public static void main(String a[]) {
int[] array = { 10, 20, 30, 50, 70, 90 };
int element = 50;
int index = linearSearch(array, element);
if (index != -1) {
System.out.println(element + " present at index: " +index);
} else {
System.out.println(element + " 不存在。");
}
}
}输出结果
50 present at index: 3
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短