一.选择
- bc
- d
- d
二.编程
1.
public class StringSort {
public static void main(String[] args) {
//给list添加元素
List<String> list = new ArrayList<String>();
list.add("orange");
list.add("tomato");
list.add("apple");
list.add("litchi");
list.add("banana");
//输出排序前list中的内容
System.out.println("排序前:");
for(String s : list) {
System.out.print(s + " ");
}
//对list中的元素进行排序
Collections.sort(list);
//输出排序后list中的内容
System.out.println();
System.out.println("排序后:");
for(String s : list) {
System.out.print(s + " ");
}
}
}
2
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 Student() {
super();
}
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "[学号:" + id + ", 年龄:" + age + ", 姓名:" + name + "]";
}
}
public class NameComparator implements Comparator<Student> {
@Override
public int compare(Student arg0, Student arg1) {
String name1 = arg0.getName();
String name2 = arg1.getName();
int n = name1.compareTo(name2);
return n;
}
}
public class StudentTest {
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> list = new ArrayList<Student>();
list.add(s1);
list.add(s2);
list.add(s3);
System.out.println("按名字排序前:");
for(Student s : list) {
System.out.println(s);
}
//按名字进行升序排序后
Collections.sort(list, new NameComparator());
System.out.println("按名字排序后:");
for(Student s : list) {
System.out.println(s);
}
}
}
3
public class Employee implements Comparable<Employee> {
private String id;
private String name;
private float money;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getMoney() {
return money;
}
public void setMoney(float money) {
this.money = money;
}
public Employee() {
super();
}
public Employee(String id, String name, float money) {
super();
this.id = id;
this.name = name;
this.money = money;
}
@Override
public String toString() {
return "员工 [编号:" + id + ", 姓名:" + name + ", 工资:" + money + "]";
}
@Override
public int compareTo(Employee arg0) {
float m1 = this.getMoney();
float m2 = arg0.getMoney();
int n = new Float(m2 - m1).intValue();
return n;
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee y1 = new Employee("emp001", "张三", 1800.0f);
Employee y2 = new Employee("emp002", "李四", 2500.0f);
Employee y3 = new Employee("emp003", "王五", 1600.0f);
List<Employee> list = new ArrayList<Employee>();
list.add(y1);
list.add(y2);
list.add(y3);
System.out.println("排序前:");
for(Employee e : list) {
System.out.println(e);
}
System.out.println("排序后:");
Collections.sort(list);
for(Employee e : list) {
System.out.println(e);
}
}
}