学生成绩排序系统

package newstudent;

public class Student implements Comparable<Student>{
	private String code;
	private String name;
	private String subject;
	private int grade;
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public int getGrade() {
		return grade;
	}
	public void setGrade(int grade) {
		this.grade = grade;
	}
	public Student() {
	}
	public Student(String code, String name, String subject, int grade) {
		super();
		this.code = code;
		this.name = name;
		this.subject = subject;
		this.grade = grade;
	}
	@Override
	public String toString() {
		return "Student [code=" + code + ", name=" + name + ", subject=" + subject + ", grade=" + grade + "]";
	}
	@Override
	public int compareTo(Student o) {
		int grade1 = this.grade;
		int grade2 = o.getGrade();
		int grade = 0;
		if(grade1 > grade2){
			grade = -1;
		}else if(grade1 < grade2){
			grade = 1;
		}
		return grade;
	}
	
}
package newstudent;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class FileOpreate {
	public FileOpreate() {
		Student s1 = new Student("1","x0","y",23);
		Student s2 = new Student("2","x1","y",25);
		Student s3 = new Student("3","x2","y",21);
		Student s4 = new Student("4","x3","y",20);
		Student s5 = new Student("5","x4","y",29);
		List<Student> list = new ArrayList<Student>();
		list.add(s1);
		list.add(s2);		
		list.add(s3);
		list.add(s4);
		list.add(s5);
		try {
			FileOutputStream fos = new FileOutputStream("123.txt");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			FileInputStream fis = new FileInputStream("123.txt");
			ObjectInputStream ois = new ObjectInputStream(fis);
			
			oos.writeObject(list);
			oos.writeBoolean(true);
			oos.flush();
			// 读对象信息
			try {
				List list1 = (List) ois.readObject();
				System.out.println(list1);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			System.out.println(ois.readBoolean());//读取的顺序要和写入的顺序一致

			fos.close();
			oos.close();
			fis.close();
			ois.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

package newstudent;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {
	public static void main(String[] args) {
		Student s1 = new Student("1","x0","y",23);
		Student s2 = new Student("2","x1","y",25);
		Student s3 = new Student("3","x2","y",21);
		Student s4 = new Student("4","x3","y",20);
		Student s5 = new Student("5","x4","y",29);
		List<Student> list = new ArrayList<Student>();
		list.add(s1);
		list.add(s2);		
		list.add(s3);
		list.add(s4);
		list.add(s5);
	
	

		Collections.sort(list);
		System.out.println("排序后:");
		for(Student student : list){
			System.out.println(student);
		}
		Collections.sort(list, new Comparator<Student>(){

			@Override
			public int compare(Student o1, Student o2) {
				int s1 = o1.getGrade();
				int s2 = o2.getGrade();
				return s2-s1;
			}
			
		});
		for(Student s : list) {
			System.out.println(s);
		}
	}
	
} 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值