Day10_数组(下)

Day10_数组(下)

二分法排序

使用前提:数组为有序数组

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6,7,8,9};
        int index=binarySearch(arr,7,0,arr.length-1);
        System.out.println(index);
    }
    public static int binarySearch(int[] arr,int term,int low,int high){
        int index=-1;
        int mid=(low+high)/2;
        int guess=arr[mid];
        if(low>high){
            return index;
        }else if(guess==term){
            return mid;
        }else if(guess>term){
            return binarySearch(arr,term,low,mid-1);
        }else {
            return binarySearch(arr,term,mid+1,high);
        }
    }
}

输出:

6

Arrays类自带排序,二分查找方法:

//Arrays自带排序方法
Arrays.sort(arr);
//Arrays自带二分查找方法
Arrays.binarySearch(arr,7);

排序+内部比较器

新建Student类,继承Comparable接口,重写compareTo()方法。

public class Student implements Comparable{
    private String name;
    private int age;
    private double height;

    public Student(String name, int age, double height) {
        this.setName(name);
        this.setAge(age);
        this.setHeight(height);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    //重写toString方法,改变其返回值
    @Override
    public String toString() {
        return "Student{" +name +"," + age + "," +height + '}';
    }

    @Override
    public int compareTo(Object o) {
        Student other = (Student) o;
        //比较年龄
        //return this.getAge()-other.getAge();
        //比较身高
        //return ((Double)this.getHeight()).compareTo((Double)other.getHeight());
        //比较姓名字母
        return this.getName().compareTo(other.name);
    }
}

测试该方法:

public class Compare {
    public static void main(String[] args) {
        Student s1 = new Student("lili",18,180.9);
        Student s2 = new Student("nana",20,170.9);
        System.out.println(s1.compareTo(s2));
    }
}

输出:

1

排序

创建Student对象数组,利用Student类中的compareTo()方法根据每个对象的某个属性进行排序,例如,下面的例子根据对象的姓名进行排序。该方法先将Object类型的对象转化为Compare接口类型,进而调用实现类的compareTo()方法,提高了代码的扩展性。

public class ArraysUtil {
    public static void sort(Object[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
              //将Object类转化为Compare接口类型
                Comparable com1 = (Comparable) arr[i];
                Comparable com2 = (Comparable) arr[j];
                //调用com1对象内compareTo()方法
                if (com1.compareTo(com2)>0) {
                    Object t = arr[j];
                    arr[j] = arr[i];
                    arr[i] = t;
                }
            }
        }
    }
}

测试sort()方法

import java.util.Arrays;

public class Test01 {
    public static void main(String[] args) {
        Student s1 = new Student("lili",18,180.9);
        Student s2 = new Student("nana",20,170.9);
        Student s3 = new Student("lulu",15,173.9);

        Student[] stus = new Student[3];
        stus[0]=s1;
        stus[1]=s2;
        stus[2]=s3;
        ArraysUtil.sort(stus);
        System.out.println(Arrays.toString(stus));
    }
}

输出:

[Student{lili,18,180.9}, Student{lulu,15,173.9}, Student{nana,20,170.9}]

排序+外部比较器

新建Student类,写内部类,继承Comparator接口。引入内部类中的方法进行排序,更方便对方法进行实现。

import java.util.Comparator;

public class Student{
    private String name;
    private int age;
    private double height;

    public Student(String name, int age, double height) {
        this.setName(name);
        this.setAge(age);
        this.setHeight(height);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    //重写toString方法
    @Override
    public String toString() {
        return "Student{" +name +"," + age + "," +height + '}';
    }
}

class Compare01 implements Comparator {
    //比较身高
    @Override
    public int compare(Object o1, Object o2) {
        Student s1=(Student) o1;
        Student s2=(Student) o2;
        return ((Double)s1.getHeight()).compareTo((Double)s2.getHeight());
    }
}

class Compare02 implements Comparator {
    //比较年龄
    @Override
    public int compare(Object o1, Object o2) {
        Student s1=(Student) o1;
        Student s2=(Student) o2;
        return s1.getAge()-s2.getAge();
    }
}

修改ArraysUtil类中的sort方法,形参增加Comparator对象,以调用Comparator中的方法:

import java.util.Comparator;

public class ArraysUtil {
    public static void sort(Object[] arr, Comparator com) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                //调用com1对象内compareTo()方法
                if (com.compare(arr[i],arr[j])>0) {
                    Object t = arr[j];
                    arr[j] = arr[i];
                    arr[i] = t;
                }
            }
        }
    }
}

main函数:

import java.util.Arrays;
import java.util.Comparator;

public class Test02 {
    public static void main(String[] args) {
        Student s1 = new Student("lili",18,180.9);
        Student s2 = new Student("nana",20,170.9);
        Student s3 = new Student("lulu",15,173.9);

        Student[] stus = new Student[3];
        stus[0]=s1;
        stus[1]=s2;
        stus[2]=s3;
        //创建Compare01对象,以调用sort()方法,根据身高进行排序
        Compare01 c1=new Compare01();
        ArraysUtil.sort(stus,c1);
        System.out.println(Arrays.toString(stus));
        //创建Compare02对象,以调用sort()方法,根据年龄进行排序
        Compare02 c2=new Compare02();
        ArraysUtil.sort(stus,c2);
        System.out.println(Arrays.toString(stus));
      
        //新建Comparator对象,重写compare方法,根据姓名进行排序
        Comparator c3=new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Student s1=(Student) o1;
                Student s2=(Student) o2;
                return s1.getName().compareTo(s2.getName());
            }
        };
        ArraysUtil.sort(stus, c3);
        System.out.println(Arrays.toString(stus));
      
        //直接在方法传参的位置内新建Comparator对象,重写compare方法,根据姓名进行排序,该方式与上一个方式一样,只是没有将新建Comparator对象指向一个变量
        ArraysUtil.sort(stus, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Student s1=(Student) o1;
                Student s2=(Student) o2;
                return s1.getName().compareTo(s2.getName());
            }
        });
        System.out.println(Arrays.toString(stus));
    }
}

输出:

[Student{nana,20,170.9}, Student{lulu,15,173.9}, Student{lili,18,180.9}]
[Student{lulu,15,173.9}, Student{lili,18,180.9}, Student{nana,20,170.9}]
[Student{lili,18,180.9}, Student{lulu,15,173.9}, Student{nana,20,170.9}]
[Student{lili,18,180.9}, Student{lulu,15,173.9}, Student{nana,20,170.9}]

main函数

  • 方法特殊:程序的入口,格式必须固定
  • 可否有重载的main方法?可
  • public:权限修饰符 整个项目
  • static:静态的,先于对象存在
  • void:返回值类型为空
  • main()方法名
  • 虚拟机在调用main函数的时候,传入的是长度为0的数组

二维数组

二维数组本质上是一维数组,在一维数组内存储指向第二维数组的指针地址。

//初始化数组
int[][] a =new int[3][2];
//a[0]指向长度为3的数组
a[0]={1,2,3};

猜你喜欢

转载自www.cnblogs.com/gaoyao/p/13401225.html