为什么在尝试从我的文件中读取对象时出现 ClassCastException?

Why do I get ClassCastException when trying to read objects from my file?

我收到一个运行时错误:线程 "main" java.lang.ClassCastException 中的异常:lab07.Loan 无法转换为 [Llab07.Loan; 在 lab07.TestLoanClass.main(TestLoanClass.java:27)。

错误可能出在可序列化的class Loan 中,但请先看一下这个。因为如果我避免使用数组,一切都很好。我正在使用 Eclipse IDE.

public class TestLoanClass {

public static void main(String[] args) throws IOException,
        ClassNotFoundException {
    Loan[] loan = new Loan[5];

    ObjectOutputStream output = new ObjectOutputStream(
            new FileOutputStream("Exercise19_06.dat"));

    for (int i = 0; i < 5; i++) {
        loan[i] = new Loan(1.5 * i,i * 2,10000 * i);
        output.writeObject(loan[i]);
    }
    output.close();

    ObjectInputStream input = new ObjectInputStream(new FileInputStream(
            "Exercise19_06.dat"));


    //Conversion below is perfectly legal to type, but I get an error when 
    //I run the program. Appearently it has something with Object being 
    //cast to a Loan. Shouldn't the error show up while writing the code?

      Loan[] loanRead = (Loan[]) (input.readObject());

    //However, it work's just fine if I avoid using an array

    for (int j = 0; j < 5; j++) {

        System.out
                .printf("The loan was created on %s\n"
                        + "The monthly payment is %.2f\nThe total payment is %.2f\n",
                        loanRead[j].getLoanDate().toString(),
                        loanRead[j].getMonthlyPayment(),
                        loanRead[j].getTotalPayment());
    }

    input.close();
}

}

删除 Loan[] loanRead = (Loan[]) (input.readObject()); 并在第二个 for 循环中放入 loan[j] = (Loan) (input.readObject());

当然,用 loan[j].

在 for 循环中更改所有出现的 loanRead[j]