# 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);
``````

## 排序+内部比较器

``````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
``````

### 排序

``````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;
}
}
}
}
}
``````

``````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}]
``````

## 排序+外部比较器

``````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();
}
}
``````

``````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};
``````