Arrays - potatoscript/dsa GitHub Wiki
๐ฆ What is an Array?
Imagine a row of mailboxes ๐ฌ lined up neatly.
Each box:
- Has its own number (called index),
- Can hold one item (like a number, word, or toy),
- Is placed side by side in one long row.
Thatโs an Array in computer land! ๐ป
+-----+-----+-----+-----+-----+
| 10 | 20 | 30 | 40 | 50 |
+-----+-----+-----+-----+-----+
โ โ โ โ โ
Index 0 1 2 3 4
๐ This is an array with 5 numbers.
๐ง Why Do We Use Arrays?
Arrays help us:
- Group similar items together ๐งธ๐งธ๐งธ
- Access any item instantly by its number (index)
- Store data in a fixed, ordered way
๐งธ Real-Life Examples
Situation | Array Use |
---|---|
๐ง Cupcakes in a tray | Each cupcake is in a slot (index) |
๐ Pencils in a case | Each pencil has a spot |
๐งฆ Socks in a drawer | Each drawer section = index |
๐งช C# Code Example
string[] fruits = { "Apple", "Banana", "Cherry", "Date" };
Console.WriteLine(fruits[0]); // Apple
Console.WriteLine(fruits[2]); // Cherry
๐๏ธ How Arrays Work in Memory
An array:
- Uses contiguous memory (placed side-by-side ๐งฑ๐งฑ๐งฑ)
- Is fixed-size (you decide the number of items when you create it)
int[] scores = new int[5]; // Can hold 5 scores
๐งฎ Indexing โ The Secret Key
Index | Value |
---|---|
0 | "Cat" |
1 | "Dog" |
2 | "Fox" |
To get "Dog":
string animal = animals[1]; // Dog
๐ Index starts from 0!
โจ Fun Array Operations
1. ๐ฅ Add (Set) Value
int[] numbers = new int[3];
numbers[0] = 100;
numbers[1] = 200;
numbers[2] = 300;
2. ๐ค Get Value
Console.WriteLine(numbers[1]); // 200
3. ๐ Loop Through All
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
Or the cooler version:
foreach (int num in numbers)
{
Console.WriteLine(num);
}
๐ฅ What Happens If You Go Too Far?
Console.WriteLine(numbers[10]);
โ Error! Thatโs called an IndexOutOfRangeException!
๐ซ You tried to open a mailbox that doesnโt exist.
๐ Arrays of Different Types
You can make arrays of:
- ๐งฎ
int[]
โ numbers - ๐ค
string[]
โ words - โ
bool[]
โ true/false
bool[] lights = { true, false, true };
๐ง Array Facts Cheat Sheet
Fact | Meaning |
---|---|
๐ข Indexed | Start at 0 |
๐ Fixed Size | You canโt change its length |
๐ Fast Access | O(1) time to get any item |
๐ Slow Insert/Delete | Need to shift all other items |
๐ง How is Array Different From List?
Feature | Array | List |
---|---|---|
Size | Fixed | Flexible |
Speed | Fast | Slower insert/delete |
Syntax | Simple | Has more functions |
๐งฑ Visual Breakdown: Storing Student Scores
int[] scores = { 85, 90, 78, 92, 88 };
// Index: 0 1 2 3 4
// Score: 85 90 78 92 88
To get the 3rd studentโs score:
int third = scores[2]; // 78
๐ก Behind the Scenes: How Computers Use Arrays
- Each item has a memory address
- Computers calculate the exact address using:
address = baseAddress + (index ร size)
- This is why accessing items is super fast!
๐ Big O for Arrays
Operation | Time | Notes |
---|---|---|
Get item | O(1) | Super fast! |
Set item | O(1) | Direct overwrite |
Search item | O(n) | Need to scan |
Insert/Delete | O(n) | Shift items needed |
๐ Common Mistakes to Avoid
-
โ Accessing outside the array:
fruits[5]; // ERROR!
-
โ Changing length after creation:
// You can't do: fruits.Length = 10;
-
โ Confusing
Length
(property) andCount()
(LINQ):fruits.Length // โ fruits.Count() // Only if you use LINQ
๐ ๏ธ Practice Time โ Mini Challenges
๐ฅ Challenge 1
Create an array of 4 animals and print the second one.
๐ฅ Challenge 2
Make a loop that says hello to 5 different names in an array.
๐ฅ Challenge 3
What happens when you use index 100 in an array of 3 items?
๐ง Summary โ What We Learned
โ
Arrays store multiple items in one place
โ
Each item has an index starting at 0
โ
Fixed size, super fast to access
โ
Commonly used in loops, search, sort, and more