Java 如何从序列化的arraylist中拾取某些对象?

Java 如何从序列化的arraylist中拾取某些对象?,java,serialization,Java,Serialization,在选项2和选项3中,我不知道如何为某些数据类型I.e引用序列化的arraylist。字符串last name或double gpa,然后比较它们。在实验单上,我们得到了关于如何做其他事情的说明,但对于做选项2和选项3,它实际上是在说“弄明白”,教授从来都不善于向全班解释事情。所以我希望有人能给我解释一下 编辑:901号是我所在大学的学号 这是我的学生班,里面的一切都应该很好 import java.io.Serializable; public class Student implements

在选项2和选项3中,我不知道如何为某些数据类型I.e引用序列化的arraylist。字符串last name或double gpa,然后比较它们。在实验单上,我们得到了关于如何做其他事情的说明,但对于做选项2和选项3,它实际上是在说“弄明白”,教授从来都不善于向全班解释事情。所以我希望有人能给我解释一下

编辑:901号是我所在大学的学号

这是我的学生班,里面的一切都应该很好

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();
}
}

您可以创建一个循环,并在choice2的给定属性Id和Choice3的lastName上逐个比较对象

选择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的语句