[英]Sending an ArrayList of serialized objects to client (TCP) from server
[英]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.