There are majorly 4 types of arrays
1. Fixed Size Array
2. Dynamic Sized Array
3. 1-Dimensional Array
4. Multi-Dimensional Array
Classification of Types of Arrays
However, these array types can be broadly classified in two ways:
- On the basis of Size
- On the basis of Dimensions
Types of ArraysTypes of Arrays on the basis of Size:
1. Fixed Sized Arrays:
We cannot alter or update the size of this array. Here only a fixed size (i,e. the size that is mentioned in square brackets []) of memory will be allocated for storage. In case, we don't know the size of the array then if we declare a larger size and store a lesser number of elements will result in a wastage of memory or we declare a lesser size than the number of elements then we won't get enough memory to store all the elements. In such cases, static memory allocation is not preferred.
C++
// Method 1 to create a fixed sized array.
// Here the memory is allocated at compile time.
int arr[5];
// Another way (creation and initialization both)
int arr2[5] = {1, 2, 3, 4, 5};
// Method 2 to create a fixed sized array
// Here memory is allocated at run time (Also
// known as dynamically allocated arrays)
int *arr = new int[5];
C
// Method 1 to create a fixed sized array.
// Here the memory is allocated at compile time.
int arr1[5];
// Another way (creation and initialization both)
int arr2[5] = {1, 2, 3, 4, 5};
// Method 2 to create a fixed sized array
// Here memory is allocated at run time (Also
// known as dynamically allocated arrays)
int *arr = (int*)malloc(n * sizeof(int));
Java
// Fixed sized array examples
int[] arr1 = new int [5];
// Another way (Array creation and
// initialization both)
int[] arr2 = {1, 2, 3, 4, 5};
Python
import array
# Static Array
arr = array.array('i', [1, 2, 3, 4, 5])
C#
// Fixed sized array examples
int[] arr1 = new int [5];
// Another way (Array creation and
// initialization both)
int[] arr2 = {1, 2, 3, 4, 5};
2. Dynamic Sized Arrays:
The size of the array changes as per user requirements during execution of code so the coders do not have to worry about sizes. They can add and removed the elements as per the need. The memory is mostly dynamically allocated and de-allocated in these arrays.
C++
#include<vector>
// Dynamic Integer Array
vector<int> v;
C
// C does not seem to support
// dynamic sized arrays as of now
Java
// Dynamic Integer Array
ArrayList<Integer> arr = new ArrayList<>();
Python
C#
// Similar to Java
ArrayList myList = new ArrayList();
JavaScript
// Dynamic Sized Array
let arr = new Array();
Types of Arrays on the basis of Dimensions
There are majorly three types of arrays on the basis of dimensions:
You can imagine a 1d array as a row, where elements are stored one after another.
Â
1D arraySyntax for Declaration of Single Dimensional Array
Below is the syntax to declare the single-dimensional array
data_type array_name[array_size];
where,
- data_type: is a type of data of each array block.
- array_name: is the name of the array using which we can refer to it.
- array_size: is the number of blocks of memory array going to have.
For Example
int nums[5];
2. Two-dimensional (2D) array:
Multidimensional arrays can be considered as an array of arrays or as a matrix consisting of rows and columns.
Â
2D arraySyntax for Declaration of Two-Dimensional Array
Below is the syntax to declare the Two-dimensional array
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension];
where,
- data_type: is a type of data of each array block.
- array_name: is the name of the array using which we can refer to it.
- sizeof_dimension: is the number of blocks of memory array going to have in the corresponding dimension.
For Example
int nums[5][10];
3. Three-dimensional array:
A 3-D Multidimensional array contains three dimensions, so it can be considered an array of two-dimensional arrays.
Â
3D arraySyntax for Declaration of Three-Dimensional Array
Below is the syntax to declare the Three-dimensional array
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension][sizeof_3rd_dimension];
where,
- data_type: is a type of data of each array block.
- array_name: is the name of the array using which we can refer to it.
- sizeof_dimension: is the number of blocks of memory array going to have in the corresponding dimension.
For Example
int nums[5][10][2];
Similar Reads
What is Array? Array is a linear data structure where all elements are arranged sequentially. It is a collection of elements of same data type stored at contiguous memory locations. For simplicity, we can think of an array as a flight of stairs where on each step is placed a value (let's say one of your friends).
2 min read
Arrays and Strings in C++ Arrays An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar types of elements as in the data type must be the same for all elements. They can be used to store the collection
5 min read
C++ Multidimensional Array A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more.Syntax of Multidimensional ArraysC++data_type array_name[s1][s2]...[sn];where s1, s2,â¦, sn are the
8 min read
Array Notes for GATE Exam [2024] Arrays are fundamental data structures in computer science, and mastering them is crucial for success in the GATE exam. This article aims to provide concise yet comprehensive notes on arrays, covering essential concepts and strategies to help you tackle array-related questions in the GATE 2024 exam.
15+ min read
When to use and avoid array Arrays are fundamental Data Structures in programming, offering a structured way to store and organize elements. However, like any tool, arrays are not a one-size-fits-all solution. Knowing when to embrace or avoid arrays is crucial for efficient and effective software development. In this explorati
3 min read