# A Guide to Two-Dimensional Arrays

### Author: Daragh Ó Tuama

A Guide to Two-Dimensional Arrays Arrays simplify the procedure and are used vastly in programming. To make things simpler, other types of arrays also exist, one of them being two-dimensional arrays (2D arrays). Now, let’s look into what are two-dimensional arrays and their many advantages in making programming simpler.

Arrays are data structures which are used in programming to store a collection of values belonging to the same data type. The stored values are stored in a contiguous memory location where each and every value, along with their indexes, are stored in a neat arrangement.

When a user wants to access a certain value, the indexes come in handy.

## What is a Two-Dimensional Array?

Two-dimensional arrays, in a nutshell, are arrays inside arrays that contain homogeneous data in tabular form.

Multidimensional arrays containing values or data represented in a grid or table style with rows and columns are called two-dimensional arrays. Each value has two indices indicating the row and column on which it is kept because it is saved in row and column dimensions.

Two-dimensional arrays can readily be used for any function and are used to store enormous amounts of data at once.

One example is the widespread use of 2D arrays in game development. Two-dimensional arrays are used in 2D games to specify the character location.

2D arrays are utilised in virtually all computer languages. Let’s examine how 2D arrays are used in other programming languages using examples since the concept is the same, but the syntax, declaration, and type of use vary.

### Size of Two-dimensional Arrays

By multiplying the sizes of each dimension, it is possible to determine the total number of elements that may be held in a multidimensional array.

## 2D Arrays in Python

In Python, 2d arrays are declared as follows:

##### Syntax:
``Array_name = [rows][columns]``

Here,

• The array name refers to the name of the array.
• The size of the array is declared.

There is also another way to declare two-dimensional arrays in python.

##### Syntax:
``Arr-name = [ [m1, m2, m3, … . mn], [n1, n2, n3, … .. nn] ]``

Here,

• The elements of the arrays are defined in the declaration itself.

Example:

``employee_dt =[ [ 65,56,78,98], [89,79,89,89], [78,56,45,89], [56,23,23,41], [45,23,12,56] ]  ``

### Accessing Elements of Two-Dimensional Arrays in Python

Using the index position, the values can be easily accessed.

##### Syntax:
``array[row index]``
• Using the above syntax, the whole row values are accessed.
``Array[row index][column index]``
• The element present in the particular row and column is accessed using the above syntax.

Example:

``````array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]
print(array)
print(array)
print(array)
print(array)
print(array)``````

## 2D Arrays in Java

In Java, 2d arrays are defined similarly to 1d arrays. The syntax to declare 2d arrays is as follows:

##### Syntax:
``data_type[1st dimension][2nd dimension] arrayname = new data_type[size1][size2];``

Here,

• The data_type refers to the data type of the array.
• The array name refers to the name of the array.
• The sizes are used to declare the size of the arrays. Size1 is a row, and size2 is a column.

Example:

``int[][] twoD_tbr= new int;``

After declaration, the next step is initialisation. The syntax is as follows:

##### Syntax:
``array_name[row_index][column_index] = value;``

Here,

• In the row_index, the row number is entered.
• In the column_index, the column number is entered.
• The value to be stored in this index is entered.

Example:

``````arr = 1;
arr = 2;
arr = 3;``````

### Types of Declaration

In two-dimensional arrays, there are two types of declaration, namely direct and indirect declaration.

#### Direct Method of Declaration

In the Direct Method of Declaration, the arrays are declared along with the values.

##### Syntax:
``````data_type[][] array_name = {
{valueR1C1, valueR1C2, ....},
{valueR2C1, valueR2C2, ....}
};``````

Example:

``int[][] tbr= {{1, 2}, {3, 4}};``

#### Indirect Method of Declaration

The indirect declaration method is the same as the usual type of declaration. The array is first declared in one statement and later initialised in another statement.

Example:

``````int[][] tbr= new int;
arr = 1;``````

### Accessing Elements of Two-Dimensional Arrays in Java

To access the element, you can simply define the array name and the index of the value you are accessing.

##### Syntax:
``x[row_index][column_index]``

Example:

In the example below, we are asking for the value stored in row 0 and column 0. The output will be the value stored in that location.

``````class GFG {
public static void main(String[] args)
{
int[][] tbr = { { 2,2}, { 5,5 } };
System.out.println("arr = " + arr);
}
}``````

Output:

``arr = 1``

## The Bottom Line

Two-dimensional arrays make data storage and access easier and more convenient. This post shows how two-dimensional arrays can be beneficial and how they’re utilised in different programming languages. Try out and understand the concept using the examples provided.

## Free Coding Basics

If you’re new to software development, some of the terms in this article may seem daunting. However, anyone can learn to code. Try our free 5 Day Coding Challenge and learn the basics of HTML, CSS and JavaScript. Register now through the form below.