【问题标题】:Array index out of bounds with JSwing and 2D arrayJSwing和二维数组的数组索引越界
【发布时间】:2014-06-19 20:05:21
【问题描述】:

我正在尝试创建一个存储学生考试成绩的程序。

每个学生都有 4 个测试分数,必须存储在一个二维数组中。 JSwing 应用程序将允许用户输入 4 个测试标记,一旦他们单击一个按钮,它会将 4 个标记添加到数组中。

我试图通过不断收到 arrayindexboundexception 来弄清楚如何做到这一点。另外,我什至不确定我是否做得正确。

顺便说一句,这都是在JSwing完成的。

public class StudentGradesView extends FrameView {
    final static int students = 15;
    final static int numOfTest = 4;

    //DELCARING THE 2D ARRAY
    double[][] marks = new double[students][numOfTest];

    public void addInfo() {
        double testScores[] = new double[4];
        String test1, test2, test3, test4;
        double score1, score2, score3, score4;
        int i = 0;
        test1 = testOneIn.getText();
        test2 = testTwoIn.getText();
        test3 = testThreeIn.getText();
        test4 = testFourIn.getText();
        score1 = Double.parseDouble(test1);
        score2 = Double.parseDouble(test2);
        score3 = Double.parseDouble(test3);
        score4 = Double.parseDouble(test4);
        testScores[0] = score1;
        testScores[1] = score2;
        testScores[2] = score3;
        testScores[3] = score4;

        //First for loop starts at in the first row and automatically
        //increments to the next row after the first rows columns are filled
        for (int row = 0; row < marks.length; row++) {

            //Second loop automatically cycles through each column in the
            //current row adding test scores. After this loop finishes
            //the program returns to the first loop and advances to the next 
            //row, at which point it will fill that row with the same test data.
            for (int col = 0; col < 4; col++) {
                marks[row][col] = testScores[i];
                i++;
            }
        }
    }

}

这是我目前所拥有的。任何帮助表示赞赏。

【问题讨论】:

  • 为什么需要 2 个循环?
  • 2 个循环,因为一个用于行,一个用于列。第一个学生有 4 个考试成绩,第二个学生有 4 个考试成绩
  • 是的,OP 是对的——二维数组是合适的(行 = 学生,列 = 测试),因此使用两个循环来填充它也是如此。这里的大问题 - 我假设这是在 c/p 中将此代码放到此处的拼写错误 - 你的 marks 数组在哪里创建?
  • 什么是“JSwing”?我不熟悉这种野兽。
  • @drewmore 这就是为什么我对为什么 2 个循环感到困惑 - 我在任何地方都看不到 marks 数组。

标签: java arrays multidimensional-array indexoutofboundsexception


【解决方案1】:

您正在增加i 超出testScores 数组的范围。该数组的长度仅为 4。marks 的第二次迭代将导致 i &gt; testScores.length 给您异常。

考虑更新您的输入以接受学号:

int student = Integer.parseInt(studentIn.getText());

然后您可以只将测试分数输入应用到该学生:

for (int col = 0, i = 0; col < 4; col++, i++) {
    marks[student][col] = testScores[i]; 
}

此外,由于您没有执行双重 for 循环,因此不需要 i 变量,您可以改用 col

for (int col = 0; col < 4; col++) {
    marks[student][col] = testScores[col]; 
}

【讨论】:

    【解决方案2】:

    对于你原来的问题

    for(int row=0; row <marks.length; row++){
            for(int col=0;col <4; col++){
                marks[row][col]= testScores[i];
                i++;
            }
    

    正在增加 i 超出 testScores 的长度。使用这个

    for(int row=0; row <marks.length; row++){
                i = 0;
                for(int col=0;col <4; col++){
                    marks[row][col]= testScores[i];
                    i++;
             }
    

    FWIW,您还可以减少本节中的代码量、变量的数量,并最终减少为变量分配的内存量:

        test1=testOneIn.getText();
        test2=testTwoIn.getText();
        test3=testThreeIn.getText();
        test4=testFourIn.getText();
        score1=Double.parseDouble(test1);
        score2=Double.parseDouble(test2);
        score3=Double.parseDouble(test3);
        score4=Double.parseDouble(test4);
        testScores[0]=score1;
        testScores[1]=score2;
        testScores[2]=score3;
        testScores[3]=score4;
    

    通过这样做

        testScores[0] = Double.parseDouble(testOneIn.getText());
    

    【讨论】:

    • 我需要 15 个代表。新成员来了 lmao!
    • 好点。我认为您仍然应该能够通过单击解决您原始问题的答案(不是我的,因为它是一个分支)来选择您的答案。
    • 其实好像没有人回答。一分钟后重新检查此答案
    • 在这种情况下,row 也必须是一个私有类变量。
    • 是的,问题在于您的双 for 循环使用一组测试分数循环遍历整个二维数组。您要么需要按照@JeffWard 的建议单独选择每个学生,要么按照我的建议自动递增。或者,也许还有另一种解决方案,但这两个有效。
    【解决方案3】:

    我不确定是否应该添加第二个答案,但它应该是这样的

    public class StudentGradesView extends FrameView {
     final static int students=15;
     final static int numOfTest=4;
    
     //Start with the first student in the 2D array
     private int row = 0;
    
    
    //DELCARING THE 2D ARRAY
    double[][] marks = new double[students][numOfTest];
    
    
    public void addInfo(){
        double testScores[]=new double[4];
        testScores[0]=Double.parseDouble(testOneIn.getText());
        testScores[1]=Double.parseDouble(testTwoIn.getText());
        testScores[2]=Double.parseDouble(testThreeIn.getText());
        testScores[3]=Double.parseDouble(testFourIn.getText());
    
        //Add all four test scores to the current student
            for(int col=0;col < testScores.length; col++){
                marks[row][col]= testScores[col];
                }
    
        //Advance to the next student
        row++;
       }
      }
    

    【讨论】:

    • 我再次编辑了这个。我刚刚意识到您甚至不需要 int i 来跟踪 testScores 数组索引。
    猜你喜欢
    • 2013-05-01
    • 2014-03-04
    • 2013-12-09
    • 1970-01-01
    • 2015-06-17
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多