LineSearch类
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 ={10,13,15,46,78,76,82,54};
int res1 = LinearSearch.<Integer>search(data,13);
int res2 = LinearSearch.search(data,99);
System.out.println(res1);
System.out.println(res2);
Student [] students = {new Student("Alice"),
new Student("Taotao"),
new Student("Chares")};
Student taotao = new Student("Taotao");
int res3 = LinearSearch.search(students,taotao);
System.out.println(res3);
}
}
Student类
/**
* @author ${范涛之}
* @Description
* @create 2021-09-01 13:58
*/
public class Student {
private String name;
public Student(String name){
this.name = name;
}
@Override
public boolean equals(Object student){
if(this == student) {
return true;
}
if (student == null) {
return false;
}
if (this.getClass() != student.getClass()) {
return false;
}
Student another = (Student) student;
return this.name.equals(another.name);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/81105.html