Understanding Arrays
What Is an Array?
An array is one of the simplest and most important data structures in programming. It stores multiple values of the same type in a single, continuous block of memory. Each value in the array can be accessed using an index.
Arrays are the foundation for many other data structures and algorithms. Before moving to advanced concepts, mastering arrays is essential.
How Arrays Work in Memory
Arrays are stored in contiguous (continuous) memory locations. This means all elements are placed next to each other.
For example, an integer array:
index: 0 1 2 3 4
value: 5 10 15 20 25
Because memory is continuous, accessing any element is very fast. You can directly jump to an element using its index.
Access Time
Accessing an element using its index takes O(1) time.
This is why arrays are often the first choice for storing data.
Types of Arrays
Static Array
A static array has a fixed size. Once created, its size cannot change.
int[] numbers = new int[5];
Dynamic Array
A dynamic array can grow in size. Examples include:
List in C#
ArrayList in Java
Even though they grow automatically, internally they still use arrays.
Common Array Operations
1. Accessing an Element
Access by index:
int value = numbers[2];
Time complexity: O(1)
2. Updating an Element
numbers[1] = 25;
Time complexity: O(1)
3. Inserting an Element
In static arrays, insertion is tricky because size is fixed. In dynamic arrays, insertion may require shifting elements.
Example: Insert at the beginning
Time complexity: O(n)
4. Deleting an Element
Deletion may require shifting elements.
Time complexity: O(n)
5. Searching
Linear search: Check every element ? O(n)
Binary search: Works only on sorted arrays ? O(log n)
Example Code: Basic Array Usage
int[] numbers = { 5, 10, 15, 20, 25 };
Console.WriteLine("Array elements:");
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
Understanding Array Limitations
Arrays are powerful, but they also have limitations:
Fixed size (for static arrays)
Insertion and deletion are slow (require shifting)
Cannot grow automatically (without using dynamic structures like List)
Where Arrays Are Used in Real Life
Arrays are used everywhere:
Storing student marks
Managing inventory quantities
Search systems
Image processing (2D arrays)
Games (positions, scores, maps)
Because they are fast and simple, arrays are the foundation for advanced data structures such as heaps, hash tables, and graphs.
2D Arrays
A 2D array represents data in rows and columns.
int[,] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
Accessing a value:
int value = matrix[1, 2];
This fetches the element from row 1, column 2.
Example Problem: Find the Maximum in an Array
int[] arr = { 3, 9, 2, 7, 6 };
int max = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
Console.WriteLine("Maximum value: " + max);
Time and Space Complexity for Arrays
Time Complexity
Access: O(1)
Search: O(n)
Insert: O(n)
Delete: O(n)
Space Complexity
O(n)
Summary
Arrays are fast, simple, and form the building blocks of many other data structures. Understanding how they work helps you learn more advanced topics like linked lists, stacks, queues, and trees.