Arrays (Lists): Ordered Collections
An array is an ordered list of values. You read items by position, change them, add to the end, and loop through. Arrays show up in almost every program.
From Bee: arr.sort() sorts as strings by default. [10, 2, 1].sort() returns [1, 10, 2]. Always pass a comparator for numbers.
What Is an Array?
An array (Python calls them lists) is an ordered collection of values. Each value has a numeric index starting at 0. The first item is at index 0; the second at index 1; and so on.
Reading and Changing Elements
Use square brackets to access an element by index. Out-of-range indexes either throw an error (Python, Java) or return undefined (JS).
Common Operations
Adding to the end (push/append), removing from the end (pop), inserting at a position, removing by value, sorting, mapping (transforming each item), filtering β these are the verbs you'll use daily with arrays.
Code Examples in Three Languages
let scores = [10, 20, 30];
scores[0]; // 10 (first)
scores.length; // 3
scores[scores.length - 1]; // 30 (last)
// Mutate
scores.push(40); // [10,20,30,40]
scores.pop(); // 40 β removes last
scores[1] = 99; // [10,99,30]
// Iterate
for (const s of scores) console.log(s);
// Transform
const doubled = scores.map(n => n * 2);
const high = scores.filter(n => n > 15);scores = [10, 20, 30]
scores[0] # 10
len(scores) # 3
scores[-1] # 30 (negative = from end)
# Mutate
scores.append(40)
scores.pop() # 40
scores[1] = 99
# Iterate
for s in scores:
print(s)
# Transform (list comprehension)
doubled = [n * 2 for n in scores]
high = [n for n in scores if n > 15]int[] scores = {10, 20, 30};
scores[0]; // 10
scores.length; // 3 (no parens β it's a property)
// Fixed-size arrays can't grow. Use ArrayList:
List<Integer> list = new ArrayList<>();
list.add(40);
list.remove(0);
list.set(1, 99);
for (int s : scores) {
System.out.println(s);
}Best Practices
- Use built-in methods (
map,filter) over manual loops when transforming. - Avoid sparse arrays (gaps with no values) β they confuse readers.
- Don't modify an array while looping over it.
- Use array destructuring (JS/Python) for cleaner code:
const [a, b] = arr;
Common Mistakes
- Off-by-one when accessing the last element. Index of last is
length - 1. - Out-of-bounds access. Returns
undefinedin JS but throws in Python and Java. - Treating
lengthas a method in Java arrays. It's a property β no parentheses. - Confusing fixed-size and resizable arrays in Java. Use
ArrayListwhen you need to grow.
We have a dedicated Common Bugs with Arrays page β five real broken snippets with the fix. Read it before you start writing.
How It Works Under the Hood
An array is, in most languages, a contiguous block of memory holding values of equal size. Reading arr[5] takes constant time: the runtime computes start_address + 5 Γ element_size and reads that memory location. This is why array indexing is so fast.
Insertion at the end (push) is amortized O(1): the array has spare capacity for occasional growth. Insertion in the middle (splice) requires shifting every later element β O(n). When you find yourself doing many middle-insertions, reach for a different data structure (linked list, deque).
JavaScript arrays are technically objects (sparse, mixed-type), but engines optimize "regular" arrays into the fast path you'd expect. Mixing types or leaving holes can deoptimize them.
From Beginner to Pro
The same concept gets deeper as you grow. Here's what mastery looks like at three levels:
You read arr[i], push and pop, loop with for.
You use map, filter, reduce, find, some, every appropriately.
You think about complexity (O(1) vs O(n) operations) and pick the right structure. You use immutable patterns when they avoid bugs, mutable when they avoid copies.
Performance & Gotchas
- JS sort is alphabetical by default. Always pass a comparator for numbers.
- Spread does shallow copy. Nested objects still share refs.
- Array out-of-bounds returns
undefinedin JS but throws in Python/Java. - Mutating during iteration skips elements.
Authoritative references: MDN β Array, Python β list, Oracle β Java arrays.
Quick Quiz
Real-World Uses (Production Code, Today)
Inbox
Your email inbox is an array of message objects. Sorting by date? messages.sort(...). Filtering unread? messages.filter(...).
CSV import
Importing a spreadsheet into a script almost always lands you with an array of row-objects. Every spreadsheet operation maps to an array operation.
Browser history
Your browser keeps an array of visited URLs. Back/forward = moving an index pointer through that array.
Frequently Asked Questions
What is an array index?
The position of an item, starting at 0. arr[0] is the first item, not the second.
How do I get the last item?
arr[arr.length - 1] in JS/Java. Python supports arr[-1].
What's the difference between push and unshift?
push adds to the end; unshift adds to the front (JavaScript). Python uses append and insert(0, x).
Can arrays hold mixed types?
In JavaScript and Python, yes. In Java, an int array can only hold ints (without boxing).
How do I copy an array?
[...arr] in JS, arr.copy() in Python, arr.clone() in Java.
What's the difference between map and forEach?
map returns a new array of transformed values; forEach just runs a function on each item.
How do I sort an array?
arr.sort() in all three. Pass a comparison function for custom orderings.
What's a 2D array?
An array of arrays β useful for grids, matrices, game boards.
Related Concepts
Data Types in Programming
Related concept.
Strings
Related concept.
Operators
Related concept.
- Arrays (Lists): Ordered Collections is one of the 13 universal concepts of programming.
- The syntax differs across languages, but the underlying idea is the same.
- Practice in the playground to make it stick.