线性查找法01:实现线性查找

算法的分类

排序算法

选择、插入、冒泡、希尔、快速、归并、堆排序、计数排序、桶排序、基数排序

查找算法

线性查找、二分查找

字符串算法

KMP、模式匹配

实现线性查找法

public class Algorithm {

    public static void main(String[] args) {

        int[] arr = {1, 4, 8, 1, 16};
        int target = 16;

        /**
         * 使用类名调用静态方法,节省创建对象的内存
         */
        int res = LinerSearch.search(arr, target);
        System.out.println(res);
    }
}

class LinerSearch {

    /**
     * 1.将构造方法定义为私有类型,可以禁止其他类通过创建对象的方式来调用方法,节省内存
     */
    private LinerSearch(){}

    /**
     * 2.将方法定义为static,就可以通过类名直接调用方法
     */
    public static int search(int[] arr, int target){
        
        for (int i = 0; i < arr.length; i++) {
            
            if (target == arr[i]) {
                return i;
            }
        }
        
        return -1;
    }
}

使用泛型,复用到其他基本数据类型

public class Algorithm {

    public static void main(String[] args) {

        /**
         * 泛型只能接收引用类型,基本数据类型需要使用包装类
         */
        Integer[] arr = {1, 4, 8, 1, 200};
        Integer target = 200;

        String[] str = {"a", "b", "c"};
        String target2 = "a";

        int res = LinerSearch.search(arr, target);
        System.out.println(res);

        int res2 = LinerSearch.search(str, target2);
        System.out.println(res2);
    }
}

class LinerSearch {

    private LinerSearch(){}

    /**
     * 将方法定义为泛型,就可以不受数据类型的限制使用该方法
     */
    public static<E> int search(E[] arr, E target){
        
        for (int i = 0; i < arr.length; i++) {
            /**
             * 此时target和arr[i]都是引用类型,==只能比较二者的地址,equals()方法才能比较内容是否相等
             * Integer类型的整数缓冲池范围是-127~128,当在这个范围时,==也可以正确判断;而200不在这个范围,因此会出错
             */
            if (arr[i].equals(target)) {
                return i;
            }
        }
        
        return -1;
    }
}

对自定义类查找

在查找和排序的过程中,需要对元素进行比较来判断是否是要找的元素,自定义类的属性不一,不能直接进行大小的比较

如果只是需要比较相同与否,自定义类需要重写equals();如果还需要进行排序,则需要先实现Comparable接口,再重写compareTo()方法

public class Algorithm {

    public static void main(String[] args) {

        Student[] student = {new Student("xiaoming"),
                             new Student("xiaohong"),
                             new Student("xiaohua")};

        /**
         * 自定义类的对象,不能直接进行比较,必须要重写equals()方法来指定比较的的规则(Java标准类已经重写了equals()方法,因此无需重写)
         */
        int res = LinerSearch.search(student, new Student("xiaohua"));
        System.out.println(res);
    }
}

class LinerSearch {
    
    private LinerSearch(){}
    
    public static<E> int search(E[] arr, E target){
        
        for (int i = 0; i < arr.length; i++) {
            
            if (arr[i].equals(target)) {
                return i;
            }
        }
        
        return -1;
    }
}

class Student {

    private String name;

    public Student(String name){
        
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {

        /**
         * 1.判断是否相等
         */
        if (this == obj){
            return true;
        }

        /**
         * 2.判断是否为空
         */
        if (obj == null){
            return false;
        }

        /**
         * 3.判断是否是同一个类
         */
        if (this.getClass() != obj.getClass()) {
            return false;
        }

        /**
         * 4.必须经历上述三次判断,强制转换时才不会抛出异常
         * 比较对象的姓名,需要使用String类的equals()方法
         */
        Student student = (Student) obj;
        return this.name.equals(student.name);
    }
}
posted @ 2021-09-27 18:45  振袖秋枫问红叶  阅读(76)  评论(0)    收藏  举报