【发布时间】:2015-02-03 03:46:38
【问题描述】:
在选项二和三中,我不知道如何引用某些数据类型(即字符串姓氏或双 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();
}
}
【问题讨论】:
标签: java serialization