Introduction to Arrays, Collections, and Strings

In our previous look at Variables, data types and operators, we learned how to store single pieces of information, like a name or a score. But what if you need to store the names of 30 students in a class or the high scores for a video game? Creating 30 different variables would be exhausting! This is where Arrays, Collections, and Strings come in. They allow us to group data together, making our programs much more powerful and organized.

Think of these as "containers" for data. Instead of carrying 30 individual tennis balls, you put them all in one bucket. In this chapter, we will learn how to fill these buckets, how to find specific items inside them, and how to manipulate text effectively.


1. Arrays: The Organized Row of Lockers

An Array is a data structure that holds a fixed number of values of the same type. Imagine a row of school lockers. Every locker is exactly the same size, they are all numbered in order, and they are usually used to store similar items.

Key Characteristics of Arrays

  • Fixed Size: Once you create an array of size \(10\), you cannot easily change it to \(11\). You have to plan ahead!
  • Same Data Type: In many languages (like Java), every item in an array must be the same type (e.g., all integers or all strings).
  • Indexing: This is the most important rule: Computers start counting at \(0\). The first item is at index \(0\), the second is at index \(1\), and so on.

Accessing Data

If we have an array called scores, we access the items using square brackets:

  • \(scores[0]\) is the first element.
  • \(scores[1]\) is the second element.
  • If the array has \(n\) items, the last element is always at index \(n - 1\).

Two-Dimensional (2D) Arrays

Sometimes a single row isn't enough. A 2D Array is like a grid or a table (think of a spreadsheet or a chessboard). To find a value, you need two coordinates: the row and the column.

Example: \(grid[row][column]\).
If you are looking at a seating chart, \(seats[2][3]\) would refer to the person sitting in the 3rd row and 4th column (remembering we start at \(0\)).

Quick Review: If an array has a length of \(5\), the valid indices are \(0, 1, 2, 3,\) and \(4\). Trying to access index \(5\) will cause an error!


2. Collections: The Flexible Shopping List

While arrays are great, their "fixed size" can be annoying. What if you don't know how many items you'll have? This is where Collections (like Lists in Python or ArrayLists in Java) come to the rescue.

Why use Collections?

Think of a Collection as a digital shopping list. As you think of more items, you just add them to the bottom. If you change your mind, you can scratch an item out from the middle, and the list automatically shrinks to close the gap.

Common Operations

  • Add/Append: Puts a new item at the end of the collection.
  • Remove: Deletes an item and shifts everything else to fill the space.
  • Size: Tells you how many items are currently in the collection (this changes as you add/remove).

Note: In the IB exam, you can choose to write your logic in either Java or Python. Python users will naturally use "Lists," while Java users will use "ArrayLists" or similar structures to represent these dynamic collections.

Key Takeaway: Use Arrays when you know exactly how many items you have (like days of the week). Use Collections when the number of items might change (like a list of users currently online).


3. Strings: The Beaded Necklace

A String is technically a sequence of characters (letters, numbers, or symbols). You can think of it as a beaded necklace where each bead is a character.

String Operations you MUST know

Because strings are used in almost every program, you need to know how to manipulate them:

  • Length: Finding out how many characters are in the string (including spaces!). For "Hello World", the length is \(11\).
  • Concatenation: Joining two strings together. Example: "Java" + "Script" = "JavaScript".
  • Substrings: Taking a "slice" of a string. Example: A substring of "Chocolate" from index \(0\) to \(4\) is "Choc".
  • Case Conversion: Changing text to ALL UPPERCASE or all lowercase.
  • Searching: Finding if a specific letter or word exists inside a larger string.

Common Mistake: Forgetting that spaces count! In the string "Hi !", the space at index \(2\) is just as important as the letters.


4. Comparing Arrays, Collections, and Strings

It can be confusing to decide which one to use. Here is a simple comparison table:

Arrays: Fixed size. Best for performance and fixed data (e.g., coordinates of a point).
Collections: Dynamic size. Best for lists that grow or shrink (e.g., a social media feed).
Strings: Specialized for text. Technically "immutable" in many languages, meaning you usually create a new string rather than changing the old one.


5. Working with Loops (A Sneak Peek)

To process arrays and collections efficiently, we almost always use Iteration (loops). This is covered in detail in the Selection and iteration chapter, but here is the basic idea:

If you want to print every name in an array, you tell the computer: "Start at index \(0\), and keep going until you reach the index \(length - 1\)."

Step-by-Step Logic:
1. Initialize a counter \(i = 0\).
2. Check if \(i\) is less than the array length.
3. Access \(array[i]\).
4. Increase \(i\) by \(1\).
5. Repeat.


Summary Checklist

  • Do you remember that indexing starts at \(0\)?
  • Can you explain the difference between a 1D array and a 2D array?
  • Do you know when to choose a Collection over a fixed Array?
  • Can you identify common String operations like concatenation and substrings?
  • Do you know that the last index of a container is always \(length - 1\)?

Don't worry if the 2D arrays feel a bit dizzying at first. Just remember: Row first, then Column. Think of it like reading a coordinate on a map!