Arrays
Arrays are one of the most fundamental data structures in programming. They are used to store multiple values in a single variable, allowing for efficient data manipulation and access. This guide will cover how to declare, initialize, and work with arrays in JavaScript, Java, Python, and C++.
What is an Array?β
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together to make it easier to manage them. Arrays can be indexed, modified, and iterated over to perform various operations.
Arrays in Different Languagesβ
- JavaScript
- Java
- Python
- C++
JavaScript Arrays Overviewβ
In JavaScript, arrays are dynamic, which means they can change size and hold mixed data types.
Declaration and Initializationβ
// Declaration
let fruits = ["Apple", "Banana", "Cherry"];
// Accessing elements
console.log(fruits[0]); // Output: Apple
// Modifying an array
fruits[1] = "Mango";
console.log(fruits); // Output: ["Apple", "Mango", "Cherry"]
Common Array Methodsβ
push(): Adds an element to the end.pop(): Removes the last element.shift(): Removes the first element.unshift(): Adds an element to the beginning.
fruits.push("Orange");
console.log(fruits); // Output: ["Apple", "Mango", "Cherry", "Orange"]
fruits.pop();
console.log(fruits); // Output: ["Apple", "Mango", "Cherry"]
Java Arrays Overviewβ
Java arrays have a fixed size and must be declared with a type.
Declaration and Initializationβ
// Declaration
int[] numbers = new int[5]; // Array of size 5
// Initialization
int[] primes = {2, 3, 5, 7, 11};
// Accessing elements
System.out.println(primes[2]); // Output: 5
// Modifying an array
primes[2] = 13;
System.out.println(primes[2]); // Output: 13
Looping Through Arraysβ
for (int num : primes) {
System.out.println(num);
}
Python Arrays (Lists) Overviewβ
In Python, lists are dynamic and can hold different data types.
Declaration and Initializationβ
# Declaration
fruits = ["Apple", "Banana", "Cherry"]
# Accessing elements
print(fruits[0]) # Output: Apple
# Modifying an array
fruits[1] = "Mango"
print(fruits) # Output: ["Apple", "Mango", "Cherry"]
Common List Methodsβ
append(): Adds an element to the end.remove(): Removes a specific element.pop(): Removes the last element.
fruits.append("Orange")
print(fruits) # Output: ["Apple", "Mango", "Cherry", "Orange"]
fruits.pop()
print(fruits) # Output: ["Apple", "Mango", "Cherry"]
C++ Arrays Overviewβ
C++ arrays are static and have a fixed size.
Declaration and Initializationβ
// Declaration
int numbers[5]; // Array of size 5
// Initialization
int primes[] = {2, 3, 5, 7, 11};
// Accessing elements
std::cout << primes[2] << std::endl; // Output: 5
// Modifying an array
primes[2] = 13;
std::cout << primes[2] << std::endl; // Output: 13
Looping Through Arraysβ
for (int i = 0; i < 5; i++) {
std::cout << primes[i] << " ";
}
Visualizing Arrays with Mermaidβ
Below is a simple Mermaid diagram to visualize how an array is structured in memory.
This representation shows how each element in an array can be accessed by its index.