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.