-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLinearSearch.java
More file actions
32 lines (22 loc) · 825 Bytes
/
LinearSearch.java
File metadata and controls
32 lines (22 loc) · 825 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class LinearSearch {
private LinearSearch(){}
public static <E> int search(E[] data, E target){
for(int i = 0; i < data.length; i ++)
if(data[i].equals(target))
return i;
return -1;
}
public static void main(String[] args){
Integer[] data = {24, 18, 12, 9, 16, 66, 32, 4};
int res = LinearSearch.search(data, 16);
System.out.println(res);
int res2 = LinearSearch.search(data, 666);
System.out.println(res2);
Student[] students = {new Student("Alice"),
new Student("Bobo"),
new Student("Charles")};
Student bobo = new Student("Bobo");
int res3 = LinearSearch.search(students, bobo);
System.out.println(res3);
}
}