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:

Array_name = [rows][columns]


  • 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.

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


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


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.

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.



2D Arrays in Java

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

data_type[1st dimension][2nd dimension] arrayname = new data_type[size1][size2];


  • 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.


int[][] twoD_tbr= new int[10][20];

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

array_name[row_index][column_index] = value;


  • 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.


arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 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.

data_type[][] array_name = {
                             {valueR1C1, valueR1C2, ....}, 
                             {valueR2C1, valueR2C2, ....}


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.


int[][] tbr= new int[10][20];
arr[0][0] = 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.



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[0][0] = " + arr[0][0]);


arr[0][0] = 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.

What is an AI Developer & How to Become One?

Right now, the world is witnessing an artificial intelligence technological revolution, and AI Developers are at the forefront. AI developers are those who design, develop, and deploy AI-powered solutions. They are shaping the technological future by creating intelligent systems and applications. We look into the world of AI developers, exploring their roles, skills, and the […]

Systems Analyst: A Guide 

A system analyst looks after a company’s computer systems and network and ensures they meet its goals. They ensure that the infrastructure, computers and other systems work efficiently.  Who do you think takes care of all the systems in a company and plans everything out for a firm to meet its needs? Who do you […]

What Does a .NET Developer Do & How to Become One?

.NET developers are essential in the realm of software development. They are in charge of planning, creating, testing, and managing software applications built with the Microsoft .NET framework. As a result, they are in high demand and can command substantial wages in the tech business. In this blog, we will look at what a .NET developer […]