62,628
社区成员
发帖
与我相关
我的任务
分享package homework;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Test01 {
public static void main(String[] args) throws IOException {
// 入到ArrayList集合中
ArrayList<Student> al = getStuInfo();
weiterInfoToFile(al);
readInfo();
TreeSet<Student> ts = sortStu(al);
printInfoToFile(ts);
}
public static TreeSet<Student> sortStu(ArrayList<Student> al) {
// (5)对ArrayList集合中的6个学生对象进行去重并按照年龄从小到大的顺序排序
Set<Student> set = new HashSet<Student>();
set.addAll(al);
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int i = o1.getAge() - o2.getAge();
return i == 0 ? 1 : i;
}
});
for (Student student : set) {
ts.add(student);
}
return ts;
}
// (6)将ArrayList集合中排序后的结果利用PrintWriter流写入到E:\\StudentInfo.txt文件中(
// 写入格式:张三-男-25)
public static void printInfoToFile(TreeSet<Student> ts)
throws FileNotFoundException {
PrintWriter pw = new PrintWriter(new File("EStudentInfo.txt"));
for (Student student : ts) {
pw.println(student.getName() + "-" + student.getGender() + "-"
+ student.getAge());
}
pw.close();
}
public static void weiterInfoToFile(ArrayList<Student> al)
throws IOException, FileNotFoundException {
// (3)将存有6个学员信息的ArrayList集合对象写入到D:\\StudentInfo.txt文件中
BufferedWriter bfw = new BufferedWriter(new FileWriter(
"DStudentInfo.txt"));
for (Student student : al) {
bfw.write(student.toString());
}
bfw.close();
}
// (2)键盘录入6个学员信息(录入格式:张三,男,25),要求有两个相同的信息,将6个学员信息存
public static ArrayList<Student> getStuInfo() {
Scanner sc = new Scanner(System.in);
ArrayList<Student> al = new ArrayList<>();
for (int i = 0; i < 6; i++) {
System.out.println("请输入第" + (i + 1) + "个学员信息:");
// 获取输入的信息,并去除首尾空格
String s = sc.nextLine().trim();
// 将接收的字符串以 , 截取 存入字符串数组中
String[] strings = s.split(",");
// 将学生信息添加到ArrayList集合中
al.add(new Student(strings[0], strings[1], Integer
.parseInt(strings[2])));
}
return al;
}
// (4)读取D:\\StudentInfo.txt文件中的ArrayList对象
public static void readInfo() throws FileNotFoundException, IOException {
BufferedReader bfr = new BufferedReader(new FileReader(
"DStudentInfo.txt"));
String line = null;
while ((line = bfr.readLine()) != null) {
System.out.println(line);
}
}
}
package homework;
public class Student {
// 1)定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性,生
// 成空参有参构造,set和get方法,toString方法
private String name;
private String gender;
private int 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 String toString() {
return "学生姓名=" + name + ", 性别=" + gender + ", 年龄=" + age + "\r\n";
}
public Student(String name, String gender, int age) {
super();
this.name = name;
this.gender = gender;
this.age = age;
}
public Student() {
}
@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;
}
}
在Student类中忘了写equals和hashCode