【问题标题】:JLabel[][] array index out of bound errorJLabel[][] 数组索引超出范围错误
【发布时间】:2013-04-02 11:07:34
【问题描述】:

我在以下代码中得到一个数组索引超出范围异常。我使用了调试器,这就是发生的事情。


JLabel[][] labelHolder = new JLabel[8][8]; 

    for(int i=0; i<8; i++){
        for(int j=0; i<8; j++){
            labelHolder[i][j] = new JLabel ();   <- error occur right on this line when i=j=0

我不知道为什么会这样,因为如果我只是将 i 和 j 换成 0 和 0,它就可以完美地工作:S

【问题讨论】:

  • 四个重复的答案...

标签: java arrays exception indexing


【解决方案1】:

第二个for-loop 中的条件检查i 的值而不是j

for(int j = 0; i < 8 ; j++)

应该是

for(int j = 0; j < 8 ; j++)

【讨论】:

    【解决方案2】:

    用途:

    for(int j=0; j<8; j++){
                 ^
    

    【讨论】:

      【解决方案3】:

      for(int j=0; i&lt;8; j++){这一行有问题,应该是j&lt;8

      【讨论】:

        【解决方案4】:

        在您的第二个循环中,您的停止条件错误:将 i &lt; 8 替换为 j &lt; 8

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-22
          • 2017-05-06
          • 1970-01-01
          相关资源
          最近更新 更多