import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Demo {static class Student implements Comparable{\x05 String name;\x05 int age;\x05public Student(String name,int age) {\x05\x05super();\x05\x05this.name = nam

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/26 01:25:39
importjava.util.ArrayList;importjava.util.Collections;importjava.util.List;publicclassDemo{staticcla

import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Demo {static class Student implements Comparable{\x05 String name;\x05 int age;\x05public Student(String name,int age) {\x05\x05super();\x05\x05this.name = nam
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Demo {
static class Student implements Comparable{
\x05 String name;
\x05 int age;
\x05public Student(String name,int age) {
\x05\x05super();
\x05\x05this.name = name;
\x05\x05this.age = age;
\x05}
\x05@Override
\x05public String toString() {
\x05\x05return name+","+age;
\x05}
\x05@Override
\x05public int compareTo(Object o) {
\x05\x05Student stu = (Student)o;
\x05\x05if(this.age>stu.age){
\x05\x05\x05return 1;
\x05\x05}
\x05\x05else if(this.age==stu.age && this.name.equals(stu.name)){
\x05\x05 return 0;
\x05\x05}
\x05\x05else{
\x05\x05\x05return -1;
\x05\x05}
\x05}
\x05/*@Override
\x05public int compareTo(Student o) {
\x05\x05Student stu = (Student)o;
\x05\x05if(this.age>stu.age){
\x05\x05\x05return 1;
\x05\x05}
\x05\x05else if(this.age==stu.age && this.name.equals(stu.name)){
\x05\x05 return 0;
\x05\x05}
\x05\x05else{
\x05\x05\x05return -1;
\x05\x05}
\x05}*/
}
public static void main(String[] args) {
\x05 List list = new ArrayList();
\x05 Student[] stus = new Student[]{new Student("小美",21),
\x05\x05\x05\x05new Student("阿聪",22),new Student("武大郎",28),
\x05\x05\x05\x05new Student("阮小七",26),new Student("晁盖",30),
\x05\x05\x05\x05new Student("鲁智深",29),new Student("孙二娘",26),
\x05\x05\x05\x05new Student("扈三娘",23),new Student("武松",24)
\x05\x05};
Collections.addAll(list,stus);
Collections.sort(list);
for(int i = 0;i

import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Demo {static class Student implements Comparable{\x05 String name;\x05 int age;\x05public Student(String name,int age) {\x05\x05super();\x05\x05this.name = nam
这是因为binarySearch的第一个参数类型是:
List