-3

2D char 配列の内容を文字列に変換し、境界線で囲む必要があります。各ボックスは、呼び出されるメソッドによって異なります。クライアントを実行するたびに、エラー java.lang.ArrayIndexOutOfBoundsException: 4 on line 107 (コードでマーク) が発生し、理由がわかりません。何が間違っているかについてのアイデアはありますか?

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
}
4

3 に答える 3

2

これは私が変更するものです:

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

array.length + 2 は、配列の外側を「指す」ことを意味します

于 2012-11-19T01:19:55.740 に答える
2

あなたのelseステートメントで:

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

あなたはarray[i-1][j-1]にアクセスしていますが、あなたは私がarray.length + 2まで行くことができると言いました。境界。

于 2012-11-19T01:20:43.347 に答える
1

toString 関数では、i は array.length + 1 まで上げることができますが、array[i-1] は範囲外になり、else 句では array[i-1][j-1] にアクセスします。ブーム!

于 2012-11-19T01:23:41.143 に答える