0

I was making boards for the tic-tac-toe/connect four/mastermind when I encountered this strange error.

public Board(int rows, int columns) {
        cells = new Cell[rows][columns];
        for(int r = 0; r < cells[0].length; r++) {
            for(int c = 0; c < cells[1].length; c++) {
                cells[r][c] = new Cell(r, c);       
        }
    }

When I tried to make a 3 x 3 board for tic-tac-toe, there was no problem whatsoever. However, when I tried to make 6 x 7 board for Connect 4, the following error message was displayed:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6
    at games.board.Board.<init>(Board.java:10)
    at games.board.BoardGameTester.main(BoardGameTester.java:7)

By debugging the program, I realized that when the number of columns is bigger than the number of rows, an error occurs. I also realized that the error occurs because the outer for loop increments once more than the length of the row. But for loop incrementing without executing first? Wow.

I'm pretty sure that the increment statement is executed after the execution of the body.

Jack
  • 11
  • 3
  • `r < cells[0].length` -> `r < rows` and `c < cells[1].length` -> `c < columns` –  Apr 12 '21 at 01:49
  • 1
    I think you meant to put `r < cells.length` and `c < cells[0].length`. – Henry Twist Apr 12 '21 at 01:53
  • **cells[0].length** is length of zero'th element, may be you need **r < rows and c < columns** – sanjeevRm Apr 12 '21 at 01:58
  • 1
    @sanjeevRm that's much more concise, although the array is constructed in a way that `cells.length == rows` and `cells[0].length == columns`. So practically there isn't a difference. – Henry Twist Apr 12 '21 at 02:05
  • @HenryTwist Thanks for the help! Your version and saka1029's version both worked perfectly for me. However, I still have a question for you. So when I say of multiple dimensional arrays, indexes for the dimensions start at nothing? According to your comment, _cells.length_ represents the length of the first dimension (row)? – Jack Apr 12 '21 at 23:51
  • Also, in the previous code, why didn't the clause _cells[1].length_ didn't cause an error since it's out of bounds of the dimensions? – Jack Apr 12 '21 at 23:58
  • I think it's best to stop thinking about it as a multi-dimensional array, and more an array of arrays. So `cells[0]` just represents the top "row" of your grid, and `cells[1]` represents the second. Therefore both have the same length/size which is why it doesn't make a difference. It generally is good practice though to use `0`, which works even if your first dimension is 1. – Henry Twist Apr 13 '21 at 00:01
  • Remember also that int[][] doesn't even necessarily represent a grid/matrix, as each inner array can have different lengths. You could have `{1, 2, 3}` as the first element/row and `{4}` as the second. – Henry Twist Apr 13 '21 at 00:02
  • @HenryTwist After about 30 mins of just looking at your comment, I finally understood what you're saying! Since _cell.length_ doesn't have the [ ], it means the length of the *most outer* array, not the length of the arrays within the array! I'm overwhelmed right now... Thank yoooouuuu!!!!! – Jack Apr 13 '21 at 02:59
  • You've got it! Good luck with your project! – Henry Twist Apr 13 '21 at 07:49

0 Answers0