Tags
Asked 3 years ago
9 Jun 2021
Views 225
dilip

dilip posted

How do I declare a two dimensional array?

How do I declare a two dimensional array?
sachin

sachin
answered Apr 26 '23 00:00

In many programming languages, you can declare a two-dimensional array by specifying the number of rows and columns. Here are some examples in different programming languages:

Java


int[][] array = new int[3][4]; // creates a 3x4 array of integers

C++


int array[3][4]; // creates a 3x4 array of integers

Python


array = [[0 for i in range(4)] for j in range(3)] # creates a 3x4 array of integers

JavaScript


let array = new Array(3); // creates an array with 3 elements
for (let i = 0; i < array.length; i++) {
  array[i] = new Array(4); // each element is an array with 4 elements
}

In general, you can create a two-dimensional array by creating an array of arrays. The outer array represents the rows and the inner arrays represent the columns. You can then access elements of the array using two indices, one for the row and one for the column.
Post Answer