Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么数组索引超出范围?_Java - Fatal编程技术网

Java 为什么数组索引超出范围?

Java 为什么数组索引超出范围?,java,Java,我需要将2D字符数组的内容转换为字符串,并用边框将其包围。无论调用什么方法,每个框都是不同的。无论何时运行客户端,我都会在代码中标记的第107行得到错误java.lang.ArrayIndexOutOfBoundsException:4,我不知道为什么。有什么问题吗 public class TwoDArrayFiller { private char [][] array; public TwoDArrayFiller(char[][] dataArray) { a

我需要将2D字符数组的内容转换为字符串,并用边框将其包围。无论调用什么方法,每个框都是不同的。无论何时运行客户端,我都会在代码中标记的第107行得到错误java.lang.ArrayIndexOutOfBoundsException:4,我不知道为什么。有什么问题吗

public class TwoDArrayFiller
{
        private char [][] array;

public TwoDArrayFiller(char[][] dataArray) 
{
    array = new char[dataArray.length][dataArray[0].length];
    for(int i = 0; i < dataArray.length ; i++ )
    {
        for( int j = 0; j < dataArray[0].length ; j++ )
        {
            array[i][j] = dataArray[i][j];
        }
    }
} //end constructor

public void fillRows()
{

    for ( int i = 0; i < array.length ; i+=2 ) 
    {
        for( int j = 0; j < array[i].length; j++ )
        {
            array[i][j] = 'X';
        }
    }
} // end fillRows
public void fillColumns()
{

for( int i = 0; i < array.length; i++ )
{
    for (int j = 0; j < array[0].length; j+=2 )
    {
        array[i][j] = 'X';
    }
}

} // end fillcolumns
public void border()
{

    for( int i = 0; i < array.length; i++ )
    {   
        for( int j = 0; j < array[0].length; j++ )
        {
            if ( i == 1 || i == 2 )
            {
                array[i][j] = 'X';
                j+=2;
            }
            else
            {

                array[i][j] = 'X';
            }
        }
    }
}// end border
public String toString()
{
    String output = new String();

    for( int i = 0; i < array.length + 2; i++ )
    {
        for( int j = 0; j < array[0].length + 2; j++ )
        {
            if( ( i == 0 && j == 0 ) || (i == 0 && j == ( array[0].length + 2 )) )
            {
                output = output + "+";
                if ( j == ( array[0].length + 2 ) )
                output = output + "\n";                                         
            }
            else if( i == ( array.length + 2 ) && j == 0 )
            {
                output = output + "+";
            }
            else if( ( i == 0 && j == ( array[0].length + 2 ) ) || ( i == 5 && j == ( array[0].length + 2 ) ))
            {
                output = output + "+\n";
            }
            else if ( i == 0 || i == ( array[0].length + 2 ) )
            {
                output = output + "-";
            }
            else if( j == 0 || j == ( array[0].length + 2 ) )
            {
                output = output + "|";
                if( j == ( array[0].length + 2 ) )
                {
                    output = output + "\n";
                }
                else{
                }
            }
            else
            {
                output = output + array[i - 1][j - 1]; // here is where the error occurs
            }
        }
    }
    return output;
}// end tostring
}

这是我要改变的:

for( int i = 0; i < array.length + 2; i++ )

array.length+2表示要在else语句中指向数组外部

else
            {
                output = output + array[i - 1][j - 1];
            }

您正在访问array[i-1][j-1],但您说过我可以上升到array.length+2,这一点指向array[array.length+1],这将超出范围。

在toString函数中,您的i可以上升到array.length+1,但是
数组[i-1]将不受限制,但在else子句中您可以访问数组[i-1][j-1]。轰

请编辑您的问题以说明发生异常的行-stacktrace将告诉您此信息。因为它是。这是一个人(即您)了解调试的地方。耶!哪一行导致上述异常?哪些价值观?为什么?我