类型安全:类型 ArrayList 的表达式需要未经检查的转换才能符合 ArrayList<Student>?这会抛出一个 ClassCastException
Type safety: The expression of type ArrayList needs unchecked conversion to conform to ArrayList<Student>? This throws a ClassCastException
我对 Java 还是很陌生,需要帮助解决我的问题。
我正在尝试编写一种方法来反序列化我调用的对象 Student
。方法如下:
public void readStudentInfo() {
// Desrializes the student objects into an ArrayList for you to compare against
try{
FileInputStream fis = new FileInputStream("student.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
deserializedStudents = (ArrayList) ois.readObject();
ois.close();
fis.close();
}
catch(IOException ioe){
ioe.printStackTrace();
return;
} catch(ClassNotFoundException c){
System.out.println("Class not found");
c.printStackTrace();
return;
}
for(Student student : deserializedStudents){
System.out.println(student.toString());
}
}
与之合作的class学生如下:
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
// Class Attributes
private String studentID;
private String rankings;
private char personalityType;
private String conflict;
private String preferences;
// Class Constructor
public Student(String ID){
this.studentID = ID;
}
public Student(String ID, String grades) {
this.studentID = ID;
grades = grades.trim();
this.rankings = grades;
}
public Student(String ID, String ranking,char personality){
this.studentID = ID;
this.rankings = ranking;
this.personalityType = personality;
}
// Accessor Methods
public String getStudentID() {
return this.studentID;
}
public String getRankings(){
return this.rankings;
}
public String getPreferences(){
return this.preferences;
}
public char getPersonalityType(){
return this.personalityType;
}
public String getConflict(){
return this.conflict;
}
//Modifier Methods
public boolean setPreferences(String pref){
this.preferences = pref;
return true;
}
public boolean setGrades(String grades){
this.rankings = grades;
return true;
}
public boolean setPersonalityType(char pers){
this.personalityType = Character.toUpperCase(pers);
return true;
}
public boolean setConflict(String ids){
this.conflict = ids;
return true;
}
@Override
public String toString(){
return studentID + ";" + rankings + ";" + personalityType + ";" + conflict + ";" + preferences;
}
}
ArrayList deserializedStudents
在 class 的顶部启动,如下所示:ArrayList<Student> deserializedStudents = new ArrayList<>();
出于某种原因,readStudentInfo()
的以下行发出此警告:类型安全:类型 ArrayList 的表达式需要未经检查的转换以符合 ArrayListJava(16777748)
如果我忽略它并且 运行 程序不管,readStudentInfo()
方法会给出以下错误:Exception in thread "main" java.lang.ClassCastException: class Student cannot be cast to class java.util.ArrayList (Student is in unnamed module of loader 'app'; java.util.ArrayList is in module java.base of loader 'bootstrap')
有人可以帮我理解为什么会这样吗?我曾多次尝试更改以下行中的演员表:deserializedStudents = (ArrayList) ois.readObject();
。我也尝试过在程序开始时启动 ArrayList 的地方稍微改变一下,但还没有成功。
澄清:这里的基本思想是我试图将一堆 Student 类型的对象反序列化为 ArrayList,以便我可以再次使用它们。
请帮忙!
好像你没有序列化一个Students列表(这是一个自己的对象),而是一个一个地序列化Students。因此,您应该序列化一个列表,或者您应该一个一个地反序列化 Students(通过不断调用 readObject())。
试试这个(使用 try-with-resources):
public void readStudentInfo() {
try (FileInputStream fis = new FileInputStream("student.ser");
ObjectInputStream ois = new ObjectInputStream(fis)) {
List<Student> deserializedStudents = new ArrayList<>();
while (fis.available() > 0) {
deserializedStudents.add((Student) ois.readObject());
}
deserializedStudents.foreach(student -> System.out.println(student));
} catch(IOException | ClassNotFoundException exc){
exc.printStackTrace();
}
}
我对 Java 还是很陌生,需要帮助解决我的问题。
我正在尝试编写一种方法来反序列化我调用的对象 Student
。方法如下:
public void readStudentInfo() {
// Desrializes the student objects into an ArrayList for you to compare against
try{
FileInputStream fis = new FileInputStream("student.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
deserializedStudents = (ArrayList) ois.readObject();
ois.close();
fis.close();
}
catch(IOException ioe){
ioe.printStackTrace();
return;
} catch(ClassNotFoundException c){
System.out.println("Class not found");
c.printStackTrace();
return;
}
for(Student student : deserializedStudents){
System.out.println(student.toString());
}
}
与之合作的class学生如下:
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
// Class Attributes
private String studentID;
private String rankings;
private char personalityType;
private String conflict;
private String preferences;
// Class Constructor
public Student(String ID){
this.studentID = ID;
}
public Student(String ID, String grades) {
this.studentID = ID;
grades = grades.trim();
this.rankings = grades;
}
public Student(String ID, String ranking,char personality){
this.studentID = ID;
this.rankings = ranking;
this.personalityType = personality;
}
// Accessor Methods
public String getStudentID() {
return this.studentID;
}
public String getRankings(){
return this.rankings;
}
public String getPreferences(){
return this.preferences;
}
public char getPersonalityType(){
return this.personalityType;
}
public String getConflict(){
return this.conflict;
}
//Modifier Methods
public boolean setPreferences(String pref){
this.preferences = pref;
return true;
}
public boolean setGrades(String grades){
this.rankings = grades;
return true;
}
public boolean setPersonalityType(char pers){
this.personalityType = Character.toUpperCase(pers);
return true;
}
public boolean setConflict(String ids){
this.conflict = ids;
return true;
}
@Override
public String toString(){
return studentID + ";" + rankings + ";" + personalityType + ";" + conflict + ";" + preferences;
}
}
ArrayList deserializedStudents
在 class 的顶部启动,如下所示:ArrayList<Student> deserializedStudents = new ArrayList<>();
出于某种原因,readStudentInfo()
的以下行发出此警告:类型安全:类型 ArrayList 的表达式需要未经检查的转换以符合 ArrayListJava(16777748)
如果我忽略它并且 运行 程序不管,readStudentInfo()
方法会给出以下错误:Exception in thread "main" java.lang.ClassCastException: class Student cannot be cast to class java.util.ArrayList (Student is in unnamed module of loader 'app'; java.util.ArrayList is in module java.base of loader 'bootstrap')
有人可以帮我理解为什么会这样吗?我曾多次尝试更改以下行中的演员表:deserializedStudents = (ArrayList) ois.readObject();
。我也尝试过在程序开始时启动 ArrayList 的地方稍微改变一下,但还没有成功。
澄清:这里的基本思想是我试图将一堆 Student 类型的对象反序列化为 ArrayList,以便我可以再次使用它们。
请帮忙!
好像你没有序列化一个Students列表(这是一个自己的对象),而是一个一个地序列化Students。因此,您应该序列化一个列表,或者您应该一个一个地反序列化 Students(通过不断调用 readObject())。
试试这个(使用 try-with-resources):
public void readStudentInfo() {
try (FileInputStream fis = new FileInputStream("student.ser");
ObjectInputStream ois = new ObjectInputStream(fis)) {
List<Student> deserializedStudents = new ArrayList<>();
while (fis.available() > 0) {
deserializedStudents.add((Student) ois.readObject());
}
deserializedStudents.foreach(student -> System.out.println(student));
} catch(IOException | ClassNotFoundException exc){
exc.printStackTrace();
}
}