0
0
Intro to Computingfundamentals~6 mins

Arrays and lists in Intro to Computing - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you have a collection of items, like a row of mailboxes or a shopping list. You need a way to keep these items organized so you can find, add, or change them easily. Arrays and lists are tools that help computers organize and manage groups of data in a simple, ordered way.
Explanation
Arrays
An array is like a row of boxes where each box holds one item. All boxes are the same size and type, so you know exactly what kind of item goes in each. You can quickly find an item by its position number, starting from zero. Arrays have a fixed size, so you decide how many boxes you need when you create it.
Arrays store items of the same type in a fixed-size, ordered sequence accessible by position.
Lists
A list is like a flexible chain of items where you can add or remove boxes anytime. Lists can hold different types of items and can grow or shrink as needed. Finding an item might take longer because you may need to look through the chain one by one. Lists are great when you don't know how many items you will have in advance.
Lists are flexible collections that can change size and hold different types of items.
Indexing and Access
Both arrays and lists use positions called indexes to find items. Indexes usually start at zero, so the first item is at position zero, the second at one, and so on. This numbering helps you quickly get or change an item without searching through everything.
Indexes starting at zero let you quickly access items by their position.
When to Use Each
Use arrays when you know the exact number of items and want fast access. Use lists when you need to add or remove items often or when the number of items can change. Choosing the right one helps your program run smoothly and handle data well.
Choosing arrays or lists depends on whether you need fixed size and speed or flexibility.
Real World Analogy

Think of an array as a row of identical mailboxes, each with a fixed number and size, where you can quickly find a letter by its mailbox number. A list is like a chain of shopping bags you can add to or remove from anytime, and you might have to look through them to find a specific item.

Arrays → A row of identical mailboxes with fixed size and numbered slots
Lists → A chain of shopping bags that can grow or shrink as needed
Indexing and Access → Mailbox numbers that help you find letters quickly
When to Use Each → Choosing mailboxes for fixed letters or bags for flexible shopping
Diagram
Diagram
┌─────────────┐
│   Arrays    │
├─────────────┤
│ [0] Item A  │
│ [1] Item B  │
│ [2] Item C  │
│ [3] Item D  │
└─────────────┘

┌─────────────┐
│    Lists    │
├─────────────┤
│ Item A →    │
│ Item B →    │
│ Item C →    │
│ Item D → ∅  │
└─────────────┘
The diagram shows an array as fixed boxes with numbered positions and a list as linked items pointing to the next.
Key Facts
ArrayA fixed-size collection of items of the same type stored in order.
ListA flexible collection that can grow or shrink and hold different types.
IndexA number starting at zero used to locate an item in an array or list.
Fixed SizeThe number of items in an array cannot change after creation.
Dynamic SizeLists can add or remove items, changing their size during use.
Common Confusions
Arrays and lists are the same because both hold multiple items.
Arrays and lists are the same because both hold multiple items. Arrays have a fixed size and hold items of the same type, while lists can change size and hold different types.
Indexing starts at 1 because it feels more natural.
Indexing starts at 1 because it feels more natural. Indexing starts at 0 in most programming languages to simplify calculations and access.
Summary
Arrays are fixed-size collections that store items of the same type in order and allow quick access by position.
Lists are flexible collections that can grow or shrink and hold different types of items, but access may be slower.
Choosing between arrays and lists depends on whether you need fixed size and speed or flexibility and dynamic size.