简体   繁体   English

嵌套for循环内的Java越界异常

[英]Java out of bounds exception inside nested for loop

this may be an easy one, but I'm getting an out of bounds exception and I'm not sure how to fix it. 这可能很容易,但是我遇到了一个异常,而且我不确定如何解决它。

Basically, I am trying to create a "table" of integer fields so that I can use them to find if all of the values in the integer fields create a magic square. 基本上,我试图创建一个整数字段的“表”,以便我可以使用它们来查找整数字段中的所有值是否都创​​建了一个幻方。 The nested for loop should create up to an 8x8 square, and it will create the first row of the square, but instead it gives me an out of bounds error. 嵌套的for循环最多可以创建一个8x8的正方形,它将创建正方形的第一行,但是却给了我一个超出范围的错误。

The error occurs inside of the nested for loop where I'm adding the IntegerField to the GUI. 错误发生在嵌套的for循环内部,其中我将IntegerField添加到GUI。

If anyone can help, that would be great. 如果有人可以帮助,那就太好了。 Let me know if you need more details. 让我知道您是否需要更多详细信息。

import javax.swing.*;
import BreezySwing.*;

public class Interface extends GBFrame{ 
    //Create integerField array to create input for magic square
    public IntegerField[][] magicSquare;
    //Create input button, integer field which sets size of square
    public IntegerField squareSize;
    public JButton inputSize;
    //Create check square button
    public JButton checkSquare;
    //Label to output if there is a magic square
    public JLabel squareLabel;
    //Size of square variable
    public int size;

    //CalcSquare object
    CalcSquare calc = new CalcSquare();

    //Constructor for Square interface
    public Interface()
    {
        squareSize = addIntegerField (0, 1, 1, 1, 1);
        inputSize = addButton ("Input Size", 2, 1, 1, 1);
        squareLabel = addLabel ("", 3, 1, 1, 1);
        checkSquare = addButton ("Check Square", 4, 1, 1, 1);
    }   
    //Creates IntegerFields on the GUI as needed.
    public void createFields()
    {
        for (int i = 0; i <= size; i++)
        {
            for (int x = 0; x <= size; x++)
            {
                magicSquare = new IntegerField[i][x];
    }
        }
    }

public void buttonClicked(JButton buttonObj)
{
        if (buttonObj == inputSize)
        {
            size = squareSize.getNumber();
            createFields();
            for (int i = 0; i <= size; i++)
            {
                for (int x = 0; x <= size; x++)
                {
                    magicSquare[i][x] = addIntegerField (0, i+1, x+1, 1, 1);
                }
            }   
        }
        else if (buttonObj == checkSquare)
        {       
        }
    }
}

A for loop condition of i <= size Should always raise red flags since if i == size, you've gone beyond the size of the array or collection. i <= size for循环条件应该始终引发红色标记,因为如果i == size,则超出了数组或集合的大小。 Note that arrays and collections are 0 based and go from 0 to size - 1. 请注意,数组和集合基于0,从0到size-1。

It should instead almost always be i < size 相反,它几乎应该总是i < size

All your loops are iterating upto size which will cause ArrayIndexOutOfBoundException. 您的所有循环都迭代到最大大小,这将导致ArrayIndexOutOfBoundException。 The Array index starts from 0 to size-1 . 数组索引从0size-1 Here is one of such loop in your code: 这是代码中的此类循环之一:

for (int i = 0; i <= size; i++)

you need to iterate the loop only till size 您只需要迭代循环直到大小

for (int i = 0; i < size; i++)

Correct other loops accordingly 相应地纠正其他循环

size is never initialized. size从未初始化。 And your <= should be < in the for loops. 并且您的<=应该是for循环中的<

In fact, if you're using size as a constant to set the size of your arrays, you should use i < size - 1 in for loops. 实际上,如果您使用size作为常量来设置数组的大小,则应使用i < size - 1 in for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM