0

Currently I created a 2D array to represent a grid for a maze. My maze constructor is as follow:

public Maze(int rows, int columns)
{
    this.rows = rows;
    this.cols = columns;
    curr = new Square[rows][columns];
}

with the following in my testing class:

Maze m = new Maze(4, 4);

However, when I am traversing through my maze, while debugging I have noticed that curr is initialized to be Square[4][] with no arguments for the columns. Does anyone have any idea what the problem may be here?

Edit: That is what I'm intending to do; I make curr = Square[rows][columns] yet when I check the value of curr while in the following loop, in the debugger tool curr has the value Square[4][] for whenever it steps into curr[i][j] in the loops.

    for(int i = 0; i < maze.length; i++)
    {
        for(int j = 0; j < maze[i].length; j++)
        {
            /* Entrance */
            if(maze[i][j] == start)
            {
                startX = j;
                startY = i;
                curr[i][j] = new Square(i, j, start, this);
            }
            /* Exit */
            else if(maze[i][j] == end)
            {
                endX = j;
                endY = i;
                curr[i][j] = new Square(i, j, end, this);
            }
            /* Traversable Squares */
            else if(maze[i][j] == traverse)
            {
                curr[i][j] = new Square(i, j, traverse, this);
            }
            /* Non-traversable Squares */
            else
            {
                curr[i][j] = new Square(i, j, noTraverse, this);
            }
        }
    }
7
  • I would use your debugger to step through here to see what the value of columns is when you are assigning curr. And how do you know that it is initialized as Square[4][]? Commented Sep 10, 2012 at 20:04
  • When I use my debugger to step through my maze, curr is always Square[4][] Commented Sep 10, 2012 at 20:11
  • what does Square[4][] mean ? do you mean something like Square[4][0]? Commented Sep 10, 2012 at 20:20
  • 1
    also new Square[rows][columns]; doesn't make sense to me.. maybe you meant int[][] square = new int[rows][columns] ? Commented Sep 10, 2012 at 20:25
  • 1
    new Square[rows][columns] creates an array of Square objects whose dimensions are rows by columns. Commented Sep 10, 2012 at 20:40

2 Answers 2

1

Java uses 'ragged arrays' for it's multidimensional arrays. In otherwords, even when you specify a rectangular array - one where all the rows have the same length, java allows for the possibility that the rows may be of different lengths.

As a result, curr = new Square[r][c] creates r+1 objects:

1 array with length r, where each element is an array of Squares (Square[])
and
r arrays of Squares, each of length c (although c could be changed for any of them)

So curr IS in fact a Square[r][]: It is an r-element array of variable-length Square Arrays, each of which just happens to be c elements in length.

curr[n] should be a Square[], while curr[n][m] is a Square

Sign up to request clarification or add additional context in comments.

Comments

1

enter image description here

Is this what you're seeing? In this case, the debugger only knows that curr is an array of Square arrays, not how many elements are in the sub-arrays. new Square[4][4] is a short-cut that automatically creates the inner array. Another way to do it would be:

Square[][] curr = new Square[4][];

for (int ctrOut = 0; ctrOut < 4; ctrOut++) {
    curr[ctrOut] = new Square[5];

    for (int ctrIn = 0; ctrIn < 5; ctrIn++) {
        curr[ctrOut][ctrIn] = new Square();
    }
}

So what the debugger is telling you appears to be correct. (Note, I used 4x5 for clarity, instead of 4x4.)

1 Comment

Yes, that is exactly what it was showing me. I thought this may have been an error, but now that it has been explained it makes sense to me. Thanks a lot guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.