java – Arrays. The fill() method caused an exception

Now I can't fill the array with spaces Whenever I use the array fill method, I encounter exceptions Now I've eliminated the rest of the code, including only the code that caused the problem Here is. Note that I am a beginner of Java, so if this problem is too simple, please don't be angry I searched here and couldn't find anything

public class board 
{       
    public static void main(String args[])
    {
        char board [][] = new char  [6][7]; 
        int column=0; 
        int row=0;

        java.util.Arrays.fill(board,' ');
    }
}

Exception theory

Solution

Arrays. Fill needs a one - dimensional array. You pass a jagged array

Instead, do this:

for(int x=0;x<board.length;x++)
    for(int y=0;y<board[x].length;y++)
        board[x][y] = ' ';

Or this:

for(int x=0;x<board.length;x++)
    Arrays.fill( board[x],' ' );
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>