Array Data Structure
A fixed-size collection of elements with contiguous memory allocation. One of the most fundamental data structures.
Definition
An array is a data structure that stores a collection of elements of the same data type in contiguous memory locations. Each element can be accessed directly using its index position. Arrays have a fixed size that is determined at the time of creation.
Key Properties
Fixed Size
Size is determined at creation and cannot be changed
Contiguous Memory
Elements are stored in adjacent memory locations
Random Access
O(1) time complexity for accessing any element
Same Data Type
All elements must be of the same type
Zero-based Indexing
First element is at index 0
Cache Friendly
Better performance due to spatial locality
Time Complexity
Operation | Time Complexity | Description |
---|---|---|
Access | O(1) | Direct access by index |
Search | O(n) | Linear search through elements |
Insertion | O(n) | May require shifting elements |
Deletion | O(n) | May require shifting elements |
Update | O(1) | Direct update by index |
Code Examples
JavaScript
// Array declaration and initialization
let numbers = [1, 2, 3, 4, 5];
let fruits = new Array('apple', 'banana', 'orange');
// Accessing elements
console.log(numbers[0]); // 1
console.log(fruits[1]); // 'banana'
// Modifying elements
numbers[2] = 10;
console.log(numbers); // [1, 2, 10, 4, 5]
// Array methods
numbers.push(6); // Add to end
numbers.unshift(0); // Add to beginning
numbers.pop(); // Remove from end
numbers.shift(); // Remove from beginning
// Iteration
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
// Modern iteration
numbers.forEach(num => console.log(num));
numbers.map(num => num * 2); // [2, 4, 20, 8, 10]
Python
# List (Python's array equivalent)
numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'orange']
# Accessing elements
print(numbers[0]) # 1
print(fruits[-1]) # 'orange' (last element)
# Modifying elements
numbers[2] = 10
print(numbers) # [1, 2, 10, 4, 5]
# List methods
numbers.append(6) # Add to end
numbers.insert(0, 0) # Insert at index
numbers.remove(10) # Remove first occurrence
numbers.pop() # Remove and return last element
# List comprehensions
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
evens = [x for x in numbers if x % 2 == 0]
# Iteration
for num in numbers:
print(num)
# Using enumerate for index
for i, num in enumerate(numbers):
print(f"Index {i}: {num}")
Java
// Array declaration and initialization
int[] numbers = {1, 2, 3, 4, 5};
String[] fruits = new String[]{"apple", "banana", "orange"};
// Accessing elements
System.out.println(numbers[0]); // 1
System.out.println(fruits[1]); // "banana"
// Modifying elements
numbers[2] = 10;
System.out.println(Arrays.toString(numbers)); // [1, 2, 10, 4, 5]
// Array length
System.out.println(numbers.length); // 5
// Iteration
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
// Enhanced for loop
for (int num : numbers) {
System.out.println(num);
}
// Using ArrayList for dynamic arrays
ArrayList<Integer> dynamicArray = new ArrayList<>();
dynamicArray.add(1);
dynamicArray.add(2);
dynamicArray.remove(0); // Remove at index 0
Common Use Cases
Data Storage
Storing collections of similar data
Mathematical Operations
Vectors, matrices, and mathematical computations
Buffer/Queue Implementation
Fixed-size buffers and circular queues
Lookup Tables
Fast access to precomputed values
Image Processing
Pixel data and image manipulation
Algorithm Implementation
Base for many sorting and searching algorithms