简体   繁体   English

将数组强制转换为readObject时发生ClassCastException

[英]ClassCastException when casting array to readObject

I have to run this array of object Student (where there's 3 students): 我必须运行对象Student的数组(有3个学生):

        try
    {
        out = new ObjectOutputStream (new BufferedOutputStream (new FileOutputStream ("Students.dat")));
        out.writeObject(s[0]);
        out.writeObject(s[1]);
        out.writeObject(s[2]);
        out.close();
    }
    catch (IOException e)
    {
        System.out.println("Error writing student data to file.");
    }
}

And each Student object must be set like this: 并且每个Student对象必须设置如下:

for (int i = 0; i<3; i++)
    {
        System.out.println("The following information applies to student " + (i+1));
        System.out.println("What is the student's name?");
        String name = input.nextLine();
        if (i == 0)
        {
            System.out.println("Please enter the name again.");
        }
        name = input.nextLine();
        System.out.println("What is the Social Security Number of this student?");
        String ssn = input.next();
        System.out.println("How many courses has the student completed?");
        int numCourses = input.nextInt();
        int [] grades = new int [numCourses];
        int credits = (5*numCourses);

        double points = 0;
        for(int k = 0; k<numCourses; k++)
        {
            System.out.println("Type a number to represent the letter grade of course " + (k+1) + ". Type 1 for an A. Type 2 for a B. Type 3 for a C. Type 4 for a D. Type 5 for an F.");
            grades[k] = input.nextInt();
            switch (grades[k])
            {
                case 1:
                    points += 4;
                    break;
                case 2:
                    points += 3;
                    break;
                case 3:
                    points += 2;
                    break;
                case 4:
                    points += 1;
                    break;
                case 5:
                    break;
            }

            s[i] = new Student (name, ssn, numCourses, grades, credits);
        }
    }

I keep getting the ClassCastException when running these lines: 运行这些行时,我不断收到ClassCastException:

Object obj = new Object();
obj = in.readObject();
Student[] ns = (Student[])obj;

The exception looks like this: 异常如下所示:

java.lang.ClassCastException: UnitSeven.Student cannot be cast to [LUnitSeven.Student;
at UnitSeven.StudentGPA.main(StudentGPA.java:21)

And line 21 is the last line in the code mentioned above. 第21行是上述代码中的最后一行。 Does anyone know how to fix this so that I can cast it properly? 有谁知道如何解决此问题,以便我可以正确投放? Thanks in advance for any help. 在此先感谢您的帮助。

You're reading a single Student but trying to cast it as an Student[] . 您正在阅读一个Student但试图将其转换为Student[] Just remove the array reference: 只需删除数组引用:

Student s = (Student)obj;

This is because you're storing each element of your array in your ObjectOutputStream by itself, noted here: 这是因为您将数组的每个元素ObjectOutputStream存储在ObjectOutputStream ,如下所示:

out = new ObjectOutputStream (new BufferedOutputStream (new FileOutputStream ("Students.dat")));
//you write each reference of Student
out.writeObject(s[0]);
out.writeObject(s[1]);
out.writeObject(s[2]);
out.close();

If you want/need to read it as an array, then store it as an array as well: 如果您希望/需要将其读取为数组,则也将其存储为数组:

out = new ObjectOutputStream (new BufferedOutputStream (new FileOutputStream ("Students.dat")));
out.writeObject(s);
out.close();

So you can read it properly: 因此,您可以正确阅读它:

Object obj = new Object();
obj = in.readObject();
Student[] ns = (Student[])obj;

Or in a single line: 或一行:

Student[] ns = (Student[])in.readObject();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM