在选项二和选项三中,我不知道如何引用某些数据类型的序列化数组列表(即字符串姓氏或双 gpa),然后比较它们。在实验表上,我们得到了如何做其他所有事情的说明,但对于选项 2 和 3,它的字面意思是“搞清楚”,教授从来不善于向全类解释事情。所以我希望有人能向我解释一下。
编辑:901 号码是我所在大学的学生 ID 号码
这是我的学生类(class)(其中一切都应该很好)
import java.io.Serializable;
public class Student implements Serializable
{
private String lastName, firstName;
private double gpa;
private int studentID, gradYear;
public Student(int studentID, String lastName, String firstName, double gpa, int gradYear)
{
this.studentID = studentID;
this.lastName = lastName;
this.firstName = firstName;
this.gpa = gpa;
this.gradYear = gradYear;
}
public int getStudentID()
{
return studentID;
}
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
public double getGPA()
{
return gpa;
}
public int getGradYear()
{
return gradYear;
}
public String toString()
{
return "Student ID: " + studentID + " Name: " + lastName.trim() + ", " + firstName.trim() + " GPA: " + gpa + " Grad year: " + gradYear;
}
}
这是我需要帮助的测试类(除了选项 2 和 3,我认为这一切都有效)
import java.util.*;
import java.io.*;
public class StudentTestOS
{
public static void main(String[] args) throws IOException, ClassNotFoundException
{
boolean done = false;
ArrayList<Student> sList = new ArrayList<Student>();
File sFile = new File("studentOS.dat");
if(sFile.exists())
{
FileInputStream myFIS = new FileInputStream(sFile);
ObjectInputStream sIn = new ObjectInputStream(myFIS);
sList = (ArrayList<Student>)sIn.readObject();
sIn.close();
}
System.out.println("Students on file: ");
for(int i = 0; i < sList.size(); i++)
System.out.println(sList.get(i).toString());
do
{
Scanner myScanner = new Scanner(System.in);
while (!done)
{
System.out.println("1 - add a student");
System.out.println("2 - display student info");
System.out.println("3 - display student info given their last name");
System.out.println("4 - exit");
int choice = Integer.parseInt(myScanner.nextLine());
if (choice == 1)
{
System.out.print("Enter 901 number: ");
int studentID = Integer.parseInt(myScanner.nextLine());
System.out.print("Enter last name: ");
String lastName = myScanner.nextLine();
System.out.print("Enter first name: ");
String firstName = myScanner.nextLine();
System.out.print("Enter gpa: ");
double gpa = Double.parseDouble(myScanner.nextLine());
System.out.print("Enter grad year: ");
int gradYear = Integer.parseInt(myScanner.nextLine());
Student myStudent = new Student(studentID, lastName, firstName, gpa, gradYear);
sList.add(myStudent);
}
else if (choice == 2)
{
System.out.print("Enter 901 number: ");
int studentID = Integer.parseInt(myScanner.nextLine());
}
else if (choice == 3)
{
System.out.println("Enter last name: ");
String lastName = myScanner.nextLine();
}
else if (choice == 4)
{
done = true;
}
else
System.out.println("Invalid menu choice!");
}
System.out.println("Goodbye!");
}while(!done);
FileOutputStream myFOS = new FileOutputStream(sFile);
ObjectOutputStream sOut = new ObjectOutputStream(myFOS);
sOut.writeObject(sList);
sOut.close();
}
}
最佳答案
您可以有一个循环,并在给定属性上一一比较对象(选择 2 的 Id 和选择 3 的姓氏)。
对于选择 2:-
System.out.print("Enter 901 number: ");
int studentID = Integer.parseInt(myScanner.nextLine());
for(Student student : sList){
if(student.getStudentID == studentId){
System.out.println(student);
break; // As student Id is unique.So, once we found the student no need to loop further.
}
}
对于选择3,将其与lastName进行比较,并且不要放置break语句,因为可能有很多具有相同的lastName。
关于java - 如何从序列化数组列表中选取某些对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27307998/