# Multi-dimensional array in Programming Languages

0 like 0 dislike
1.3k views

edited

In this post, we're gonna learn about Multi-dimensional arrays in

• Python,
• C#,
• Java.

Before we begin, let's see the part one that we talk about a one-dimensional array

## Multidimensional array:-

• is an array with more than one dimension it can be two , three,...Nth dimension.
• size of the array  can be calculated as the total number of elements that can be stored in a multi-dimensional array can be calculated by multiplying the size of all the dimensions

## What is a two-dimensional array?

• A two-dimensional array is the simplest form of multi=dimensional array
• A two-dimensional array is a two-dimensional series represented by rows and columns.
• It is used to store information in a matrix "in a tabular form".
• It can be defined as an array of arrays.

## How can we access the two-dimensional array elements?

• Each element in the two-dimensional array has row index and column index, it is accessed by these indices.
• Both row-index and column-index start at 0.

## Two-dimensional Arrays in C#

• C# support multi-dimensional arrays, we will talk about the two-dimensional form.
• It is accessed by using the two indices.
• Both row-index and column-index start at 0.
• the two-dimensional is declared with [ , ] after the data type.

### Two-dimensional array declaration in C#

```datatype[ ,] arrayName;
```
• Data Type  : It defines the element type of the array.
• [ , ] : It defines the size of the array depending on row and column indices;
• arrayName: It is the Name of array
• the three-dimensional declared with [ , , ] ...etc.

here are two examples of initializing a two-dimensional array

### Access the two-dimensional array element in C#

Each element in the two-dimensional array has row-index and column-index that we can access the element by them.

The example shows you what will you do print elements in the array depending on the indices:-

### Two-dimensional array length in C#

• The length can be calculated using array.length
• The total no of elements can be calculated by multiplying the size of all the dimensions.
`int [,] arr; int arrlength= arr.Length; // x*y the total numbers of elements`

or you can get with  multiplying all dimension

```for (int i = 0; i < arr.Rank; i++) {
total *= array3D.GetLength(i);
}```

### Looping array elements in C#

by using the for loop and the( width and height )of the 2d array we can looping the 2d array elements.

## Two-dimensional Arrays in Python

Python support multi-dimensional arrays.

• It is an array of array.
• It is accessed by using the two indices instead of one.

### multi-dimensional array declaration in Python

• A two-dimensional array is multiple lists within a list, where each list is a collection of values and each list is arranged in a separate row.
• There are many structures you can use for storing a 2D array.
• You can use function zeros() where all the elements of each array will be initialized to zero .
``` from numpy import zeros
# Calling zeros() to create an int array of 2 rows and 2 columns
a = zeros([2,2], int)
print(a)  ```

the output is  [[0 0] [0 0]]

• You can use function ones() where all the elements of each array will be initialized to one.
```from numpy import ones
# Calling ones() to create an int array of 2 rows and 2 columns
a = ones([2,2], int)
print(a) ```

the output is [[1 1]  [1 1]]

• You can use a function array() function to initialize the 2d array.

```from numpy import array
# Calling array() to create an int array of 2 rows and 3 columns
a = array([[5,4,3], [4,2,1]], int)
print(a)```

the output is [[5 4 3] [4 2 1]]

Access the multi-dimensional array element in Python

• Each element in the two-dimensional array has row-index and column-index that we can access the element by them.

### multi-dimensional array length in Python

• The length can be calculated using len()
• you can get the height and the width of the array "list" by using len() function

### Looping multi-dimensional elements in Python

by using the for loop

•  the for-loop variable in Python can iterate not only over a range(), but generally over all the elements of any sequence.

the output will be

```1 2 3 4

5 6

7 8 9 ```

## Multi-dimensional Arrays in java

• Java support multi-dimensional arrays
• IT is an array of array.
• It is accessed by using the two indices.
• Both row-index and column-index start at 0.
• The two-dimensional is declared with [ ][ ] after the data type.
• The multi-dimensional array is declared as [ ][ ][ ]....[n]

### Two-dimensional array declaration in C#

```datatype[][] arrayName;
```

Data Type  : It define the element type of the array.
[ ][ ] : It define the dimensions of the array
arrayName: It is the Name of array

• the three-dimensional declared with [  ][  ][ ]
• multi-dimensional array declared with [ ][ ][ ].....[Nth dimension].

here are two examples of two-dimensional and three-dimensional  array

### Access the two-dimensional array element in Java

• Each element in the two-dimensional array has row-index and column-index that we can access the element by them.

The example shows you what will you do to print elements in the array depending on the indices:-

### Two-dimensional array length in java

• The length can be calculated using array.length that will be the number of arrays inside it.

### Looping two-dimensional array elements in java

by using the for loop and the( rows and columns)of the two-dimensional array we can be looping the two-dimensional array elements.

Looping three-dimensional array in java