java嵌套for()循环throws ArrayIndexOutOfBoundsException [英] java nested for() loop throws ArrayIndexOutOfBoundsException

查看:155
本文介绍了java嵌套for()循环throws ArrayIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Java书中的JPanel练习。我的任务是使用GridLayout创建一个5x4的网格。



当我循环访问容器来添加面板和按钮时,第一个add()抛出OOB异常。我做错了什么?

  package mineField; 

import java.awt。*;
import javax.swing。*;

@SuppressWarnings(serial)

public class MineField extends JFrame {

private final int WIDTH = 250;
private final int HEIGHT = 120;
private final int MAX_ROWS = 5;
private final int MAX_COLUMNS = 4;

public MineField(){

super(Minefield);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

容器mineFieldGrid = getContentPane();
mineFieldGrid.setLayout(new GridLayout(MAX_ROWS,MAX_COLUMNS));

//循环通过数组,添加面板,然后向面板添加按钮。 (int i = 0; i {
JPanel [] rows = new JPanel [i];

mineFieldGrid.add(rows [i],rows [i] .getName());
rows [i] .setBackground(Color.blue); (int j = 0; j< MAX_COLUMNS; j ++)

{
JButton [] buttons = new JButton [i];
rows [i] .add(buttons [j],buttons [j] .getName());
}
}

mineFieldGrid.setSize(WIDTH,HEIGHT);
mineFieldGrid.setVisible(true);

}

public int setRandomBomb(Container con)
{

int bombID;

bombID =(int)(Math.random()* con.getComponentCount());
return bombID;


}
/ **
* @param args
* /
public static void main(String [] args){

// int randomBomb;
// JButton bombLocation;

MineField minePanel = new MineField();
// minePanel [randomBomb] = minePanel.setRandomBomb(minePanel);

}

}


$ b $我确定我过度工程了一个简单的嵌套for循环。由于我是Java的新手,请善待。我确定我会有一天会回来。

解决方案

JPanel [] rows = new JPanel [i];



i 在第一次迭代中为0,你想要什么这样做:



JPanel [] rows = new JPanel [MAX_ROWS];



另外,我想你想把它完全从的循环之外,因为你似乎正在使用它的元素,这将是未初始化的。



这也是错误的:



JButton [] buttons = new JButton [ i];



可以为0,当 j例如,是2,在这种情况下,没有像按钮[j] 这样的东西。使它们全部 MAX _ * ,我想你想把它们从循环中移除,因为我在每次迭代中都没有看到重新创建它们。此外,您还需要实例化各个数组元素。


I'm working through a JPanel exercise in a Java book. I'm tasked with creating a 5x4 grid using GridLayout.

When I loop through the container to add panels and buttons, the first add() throws the OOB exception. What am I doing wrong?

package mineField;

import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")

public class MineField extends JFrame {

private final int WIDTH = 250;
private final int HEIGHT = 120;
private final int MAX_ROWS = 5; 
private final int MAX_COLUMNS = 4;

public MineField() {

    super("Minefield");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container mineFieldGrid = getContentPane();
    mineFieldGrid.setLayout(new GridLayout(MAX_ROWS, MAX_COLUMNS));

    // loop through arrays, add panels, then add buttons to panels.
    for (int i = 0; i < MAX_ROWS; i++) {
        JPanel[] rows = new JPanel[i];

        mineFieldGrid.add(rows[i], rows[i].getName());
        rows[i].setBackground(Color.blue);

        for (int j = 0; j < MAX_COLUMNS; j++) {
            JButton[] buttons = new JButton[i];             
            rows[i].add(buttons[j], buttons[j].getName());
        }
    }

    mineFieldGrid.setSize(WIDTH, HEIGHT);
    mineFieldGrid.setVisible(true);

}

public int setRandomBomb(Container con)
{

    int bombID;

    bombID = (int) (Math.random() * con.getComponentCount());
    return bombID;


}
/**
 * @param args
 */
public static void main(String[] args) {

    //int randomBomb;
    //JButton bombLocation;

    MineField minePanel = new MineField();
    //minePanel[randomBomb] = minePanel.setRandomBomb(minePanel);

}

}

I'm sure I'm over-engineering a simple nested for loop. Since I'm new to Java, please be kind. I'm sure I'll return the favor some day.

解决方案

JPanel[] rows = new JPanel[i];

i is 0 in the first iteration, which isn't what you want. Make that:

JPanel[] rows = new JPanel[MAX_ROWS];

Also, I think you want to take that completely outside the for loop, since you seem to be using its elements, which would be uninitialised...

This is also wrong:

JButton[] buttons = new JButton[i];

i can be 0 when j is 2 for example, in which case there's no such thing as a buttons[j]. Make them all MAX_* and I think you want to take them out of the loop, since I don't see the point in recreating them at every iteration. Also, you need to instantiate the individual array elements as well.

这篇关于java嵌套for()循环throws ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆