seegongsik
Saved words
Data

Lining things up: arrays and lists

There is more than one way to keep many things in order. If you stick the slots side by side in numbered order, you can pull one out at once just by its number. But if you scatter the slots around and link them with notes that hold the next slot's address, it becomes easy to slip a new slot into the middle. Pulling out fast or slipping in easily, which do you pick?

01

Lockers packed together: the array

Earlier we learned that each container
has its own shape.
The simplest shape of all is the array.
It is a locker where numbered slots
are packed together in one row.
Slot 0, slot 1, slot 2,
each slot carries its own number.
Since the slots are all packed together,
knowing the number tells you at once where it is.

array (numbered packed slots)
Tap a number to jump to that slot

Tap a number to jump straight to that slot. The slots are packed together, so the number finds it at once.

When you tapped a number,
that slot lit up right away.
Since the slots sit side by side,
any slot is pinned in one step.
Pulling something out by number like this
is called fast access.
It is the array's biggest strength.
But must the slots really stay packed?
Could we not scatter them instead?

02

Scattered lockers: the list

A list does not pack its slots.
The slots are scattered here and there.
So how is the order kept?
Each slot gets one little note.
That note holds the address
of where the next slot is.
So from the first slot you read its note
and move to the next slot, then read that note
and go on to the one after.

list (scattered slots + next-address notes)
#34Ahere
#07B
#91C
#22D
Tap the note to follow it to the next slot

Tap the note to follow it to the next slot. The slots are scattered, but the notes keep the order.

Following the notes,
the scattered slots linked into one line.
Unlike an array that jumps by number,
a list must read each note
and go one slot at a time, in turn.
It looks a little more bothersome,
yet this scattered structure
gives an unexpected advantage.
You will soon see what it is.

03

Fast access vs slow access

Now let us compare the two.
Say you want the fifth slot.
For an array, the number alone does it.
The slots are packed, so it jumps in one go.
A list cannot do that.
From the first slot you follow the notes,
one slot, two slots, three slots,
counting your way along in turn.
So the farther it is, the longer it takes.

goal: reach the fifth slot
array
0A
1B
2C
3D
4E
0 clicks
list
0A
1B
2C
3D
4E
0 clicks
Reach the fifth from both sides and compare the clicks

Reach the fifth one. The array does it in one step; the list goes one slot at a time from the start. Compare the clicks.

The difference is clear.
The array took one tap to arrive,
while the list took several taps to get there.
For pulling things out,
the array that jumps by number is far faster.
So is the array simply better?
No. In the next scene
comes the moment the list shines:
when you slip something into the middle.

04

Slipping one into the middle

This time, into the middle of the line
let us slip a new slot.
For a list it is very easy.
You change where the front slot's note points
so it points to the new slot,
and make the new slot's note
point to what used to be next.
Fix just two notes,
leave the rest of the slots as they are, and it is done.

goal: slip new slot X into the middle
array
A
B
C
D
list
B
->
C
Insert X from both sides and compare the work

Insert a new slot in the middle. The list fixes two notes; the array pushes every later slot over. Compare how much work each takes.

How did the array fare?
To make room in the middle
it had to push every later slot over, one by one.
The slots are packed, so there is no empty gap.
The list, meanwhile, finished with two notes.
So for inserting in the middle often,
a list is far more comfortable.
Fast access goes to the array,
easy insertion goes to the list.
The two are a trade for each other.

05

Let's wrap up

Gathered on one line, it is this.
An array is a locker of packed numbered slots,
with fast access by number.
A list is scattered slots,
each wearing a note with the next slot's address,
so inserting in the middle is easy.
Reaching the fifth one, the array is fast;
slipping one into the middle, the list is comfortable.
Pick by what you do most often.

Tap the four key points in order
Tap the points below in order from the top

Tap the key points in order to review. (packed-locker array -> scattered-locker list -> fast vs slow access -> middle insertion)

Now you know that even the same things,
depending on how you store them,
are good at different jobs.
Arrays and lists
are the first fork in choosing a container's shape.
Next we will look at special containers
that put in and take out
only at one end,
stacking and queueing, together.

In one lineAn array is a locker where numbered slots sit packed side by side. Give a number and you jump straight to that slot, pulling it out very fast. A list is slots scattered around, each holding a note with the next slot's address. So to reach the fifth one you must follow the notes from the start, one slot at a time, which is slower. But to slip a new slot into the middle you only fix two notes, so it is easy. An array instead has to push every later slot over, which is a hassle. Need fast access, pick an array; insert in the middle often, pick a list. You trade one for the other.
Data
Was this helpful? Support seegongsik