扎实打牢数据结构算法根基,从此不怕算法面试系列之005 week01 02-05 使用自定义类测试我们前面实现的支持泛型的线性查找法
1、算法描述2、上一篇的实现结果在扎实打牢数据结构算法根基,从此不怕算法面试系列之004week0102-04使用泛
2023-04-16
在扎实打牢数据结构算法根基,从此不怕算法面试系列之004 week01 02-04 使用泛型实现线性查找法中,我们实现了:
package com.mosesmin.datastructure.week01.chap02;/** * @Misson&Goal 代码以交朋友、传福音 * @ClassName LinearSearch03 * @Description TODO * @Author MosesMin * @Date 2023/4/13 * @Version 1.0 */public class LinearSearch04 { private LinearSearch04(){} public static int search(E [] data,E target){// 将search方法定义为泛型方法 for (int i = 0; i < data.length; i++) if (data[i].equals(target))// 这里判断相等不能使用==了,==判断的是引用相等,这里需要使用判断值相等,所以用equals方法 return i; return -1; } public static void main(String[] args) { Integer [] data = {1,18,22,10,35};//3、所以这里的解决方法是,将int修改为Integer //int res = LinearSearch04.search(data,10);// 1、这里报错了,因为Java中泛型只能接受类对象,不能接受基本数据类型 //此时,这里的参数10已经被JVM从int类型的10自动转换为Integer类型的10了,所以不再报错 int res = LinearSearch04.search(data,10); //Java7以前需要加上这样的泛型限定,Java8以后可以省略 System.out.println(res); //int res2 = LinearSearch04.search(data,666);// 2、这里报错了,因为Java中泛型只能接受类对象,不能接受基本数据类型 int res2 = LinearSearch04.search(data,666);//此时,这里的参数10已经被JVM从int类型的666自动转换为Integer类型的666了,所以不再报错 System.out.println(res2); }} 实现一个Student类
(资料图片)
package com.mosesmin.datastructure.week01.chap02;/** * @Misson&Goal 代码以交朋友、传福音 * @ClassName Student * @Description TODO * @Author MosesMin * @Date 2023/4/13 * @Version 1.0 */public class Student { private String name; public Student(String name) { this.name = name; }}测试Student类,声明一个Student类数组students,在students中查找学生目标元素mosesmin
package com.mosesmin.datastructure.week01.chap02;/** * @Misson&Goal 代码以交朋友、传福音 * @ClassName LinearSearch03 * @Description TODO * @Author MosesMin * @Date 2023/4/13 * @Version 1.0 */public class LinearSearch05 { private LinearSearch05(){} public static int search(E [] data,E target){// 将search方法定义为泛型方法 for (int i = 0; i < data.length; i++) if (data[i].equals(target))// 这里判断相等不能使用==了,==判断的是引用相等,这里需要使用判断值相等,所以用equals方法 return i; return -1; } public static void main(String[] args) { Integer [] data = {1,18,22,10,35};//3、所以这里的解决方法是,将int修改为Integer //int res = LinearSearch04.search(data,10);// 1、这里报错了,因为Java中泛型只能接受类对象,不能接受基本数据类型 //此时,这里的参数10已经被JVM从int类型的10自动转换为Integer类型的10了,所以不再报错 int res = LinearSearch05.search(data,10); //Java7以前需要加上这样的泛型限定,Java8以后可以省略 System.out.println(res); //int res2 = LinearSearch04.search(data,666);// 2、这里报错了,因为Java中泛型只能接受类对象,不能接受基本数据类型 int res2 = LinearSearch05.search(data,666);//此时,这里的参数10已经被JVM从int类型的666自动转换为Integer类型的666了,所以不再报错 System.out.println(res2); Student [] students = { new Student("Peter"), new Student("John"), new Student("Paul"), new Student("MosesMin") };// Peter 彼得、John 约翰、Paul 保罗、MosesMin都是耶稣的门徒——学生 Student mosesmin = new Student("MosesMin"); int res3 = LinearSearch05.search(students,mosesmin); System.out.println(res3); }} 查找结果:
返回-1,没有找到。
为什么呢?因为学生类没有重写equals方法,所以默认调用的是Java中的基类Object类的equals方法,该方法源码如下,比较的是对象(即引用,在内存中的地址)是否相等,而不是比较对象的值是否相等。
public boolean equals(Object obj) { return (this == obj);}可以参考文章Java中==和equals()的区别 详细了解区别。
所以这里需要我们为Student类重写覆盖Object类的equals方法。重写完equals方法的Student类如下:
package com.mosesmin.datastructure.week01.chap02;/** * @Misson&Goal 代码以交朋友、传福音 * @ClassName Student * @Description TODO * @Author MosesMin * @Date 2023/4/13 * @Version 1.0 */public class Student { private String name; public Student(String name) { this.name = name; } /** * 覆盖实现Object类的equals方法 * @param student * @return */ @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;//将传来的Object类型的参数student变量强制转换为Student类型的变量,并赋值给another return this.name.equals(another.name);// 这里this.name是String类型,调用的是String类型的equals方法,String类的equals方法重写了Object类的equals方法,比较的是String字符串的值 } /** * Student类自己的equals方法 * @param student * @return */ public boolean equals(Student student){ return false; }}注意,重写equals方法时,下面这三个非常简单的套路判断条件,一看代码应该就可以动,可以记住它们:
if (this == student) return true; if (student == null) return false; if (this.getClass()!=student.getClass()) return false;此时,再次执行LinearSearch05类的main方法,进行测试,可以看到如下测试结果:
如图,在students数组中找到了mosesmin,mosesmin的索引为3。
标签: