1.用代码实现以下需求
(1)定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性,生成空参有参构造,set和get方法,toString方法
public class Student {
String name;
String gender;
int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, String gender, int age) {
super();
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Student [name=" + name + ", gender=" + gender + ", age=" + age + "]";
}
}
(2)键盘录入6个学员信息(录入格式:张三,男,25),要求有两个相同的信息,将6个学员信息存入到ArrayList集合中
private static List<Student> inputStuInfo() {
List<Student> stu = new ArrayList<>();
// Student s = new Student();
Scanner sc = new Scanner(System.in);
for (int i = 1; i < 3; i++) {
System.out.println("请输入第" + i + "个学生的信息,格式如下:张三,男,21");
String[] strs = sc.nextLine().split(",");
Student ss = new Student(strs[0], strs[1], Integer.parseInt(strs[2]));
stu.add(ss);
}
System.out.println("学生信息全部添加成功。");
return stu;
}
(3)将存有6个学员信息的ArrayList集合对象写入到D:\\StudentInfo.txt文件中
private static void writeInfoToFile(List<Student> stu, String string) throws IOException {
File file = new File(string);
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(stu);
}
(4)读取D:\\StudentInfo.txt文件中的ArrayList对象
private static List<Student> readStuInfo(String string) throws IOException, ClassNotFoundException {
List<Student> list = new ArrayList<>();
File file = new File(string);
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
list = (List<Student>) ois.readObject();
return list;
}
(5)对ArrayList集合中的6个学生对象进行去重并按照年龄从小到大的顺序排序
private static List<Student> removeDumplicateAndSort(List<Student> list) {
Set<Student> set = new HashSet<>();
set.addAll(list);
TreeSet<Student> treeset = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() > o2.getAge() ? 1 : -1;
}
});
for (Student s : set) {
treeset.add(s);
}
List<Student> sortList = new ArrayList<>();
for (Student s : treeset) {
sortList.add(s);
}
return sortList;
}
(6)将ArrayList集合中排序后的结果利用PrintWriter流写入到E:\\StudentInfo.txt文件中(写入格式:张三-男-25)
private static void writeToFile(List<Student> list, String string) throws FileNotFoundException {
// TODO Auto-generated method stub
PrintWriter pw = new PrintWriter(new File(string));
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student stud = it.next();
pw.write(stud.getName() + "-" + stud.getGender() + "-" + stud.getAge());
}
pw.close();
}