java - 如何从序列化数组列表中选取某些对象?

标签 java serialization

在选项二和选项三中,我不知道如何引用某些数据类型的序列化数组列表(即字符串姓氏或双 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/

相关文章:

java - 将属性写入二维数组

python - 如何判断哪个对象属性泡菜失败?

java - 你能让一个对象在运行时可序列化吗?

java - boot层初始化出错FindException : Module not found

java - 将 json 值 key 对解压为 SharedPreferences 的变量

java - 在不解压的情况下查看 zip 中的图像

java - 错误 - 内部类中的非法静态声明

c# - 将多个命名空间添加到 MessageContract WCF 响应对象 (MessageBodyMember)

jquery 使用数组序列化输入

c# - 使集合可用于两个流程