2D Arrays
Overview
The demo below allows you to simulate a 2-dimensional array. You can select whichever language you are working with from the dropdown box.
How to Reference an Item
When referencing an item in a 2D array, the row-index comes first followed by the column-index. This means that it can be a little confusing because assuming rows run up and down and columns run left to right, we are actually putting the y-coordinate before the x-coordinate.

A Few Things to Remember
- 2D arrays will have both an row-index and a column-index.
- The first item in a 2D array is at location (0, 0). The last item is at the location that is one less than the number of rows in the y and one less than the number of columns in the x. (e.g. a 7x4 array of items would have locations from (0, 0) to (6, 3) because it has 7 rows and 4 columns).
- The items in a 2D array can be of any type; they are not limited to the four basic options provided in this tool (integer, double, string, and boolean). However, every item in the array must be of that same single type.
- The language you selected in the dropdown above will not impact the behavior of the 2D array; it simply helps guide how you put your array into code.
Creating a 2D Array
In order to use a 2D array, you first must create and initialize it. In the section directly below, specify a few important properties of the array.
Creating & Initializing |
|
2D arrays may contain many different values. However, every item in the array must be of the same type. For example, you can't have an array containing both numbers and words. |
|
Give the 2D array a name. This will not impact how the array actually works, it will just help you keep track of it in your code. |
|
Finally, initialize the 2D array. This determines what the values in the array will look like when it is first created. Specify how many rows and columns there will be, and the array will be filled with "empty" default values. |
|
What the Code Looks Like:
|
This is what the code looks like to create and initialize the 2D array in your selected language using the properties you specified. |
Current 2D Array
The table below gives a live look at the current 2D array you just created using the properties you specified above. Feel free to go back and change those properties at any time, however note that those changes may reinitialize the values currently stored in the array (clearing them).
Values can be located using a 2-axis system with the row-index on the left and the column-index on the right. Use the sections below the table to read the value of specific items and modify the value of items. When you read or set an item, the indices and the value will be highlighted in red to show where the action took place.
Click this button to reset the 2D array contents back to the initialized values. |
IF YOU CAN SEE THIS THERE IS A JAVASCRIPT ERROR (Try reloading the page I guess) :( |
---|
Getting A Specific Value |
|
The value of the item at the location you specified will be read and outputted. For example, if the item at row 3 column 2 contains the number 21, reading from (3, 2) will output "21". |
|
What the Code Looks Like:
|
This is what the code looks like to read a value from the 2D array in your selected language. Note that we are also adding a print statement to display the value. |
Setting A Specific Value |
|
The value at the location you specified will be changed to the new value you provided. |
|
What the Code Looks Like:
|
This is what the code looks like to set a value in the 2D array in your selected language. |