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[0])
print(array[2])
print(array[0][2])
print(array[2][3])

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[10][20];

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

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

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

Output:

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.

Is now a good time to become a software developer?

Recent stories about layoffs in the tech industry have made some people ask themselves whether or not now is a good time to become a software developer. It’s a natural question to ask, especially considering how we perceive companies like these as the largest “tech” companies. However, technology will not stop growing regardless of their […]

25 Great UX Portfolio Examples 

Visual appeal is the first and foremost factor for every designer who wants to stand out from the mass to exhibit their novel designs. So how would you showcase your talents to acquire new clients? First, you must work on creating visually appealing and beautiful designs, and later you should exhibit those designs through a […]

HTML & Accessibility   

It’s crucial to consider accessibility while planning and developing websites for all audiences. For example, ensure that websites are accessible to blind or visually impaired individuals due to the graphic and complex nature of web pages. Any website may be given access to users of assistive devices like screen readers with a little extra effort […]