Array Data Structure Tutorial: Learn Arrays from Scratch (2026)
Arrays are the most fundamental data structure in computer programming. They store elements of the same type in contiguous memory locations, enabling O(1) random access via indices. Understanding arrays is crucial because nearly every complex data structure — from heaps to hash tables — relies on array-based storage under the hood.
In this comprehensive tutorial, you will learn array declaration, traversal, insertion, deletion, rotation operations, and working with 2D arrays. Each operation includes C++, Java, and Python implementations to help you grasp the concepts across languages.
Array Declaration and Initialization
Arrays can be declared statically (fixed size at compile time) or dynamically (allocated at runtime). In C++, you use int arr[5]; for static arrays; Java uses int[] arr = new int[5];; Python uses lists like arr = [0] * 5.
Initialization can be done at declaration time: int arr[] = {1, 2, 3, 4, 5}; in C++, int[] arr = {1, 2, 3, 4, 5}; in Java, or arr = [1, 2, 3, 4, 5] in Python. Once declared, elements are accessed using the index operator arr[i].
// C++
int arr[5] = {10, 20, 30, 40, 50};
cout << arr[2]; // 30
// Java
int[] arr = {10, 20, 30, 40, 50};
System.out.println(arr[2]); // 30
// Python
arr = [10, 20, 30, 40, 50]
print(arr[2]) # 30
Traversal and Insertion
Traversing an array means visiting each element once, typically with a for loop. Insertion at the end is O(1) amortized in dynamic arrays; insertion at a specific index requires shifting elements, making it O(n).
For example, inserting at position k requires moving every element from index k onward one position to the right. In Python's list, insert() handles this internally, while C++ and Java require manual shifting.
// C++ — insert at position 2
int n = 5, arr[6] = {1,2,3,4,5};
for(int i=n; i>=2; i--) arr[i] = arr[i-1];
arr[2] = 99; n++;
// Java — insert at index 2
int[] arr = {1,2,3,4,5};
int[] newArr = new int[6];
System.arraycopy(arr, 0, newArr, 0, 2);
newArr[2] = 99;
System.arraycopy(arr, 2, newArr, 3, 3);
// Python
arr = [1,2,3,4,5]
arr.insert(2, 99)
Deletion and Rotation
Deletion also requires shifting remaining elements to fill the gap, resulting in O(n) time complexity. Array rotation moves elements to the left or right by a given number of positions. A common technique uses three reversals: reverse the whole array, then reverse the two segments.
Left rotation by k positions can be performed in O(n) time and O(1) extra space using the reversal algorithm. 2D arrays (matrices) extend the concept to multiple dimensions with row-major or column-major storage.
// C++ — left rotate by 2
void rotate(int arr[], int n, int k) {
reverse(arr, arr+k);
reverse(arr+k, arr+n);
reverse(arr, arr+n);
}
// Java — left rotate by 2
Collections.rotate(Arrays.asList(arr), -2);
// Python — left rotate by 2
arr = arr[2:] + arr[:2]
Frequently Asked Questions
What is the time complexity of array access?
O(1) — constant time, because the address is computed as base + index * element_size.
Can arrays hold mixed data types?
In C++ and Java, arrays are homogeneous (same type). Python lists can hold mixed types.
Originally published on Ayodhyyya. Last updated June 1, 2026.