为什么我的对象数组没有正确制作?

Why is my object array not being made correctly?

我正在尝试为我的 TestScores class 创建一个对象数组,它接受一个数组,然后可以使用 averageScore 方法获得平均分数。然而,它似乎只写入对象数组的最后一个值,因为输出只是打印输入的最后一个值。我尝试使用系统打印行在填充它的 for 循环中获取对象数组输出,这似乎工作正常,但在循环之外它似乎没有给出预期的输出,这将是平均值输入到 class.

中的每个数组
import java.io.*;
import java.util.Scanner;
public class TestScores implements Serializable{

private int[] testArray;

public TestScores(int[] parameter)
        throws InvalidTestScore {
    testArray = parameter;

    for (int k : testArray) {
        if (k > 100 || k < 0) {
            throw new InvalidTestScore(k);
        }
    }
}

    public int averageScore() {
        int sum = 0;

        for (int j : testArray) {
            sum += j;
        }
        return sum / testArray.length;
    }


public static void main(String[] args) throws InvalidTestScore, IOException, ClassNotFoundException {
    int input;
    int[] array = new int[2];

    Scanner keyboard = new Scanner(System.in);


    TestScores[] testScores = new TestScores[5];
    for(int k = 0; k < testScores.length; k++) {

        for (int i = 0; i < 2; i++) {
            System.out.print("Enter a score: ");
            input = keyboard.nextInt();
            array[i] = input;
        }
        System.out.println("Array created ");
        testScores[k] = new TestScores(array);
    }

    FileOutputStream outStream =
            new FileOutputStream("Object.dat");
    ObjectOutputStream objectOutputFile =
            new ObjectOutputStream(outStream);

    for (TestScores testScore : testScores) {
        objectOutputFile.writeObject(testScore);
    }
    objectOutputFile.close();


    System.out.println("Objects serialized and written to objects.dat");

    FileInputStream inStream =
            new FileInputStream("Object.dat");
    ObjectInputStream objectInputFile =
            new ObjectInputStream(inStream);

    TestScores[] scores2 =
            new TestScores[5];

    for(int m = 0; m < scores2.length; m++){
        scores2[m] =
                (TestScores)objectInputFile.readObject();
    }

    objectInputFile.close();

    for(int n = 0; n < 5; n++){
        System.out.println("Average score " + scores2[n].averageScore());
    }
}

}

根据评论,我更改了我的代码并将其设为一个数组数组,因此我没有在整个程序中使用相同的数组。

    public static void main(String[] args) throws InvalidTestScore, 
    IOException, ClassNotFoundException {
    int input;
    int[][] array = new int[5][2];


    Scanner keyboard = new Scanner(System.in);


    TestScores[] testScores = new TestScores[5];
    for(int k = 0; k < testScores.length; k++) {

        for (int i = 0; i < 2; i++) {
            System.out.print("Enter a score: ");
            input = keyboard.nextInt();
            array[k][i] = input;
        }

        System.out.println("Array created ");
        testScores[k] = new TestScores(array[k]);
    }

您将相同的 array 输入数据添加到所有 TestScore 对象。因此,如果您为 TestScore 个实例的后续实例修改 array,您也会修改所有先前创建的实例。

有两种可能的解决方案对我来说很有用,请选择一种更适合您的解决方案:

  1. 您在第一个循环中立即将每个 TestScore 实例写入文件 - 而不是创建一个新的 TestScore 实例!您不需要 TestScore 数组来输入所有值。
  2. 在创建 TestSuite 的新实例之前,您还在输入中创建了 array 的新实例。所以每个 TestSuite 都有它自己的数组实例。