B2.2.2 Construct programs that apply arrays and Lists.
• One-dimensional (1D) arrays, two-dimensional (2D) arrays, ArrayLists in Java
• One-dimensional (1D) Lists and two-dimensional (2D) Lists in Python
• Add, remove and traverse elements in a dynamic list
The Big Idea
Arrays and lists are foundational data structures that allow us to store, organize, and manipulate collections of data in a single variable. Whether we’re dealing with a simple set of values (e.g. test scores) or more complex data grids (e.g. a chessboard), using arrays and lists allows a program to efficiently access, update, and manage multiple elements.
In Java, the term array refers to a fixed-size indexed structure, and ArrayList refers to a resizable, dynamic list. In Python, the term list always refers to a dynamic, resizable array-like structure.
Understanding how to construct, traverse, add to, and remove from these data structures—both in one dimension and two—is essential for solving a wide range of problems in computer science.
1. One-Dimensional Arrays and Lists
Java: One-Dimensional Arrays
int[] numbers = new int[5]; // Declaration
numbers[0] = 10; // Assignment
System.out.println(numbers[0]); // Access
Python: One-Dimensional Lists
numbers = [10, 20, 30] # Declaration and initialization
numbers[0] = 15 # Modify
print(numbers[0]) # Access
- Indexing starts at
0in both Java and Python. - The main difference is mutability and resizing: Java arrays are fixed-size, while Python lists are dynamic.
2. Two-Dimensional Arrays and Lists
Java: 2D Array (Matrix)
int[][] matrix = new int[3][3];
matrix[0][0] = 1;
System.out.println(matrix[0][0]);
- Java supports arrays of arrays (
int[][]), which can be jagged (rows of different lengths).
Python: 2D List
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[0][0]) # Output: 1
- Python lists can also form "matrices" (lists of lists), and their dimensions are not required to be uniform.
3. ArrayLists in Java
Dynamic List Declaration and Manipulation
import java.util.ArrayList;
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.remove("Alice");
System.out.println(names.get(0)); // Outputs: "Bob"
ArrayListautomatically resizes.- Elements can be added or removed at runtime.
Key Methods:
.add(element).remove(index or value).get(index).size()
4. Python Dynamic Lists
Python lists are inherently dynamic and provide built-in methods for flexible manipulation.
names = []
names.append("Alice")
names.append("Bob")
names.remove("Alice")
print(names[0]) # Outputs: "Bob"
Key Methods:
.append(item)– add to end.insert(index, item)– insert at specific index.remove(item)– remove by valuedel list[index]– remove by index.pop()– remove and return last item.len()– get number of elements
5. Traversing Arrays and Lists
Java: Looping through Arrays
int[] scores = {90, 80, 70};
for (int i = 0; i < scores.length; i++) {
System.out.println(scores[i]);
}
Java: Looping through an ArrayList
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
for (String name : names) {
System.out.println(name);
}
Python: Looping through Lists
scores = [90, 80, 70]
for score in scores:
print(score)
Python: Looping through a 2D List
matrix = [
[1, 2],
[3, 4]
]
for row in matrix:
for value in row:
print(value)
6. Programmatic Example: Dynamic List (Python)
shopping_list = []
# Add items
shopping_list.append("apples")
shopping_list.append("bananas")
shopping_list.insert(1, "oranges")
# Traverse
for item in shopping_list:
print(item)
# Remove
shopping_list.remove("bananas")
del shopping_list[0]
print("Final list:", shopping_list)
7. Common Mistakes
| Mistake | Explanation |
|---|---|
| Out-of-bounds access | Accessing an index that doesn’t exist (e.g. list[10] in a list of size 3). |
| Removing items during traversal | Modifying a list while iterating over it can cause errors. |
| Confusing 1D with 2D logic | Forgetting to use nested loops when dealing with 2D structures. |
| Assuming fixed size | Trying to assign to an index that hasn’t been created in a dynamic list. |
Summary Table
| Feature | Java Array | Java ArrayList | Python List |
|---|---|---|---|
| Size | Fixed | Dynamic | Dynamic |
| Syntax | int[] arr | ArrayList<Type> | list = [] |
| Multidimensional | Yes (int[][]) | No (use custom classes) | Yes (list of lists) |
| Add/Remove | Not allowed | .add(), .remove() | .append(), .remove() |
| Traversal | For loop, index | For-each, .get() | For loop, indexing |
Final Thoughts
Arrays and lists are critical tools for handling collections of data. Arrays provide fast, indexed access with fixed size, while dynamic lists like Java’s ArrayList or Python’s list offer flexibility and ease of use. Mastery of these structures means not only knowing how to declare and access them, but also how to build robust, dynamic programs that can handle real-world input sizes and requirements.