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

  1. โŒ Accessing outside the array:

    fruits[5]; // ERROR!
    
  2. โŒ Changing length after creation:

    // You can't do: fruits.Length = 10;
    
  3. โŒ Confusing Length (property) and Count() (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