package 数据结构.线性查找法;publicclassLinearSearch{privateLinearSearch(){}publicstatic<T>intsearch(T[] data, T target){for(int i =0; i < data.length; i++){if(target.equals(data[i]))return i;}return-1;}publicstaticvoidmain(String[] args){
Integer[] data ={112,33,55,54,12,59,74};int a = LinearSearch.search(data,54);
System.out.println(a);int b = LinearSearch.search(data,777);
System.out.println(b);
System.out.println("-----------------------------------");
Student[] students ={newStudent("张三",18,'男'),newStudent("李四",18,'女'),newStudent("王五",18,'男'),newStudent("喜喜",18,'男')};int c = LinearSearch.search(students,newStudent("张三",18,'男'));
System.out.println(c);}}
package 数据结构.线性查找法;public class LinearSearch { public static int search(int[] data, int target) { for (int i = 0; i < data.length; i++) { if (target == data[i]) return i; } return -1; }