Arrays and Lists
Every program so far has stored each value under its own name. That breaks down instantly in the real world: a leaderboard has a thousand scores and you cannot write a thousand variable names. The array is the answer: one name, many slots, each slot reached by a number called its index.
An array is a row of numbered mailboxes under a single street name. scores box 0, scores box 1, scores box 2. To read or replace a value, you give the street name and the box number. Because the boxes are numbered rather than named, a loop can visit every one of them by counting, which is the trick that makes a thousand values as easy to process as three.
The operations that matter
- Read by index: scores[0] fetches one slot instantly, no searching involved.
- Write by index: scores[2] = 99 replaces that slot in place.
- Append: add a value at the end, growing the collection by one.
- Length: ask how many values are in there right now.
- Iterate: visit every slot in order with a loop, the pairing that powers sums, searches, maximums, and everything else.
That last one is the payoff. The accumulator pattern from the loops lesson plus an array is most of practical programming: start a total at zero, walk the list, fold each element in.
The 0 versus 1 border
Here is the one genuine cross-language trap. Most languages number slots from 0, so a list of three has indexes 0, 1, 2 and the last slot is length minus one. Lua numbers from 1, so the same list has indexes 1, 2, 3 and the last slot is the length itself. Neither choice is wrong, but crossing between them breeds off-by-one errors, and reading the wrong end of that deal means reading one slot past the edge of the array, a bug with its own long security history.
In the switcher, watch three things move between languages: the starting index (Lua stands alone at 1), the append spelling (push, append, <<, a bare [] in PHP), and the length spelling (#, .length, len, count). The mental model never moves.
Life without arrays
The teaching playground has no arrays on purpose. The program below manages three scores the only way it can: three separate variables, and every operation written out three times. Run it, then imagine the same program with fifty scores.
That is the point of the exercise: without arrays, the size of your data is hard-coded into the shape of your program. With them, one loop handles three values or three million, and the code does not change when the data grows.
Arrays in shipped code
When code is compiled or obfuscated, named variables fade but arrays remain visible as a shape: a block of values walked by a counting loop. Analysts lean on that shape constantly, because an indexed loop over a data blob usually marks the interesting machinery: a lookup table, a decoding pass, a list of resources. Constant obfuscation in Level 6 and virtualization in Level 7 both revolve around big arrays of encoded data walked by exactly the loops you now recognise, on both the defense and the attack side.
An array puts many values behind one name, in order, indexed by number, which lets a counting loop process any amount of data with fixed code. Mind the 0-versus-1 border, respect the edges, and notice that data-walked-by-loop is a shape that survives even heavy obfuscation.
Frequently asked questions
What happens if I read past the end of an array?
It depends on the language, and the differences matter. Lua hands you nil, JavaScript hands you undefined, Python and Java stop you with an explicit error, and low-level languages like C historically let you read whatever memory happened to be next, which is the root of an entire category of security vulnerabilities.
What is the difference between an array and a list?
In everyday use the words are interchangeable: an ordered, indexable collection. Strictly, arrays are fixed-size blocks of memory and lists can grow, which is why Java has both int[] and ArrayList. Every course language's default collection grows on demand, so this lesson treats them as one idea.