一、选择
1.在下面代码中的(1)(2)处可以填写(多选):BC
A. int int
B. Integer Integer
C. String String
D. string string
2.下列说法中不正确的是:D
A. Comparator接口用于对自定义类进行整体排序
B. Comparator接口可以将Comparator传递给sort方法
C. int compare(T o1,T o2)比较用来排序的两个对象
D. boolean equals(Object obj)指示对象obj是否是“等于”当前对象。此方法不可以被Object类中的equals方法覆盖
3.关于Comparable接口的说法,以下哪个是错误的?:D
A. Comparable位于java.lang包
B. 调用sort方法时,需要指定Comparable接口的实现类
C. Comparable接口的抽象方法是 int compareTo(T t)
D. Comparable接口还可以用于数组的排序
二、编程
1.对英文单词进行排序,效果图如下:
任务
1、给list添加元素
2、输出排序前list中的内容
3、对list中的元素进行排序
4、输出排序后list中的内容
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class StringSort {
public static void main(String[] args) {
//给list添加元素
List<String> fruits = new ArrayList<String>();
fruits.add("orange");
fruits.add("tomato");
fruits.add("apple");
fruits.add("litchi");
fruits.add("banana");
//输出排序前list中的内容
System.out.println("排序前:");
for(String str : fruits) {
System.out.print(str + "\t");
}
//对list中的元素进行排序
Collections.sort(fruits);
//输出排序后list中的内容
System.out.println("\n" + "排序后:");
for(String str : fruits) {
System.out.print(str + "\t");
}
}
}
2.定义一个学生信息类,包括学号,姓名,年龄三个成员变量,然后按名字进行升序排序。(使用Comparator接口)
运行效果图:
任务:
public class Student {
//学号
private int stuId;
//姓名
private String stuName;
//年龄
private int stuAge;
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
//构造器
public Student() {
}
public Student(int stuId,String stuName,int stuAge) {
this.setStuId(stuId);
this.setStuName(stuName);
this.setStuAge(stuAge);
}
//重写toString方法
@Override
public String toString() {
return "[学号:" + this.getStuId() + ", 年龄:" + this.getStuAge() + ", 姓名:" + this.getStuName() + "]";
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class StudentTest implements Comparator<Student>{
@Override
public int compare(Student s1, Student s2) {
// 按名字升序排序
String name1=s1.getStuName();
String name2=s2.getStuName();
int n=name1.compareTo(name2);
return n;
}
public static void main(String[] args) {
Student s1 = new Student(40,"peter",20);
Student s2 = new Student(28,"angel",5);
Student s3 = new Student(35,"tom",18);
List<Student> stuList = new ArrayList<Student>();
stuList.add(s1);
stuList.add(s2);
stuList.add(s3);
System.out.println("按名字排序前:");
for(Student stu : stuList) {
System.out.println(stu);
}
Collections.sort(stuList, new StudentTest());
System.out.println("按名字排序后:");
for(Student stu : stuList) {
System.out.println(stu);
}
}
}
3.定义一个员工信息类,包括编号,姓名,工资三个成员变量,要求工资定义为float类型,然后按工资进行降序排序。(使用Comparable接口)
运行效果图:
任务:
public class Employee {
//编号
private String employeeNo;
//姓名
private String employeeName;
//工资
private float employeeSalary;
public String getEmployeeNo() {
return employeeNo;
}
public void setEmployeeNo(String employeeNo) {
this.employeeNo = employeeNo;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public float getEmployeeSalary() {
return employeeSalary;
}
public void setEmployeeSalary(float employeeSalary) {
this.employeeSalary = employeeSalary;
}
//构造器
public Employee() {
}
public Employee(String employeeNo,String employeeName,float employeeSalary) {
this.setEmployeeNo(employeeNo);
this.setEmployeeName(employeeName);
this.setEmployeeSalary(employeeSalary);
}
//重写toString方法
public String toString() {
return "员工[编号:" + this.getEmployeeNo() + ",姓名:" + this.getEmployeeName() + ",工资:" + this.getEmployeeSalary() + "]";
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class EmployeeTest implements Comparator<Employee>{
@Override
public int compare(Employee e1,Employee e2) {
float s1 = (int)e1.getEmployeeSalary();
float s2 = (int)e2.getEmployeeSalary();
return (int)(s2-s1);
}
public static void main(String[] args) {
Employee e1 = new Employee("emp001","张三",1800);
Employee e2 = new Employee("emp002","李四",2500);
Employee e3 = new Employee("emp003","王五",1600);
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(e1);
employeeList.add(e2);
employeeList.add(e3);
System.out.println("排序前:");
for(Employee e : employeeList) {
System.out.println(e);
}
Collections.sort(employeeList,new EmployeeTest());
System.out.println("排序后:");
for(Employee e : employeeList) {
System.out.println(e);
}
}
}