集合排序练习

一.选择

  1. bc
  2. d
  3. 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);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值