Array is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming.
Basic terminologies of ArrayIn an array, all the elements are stored in contiguous memory locations. So, if we initialize an array, the elements will be allocated sequentially in memory. This allows for efficient access and manipulation of elements.
Declaration of ArrayArrays can be declared in various ways in different languages. For better illustration, below are some language-specific array declarations:
C++
// This array will store integer type element
int arr[5];
// This array will store char type element
char arr[10];
// This array will store float type element
float arr[20];
C
// This array will store integer type element
int arr[5];
// This array will store char type element
char arr[10];
// This array will store float type element
float arr[20];
Java
// This array will store integer type element
int arr[];
// This array will store char type element
char arr[];
// This array will store float type element
float arr[];
Python
# In Python, all types of lists are created same way
arr = []
C#
// This array will store integer type element
int[] arr;
// This array will store char type element
char[] arr2;
// This array will store float type element
float[] arr3;
Javascript
Initialization of Array
Arrays can be initialized in different ways in different languages. Below are some language-specific array initializations:
C++
int arr[] = { 1, 2, 3, 4, 5 };
char arr[5] = { 'a', 'b', 'c', 'd', 'e' };
float arr[10] = { 1.4, 2.0, 24, 5.0, 0.0 };
C
int arr[] = { 1, 2, 3, 4, 5 };
char arr[5] = { 'a', 'b', 'c', 'd', 'e' };
float arr[10] = { 1.4, 2.0, 24, 5.0, 0.0 };
Java
int arr[] = { 1, 2, 3, 4, 5 };
char arr[] = { 'a', 'b', 'c', 'd', 'e' };
float arr[] = { 1.4f, 2.0f, 24f, 5.0f, 0.0f };
Python
# This list will store integer type elements
arr = [1, 2, 3, 4, 5]
# This list will store character type elements (strings in Python)
arr = ['a', 'b', 'c', 'd', 'e']
# This list will store float type elements
arr = [1.4, 2.0, 24.0, 5.0, 0.0] # All float values
C#
int[] arr = { 1, 2, 3, 4, 5 };
char[] arr = { 'a', 'b', 'c', 'd', 'e' };
float[] arr = { 1.4f, 2.0f, 24f, 5.0f, 0.0f };
JavaScript
let arr = [ 1, 2, 3, 4, 5 ];
let arr = [ 'a', 'b', 'c', 'd', 'e' ];
let arr = [ 1.4, 2.0, 24, 5.0, 0.0 ];
Why do we Need Arrays?
Assume there is a class of five students and if we have to keep records of their marks in examination then, we can do this by declaring five variables individual and keeping track of records but what if the number of students becomes very large, it would be challenging to manipulate and maintain the data.
What it means is that, we can use normal variables (v1, v2, v3, ..) when we have a small number of objects. But if we want to store a large number of instances, it becomes difficult to manage them with normal variables.
Types of ArraysThe idea of an array is to represent many instances in one variable.
Arrays can be classified in two ways:
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
# Create a fixed-size list of length 5,
# initialized with zeros
arr = [0] * 5
# Output the fixed-size list
print(arr)
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
1. One-dimensional Array(1-D Array): You can imagine a 1d array as a row, where elements are stored one after another.
2. Multi-dimensional Array: A multi-dimensional array is an array with more than one dimension. We can use multidimensional array to store complex data in the form of tables, etc. We can have 2-D arrays, 3-D arrays, 4-D arrays and so on.
To read more about Matrix Refer, Matrix Data Structure
Operations on Array 1. Array TraversalTo read more about Multidimensional Array Refer, Multidimensional Arrays in C – 2D and 3D Arrays
Array traversal refers to the process of accessing and processing each element of an array sequentially. This is one of the most fundamental operations in programming, as arrays are widely used data structures for storing multiple elements in a single variable.
How Array Traversal Works?
When an array is created, it occupies a contiguous block of memory where elements are stored in an indexed manner. Each element can be accessed using its index, which starts from 0
in most programming languages.
For example, consider an array containing five integers:
arr = [10, 20, 30, 40, 50]
Here:
10
) is at index 0.20
) is at index 1.50
) is at index 4.Array traversal means accessing each element from start to end (or sometimes in reverse order), usually by using a loop.
Types of Array Traversal
Array traversal can be done in multiple ways based on the requirement:
0
, the traversal begins from the last element and moves towards the first.2. Insertion in ArrayTo read more about Array Traversal Refer, Traversal in Array
Insertion in an array refers to the process of adding a new element at a specific position while maintaining the order of the existing elements. Since arrays have a fixed size in static implementations, inserting an element often requires shifting existing elements to make space.
How Insertion Works in an Array?
Arrays are stored in contiguous memory locations, meaning elements are arranged in a sequential block. When inserting a new element, the following happens:
For example, if we have the array:
arr = [10, 20, 30, 40, 50]
and we want to insert 25
at index 2
, the new array will be:
arr = [10, 20, 25, 30, 40, 50]
Here, elements 30
, 40
, and 50
have shifted right to make space.
Types of Insertion
1. Insertion at the Beginning (Index 0)
2. Insertion at a Specific Index
3. Insertion at the End
ArrayList
).3. Deletion in ArrayTo read more about Insertion in Array Refer, Inserting Elements in an Array – Array Operations
Deletion in an array refers to the process of removing an element from a specific position while maintaining the order of the remaining elements. Unlike linked lists, where deletion is efficient, removing an element from an array requires shifting elements to fill the gap.
How Deletion Works in an Array?
Since arrays have contiguous memory allocation, deleting an element does not reduce the allocated memory size. Instead, it involves:
For example, consider the array:
arr = [10, 20, 30, 40, 50]
If we delete the element 30
(index 2
), the new array will be:
arr = [10, 20, 40, 50]
Here, elements 40
and 50
shifted left to fill the gap.
Types of Deletion
1. Deletion at the Beginning (Index 0)
2. Deletion at a Specific Index
3. Deletion at the End
To read more about Deletion in Array Refer, Deleting Elements in an Array – Array Operations
4. Searching in Array
Searching in an array refers to the process of finding a specific element in a given list of elements. The goal is to determine whether the element exists in the array and, if so, find its index (position).
Searching is a fundamental operation in programming, as it is used in data retrieval, filtering, and processing.
Types of Searching in an ArrayThere are two main types of searching techniques in an array:
1. Linear Search (Sequential Search)
Consider an array:
arr = [10, 20, 30, 40, 50]
If we search for 30
, the algorithm will:
10
with 30
→ No match.20
with 30
→ No match.30
with 30
→ Match found at index 2
.2. Binary Search (Efficient Search for Sorted Arrays)
Consider a sorted array:
arr = [10, 20, 30, 40, 50]
If we search for 30
:
30
→ Match found!To read more about Searching in Array Refer, Searching Elements in Array
Related articles:Next Read: Applications, Advantages and Disadvantages of Array
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4