Java 将整数读入多维数组

Java 将整数读入多维数组,java,Java,我的问题发生在for循环将文件中的值读入我的分数数组的过程中。程序读入并打印前6个值或2行整数,然后我得到ArrayIndexOutOfBoundsException:2 我不知道为什么会停在那里。如果我有j 为什么-1?这就是AIOOB的来源。有两个问题: int scores[][] = new int[play-1][par-1]; // Why -1 ? 以及: for(intj=0;j实际上,有三个问题 只有3名球员,而不是18名 您需要的是18x3阵列,而不是17x2阵列 [i][

我的问题发生在for循环将文件中的值读入我的分数数组的过程中。程序读入并打印前6个值或2行整数,然后我得到ArrayIndexOutOfBoundsException:2

我不知道为什么会停在那里。如果我有j 为什么
-1
?这就是AIOOB的来源。

有两个问题:

int scores[][] = new int[play-1][par-1]; // Why -1 ?
以及:


for(intj=0;j实际上,有三个问题

  • 只有3名球员,而不是18名
  • 您需要的是18x3阵列,而不是17x2阵列
  • [i][j]
    而不是
    [j][i]
  • 您的代码与我的修改版本的差异(其工作方式很有魅力):

    22c22
    字符串[]玩家=新字符串[par];
    24c24
    整数分数[][]=新整数[游戏][par];
    32c32
    如果(k==par)
    41,42c41,42
    分数[i][j]=infle.nextInt();
    >System.out.println(分数[i][j]);
    
    非常感谢,我不确定我对-1的想法
    int scores[][] = new int [play-1][par-1];
    
    int scores[][] = new int[play-1][par-1]; // Why -1 ?
    
    for(int j=0; j<par; j++)              // should be 'j < play' as 'j'
                                          // is index to dimension
                                          // with size 'play'
    {
        for (int i=0; i<play; i++)        // should be 'i < par' as 'i' is
                                          // index to dimension with
                                          // size 'par'
        {
            scores[j][i]=infile.nextInt();
            System.out.println(scores[j][i]);
        }
    }
    
    22c22
    <     String[] players= new String[play];
    ---
    >     String[] players= new String[par];
    24c24
    <     int scores[][]= new int[play-1][par-1];
    ---
    >     int scores[][]= new int[play][par];
    32c32
    <     if (k==play)
    ---
    >     if (k==par)
    41,42c41,42
    <         scores[j][i]=infile.nextInt();
    <         System.out.println(scores[j][i]);
    ---
    >         scores[i][j]=infile.nextInt();
    >         System.out.println(scores[i][j]);