925-2

package lesson0925;

import java.io.Serializable;

public class Student implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private String name;
private String sex;
private int age;
private double height;
public Student() {
	super();
	// TODO Auto-generated constructor stub
}
public Student(String name, String sex, int age, double height) {
	super();
	this.name = name;
	this.sex = sex;
	this.age = age;
	this.height = height;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getSex() {
	return sex;
}
public void setSex(String sex) {
	this.sex = sex;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public double getHeight() {
	return height;
}
public void setHeight(double height) {
	this.height = height;
}
@Override
public String toString() {
	return "Student [name=" + name + ", sex=" + sex + ", age=" + age + ", height=" + height + "]";
}

}


package lesson0925;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
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;
import java.util.Scanner;

public class Second {
private static List<Student> list = new ArrayList<Student>();
public static void main(String[] args) throws IOException, ClassNotFoundException {

	for(int i=0;i<3;i++) {
		Student stu = createStu();
		addStu(stu);
	}
	wrtieObject(list);
	list = readObject();
	System.out.println(list);
	showStu();
	showMaxAge();
}
//创建学生
public static Student createStu() {
	Scanner scan = new Scanner(System.in);
	System.out.println("请输入学生姓名:");
	String name = scan.next();
	System.out.println("请输入学生年龄:");
	int age = scan.nextInt();
	System.out.println("请输入学生性别:");
	String sex = scan.next();
	System.out.println("请输入学生身高:");
	double height = scan.nextDouble();
	Student stu = new Student(name,sex,age,height);
	return stu;

}
//添加5个学生
public static void addStu(Student stu) {
	
	list.add(stu);
	
}
//添加对象到文件中
public static void wrtieObject(List list) throws IOException {
	FileOutputStream fis = new FileOutputStream("student.txt");
	ObjectOutputStream oos = new ObjectOutputStream(fis);
	oos.writeObject(list);
	oos.close();
}
//从文件中读取对象
public static List readObject() throws IOException, ClassNotFoundException {
	FileInputStream fis = new FileInputStream("student.txt");
	ObjectInputStream ois = new ObjectInputStream(fis);
	List list = (List)ois.readObject();
	return list;
}
//按照身高倒序输出
public static void showStu() throws ClassNotFoundException, IOException {
	 list = readObject();
	 Collections.sort(list, new Comparator<Student>() {

		@Override
		public int compare(Student o1, Student o2) {
			// TODO Auto-generated method stub
			return (int)(o2.getHeight()-o1.getHeight());
		}
	});
	System.out.println("排序后:");
	System.out.println(list);
	 
}
//显示年龄最大信息
public static void showMaxAge() throws ClassNotFoundException, IOException {
	list = readObject();
	Student max = list.get(0);
	for(Student s:list) {
		if(s.getAge()>max.getAge()) {
			max = s;
		}
	}
	System.out.println("最大年龄学生的信息:"+max);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值