簡體   English   中英

如何從序列化的數組列表中選擇某些對象?

[英]How to pick certain objects from a serialized arraylist?

在選項二和三中,我不知道如何為某些數據類型(即字符串姓氏或雙精度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();
}
}

您可以有一個循環,並在給定的屬性(選擇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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM