What is a 2D array?
Think of a cinema. One number is not enough to find your seat. You need a row and a seat number. A 2D array is that seating plan in code: values laid out in rows and columns, where every value has a two-part address.
Under the hood it is simply an array whose items are arrays. Each inner array is one row of the grid, which is why you write it with two sets of square brackets.
Reading matrix[row][col]
The first index picks the row. The second index picks the column inside that row. Counting starts at 0, so matrix[1][2] means the second row, third column.
[[5, 8, 2], [7, 1, 9]] matrix[0][1] → 8 matrix[1][2] → 9
Try it above: switch on Show indices and hover any cell to see its address. Row first, then column. Mixing the two up is the mistake everyone makes exactly once.
Visiting every cell
To touch every value you need two loops. The outer loop walks down the rows, and the inner loop walks across the columns of the current row. Count the steps on a grid with 3 rows and 4 columns: 4 steps for the first row, 4 for the second, 4 for the third. That is 12 steps for 12 cells.
So the work grows with the number of cells. For a grid with r rows and c columns, visiting everything takes r × c steps, which is written O(r · c). Reading or changing a single cell is different: you jump straight to its address, one step, no matter how big the grid is. Watch the loops run on the traversal pages: row by row, down the columns, in a spiral or along the diagonals. New to this notation? The Big-O Playground starts from zero →
How a grid sits in memory
Computer memory is one long line, not a grid. In C and C++ a 2D array is stored row by row: all of row 0, then all of row 1, and so on. This is called row-major order, and it explains a classic tip. Walking a grid row by row is usually faster than walking it column by column, because the next value you need is sitting right beside the one you just read.
In Python and JavaScript each row is its own separate list, and the outer array just points to them. That is why rows there can have different lengths, and why the bug below exists.
Common beginner mistakes
- Swapping row and column.
matrix[col][row]works on a square grid and then crashes on a rectangular one. - Going one past the end. With 3 rows the valid row numbers are 0, 1 and 2. The last row is always
rows - 1. - The shared-row bug. In Python,
[[0] * 3] * 3builds one row and points to it three times, so changing one cell changes a whole column. JavaScript'sfilldoes the same. The Code tab above shows the safe way to build a grid. - Assuming every row is the same length. Always loop to the length of the current row, not the first one.
How to use this visualizer
- Paste a grid in square-bracket form, like
[[1, 0], [0, 1]], or start from a preset such as the chess board. - A plain 1D array such as
[3, 1, 4]works too. It is drawn as a single row. - Other formats are read as well: rows of values separated by spaces, commas or tabs, one row per line (with an optional first line giving the size, the way judge input does), Python lists with single quotes and
True/False/None, and a stray trailing comma. The badge above the input shows how your text was understood. - Cells can hold numbers, text in quotes,
true,falseornull. - Give each value its own color to make patterns jump out: walls and paths in a maze, the 1s in an adjacency matrix, or the pieces on a board.
- Rows of different lengths are padded so you can see where they end.
- Working with a single list? The 1D Array Visualizer animates insert, delete, search and reverse on it, one read and write at a time.
Share a grid with your class. Press Copy link above the input to get a link that opens this exact grid, with your colours and index labels. Put it in a slide, a worksheet or a group chat and everyone sees the same thing. The grid travels inside the link itself, so nothing is stored on a server.
Teaching with this tool? Tell me what would help your class →
Questions people ask
What is the difference between a 1D array and a 2D array?
A 1D array is a single line of values, and one index is enough to find any of them. A 2D array is a grid, so every value needs two indexes: one for its row and one for its column. You can paste either kind into the visualizer; a 1D array is shown as a single row.
Is a matrix the same thing as a 2D array?
In programming, almost always yes. Matrix is the math word for a rectangular grid of numbers, and a 2D array is how you store one in code. A 2D array is a little more relaxed: it can hold text or true and false values, and its rows do not have to be the same length. To transform one, the matrix operation pages transpose, rotate, flip and multiply a matrix one cell at a time.
Why do the row and column numbers start at 0?
An index tells the computer how far to move from the start. The first row is zero steps away from the start, so it is row 0. It feels odd for about a week and then becomes automatic. It also means the last row is always the number of rows minus one.
Can the rows of a 2D array have different lengths?
In Python, JavaScript, Java and C++ vectors, yes. Each row is its own list, so nothing forces them to match. These are called jagged or ragged arrays. The visualizer draws a dotted placeholder where a shorter row ends so you can see the gap straight away.
How do I visualize a 2D array from my C++ or Python program?
From Python, print the list and paste it as it is: single quotes and True, False and None are understood. From C++ or Java, either swap the curly braces for square brackets, or print the values with spaces between them, one row per line, which the input box reads directly. Judge-style input with a size line on top works too.
Is the 2D array visualizer free?
Yes. It is free and open source under the MIT license, it needs no account, and it runs entirely in your browser, so the data you paste never leaves your computer.
Can I embed the visualizer in my own page?
Yes. On any tool, click Embed next to Copy link and paste the iframe snippet into your course page, blog or learning platform. The embedded page keeps the grid, the controls and the Learn tabs, hides the site header and footer, and carries a small link back to the full site.