Login | Register

Nerd Paradise

Artisanal tutorials since 1999

This is one of those odd topics that even for professionals, seems easily forgotten. So here it is. The 2D array quick reference.

Creating a 2D Array in Java

int[][] grid = new int[10][];

This creates an array of size 10 capable of holding int[]'s. It starts out filled with default values (null), so it isn't quite a grid yet. You need to loop through and populate it yourself.

Creating a 2D Array in C#

C# is one of those rare languages that has support for a true 2D array built into its syntax.

int[,] grid = new int[10, 10];

This creates a 2D array of integers that start out at 0.

Furthermore, you can do this in as many dimensions as you want. If you want to make a 10 x 10 x 10 cube of integers, you can do that...

int[,,] cube = new int[10, 10, 10];

Creating a (Jaggy) 2D Array in C#

But of course C# still supports jaggy arrays. The initialization code is exactly like Java's...

int[][] grid = new int[10][];

This creates an array of length 10 capable of holding int[]'s. This array starts out filled with nulls. You'll have to loop through it yourself to fill it up to create a true grid.

Creating a 2D Array in Python

Python, like most languages doesn't support a 2D array (list) type at the syntax level. But its list type can hold anything so really it's just a matter of creating a list filled with other lists. Furthermore, you can use list comprehensions to make one in one line...

grid = [[None] * dimension2] for _ in range(dimension1)

It's important to note that you can't do the following:

grid = [[None] * dimension2] * dimension1 # THIS IS WRONG

This will create a single inner list of length dimension2 and repeat the same list reference for each column. That means when you modify one cell in the grid, the same cell in all the columns will update as well. That is probably not what you wanted.

Creating a 2D Array in JavaScript

JavaScript is the worst in this regard. There's no built-in 2D list type, no list comprehension syntax like Python to allocate everything in one line, and no easy way to just create a list of length n. Making a grid in JavaScript is a fairly manual process.

var grid = [];
for (var x = 0; x < width; ++x) {
  var column = [];
  for (var y = 0; y < height; ++y) {
    column.push(null);
  }
  grid.push(column);
}

Creating a 2D Array in C

C is similar to C# and Java's syntax, with the slight difference that the brackets appear after the variable name and contains a column and row count.

int grid[2][3];

This allocates a continuous memory space of 6 integers onto the stack. Once this function is over, these values will be reclaimed. If you want to allocate to the heap, then you have to use malloc and have to work with pointers.

Since an array is really just a pointer to the first item in the array and a grid is an array of arrays, the type for a 2D grid of integers is int**.

Of course you'd write this with a loop, but for the sake of brevity...

int** grid = (int**) malloc(sizeof(int*) * 2);
grid[0] = malloc(sizeof(int) * 3);
grid[1] = malloc(sizeof(int) * 3);
printf("bottom right corner contains a %d\n", grid[1][2]);

Please note that describing C's syntax as being "similar to C# and Java's syntax" was a call to arms for people to shout angrily in the comment thread about how Java and C# copied C.