Constructing Programs That Apply Arrays and Lists
Arrays
Fixed-size, ordered collections of elements of the same data type.
One-Dimensional Arrays (1D)
Represent a single row or list of elements.
ExamplePython:
numbers = [1, 2, 3, 4]Java:
int[] numbers = {1, 2, 3, 4};Two-Dimensional Arrays (2D)
Represent rows and columns (like a table).
ExamplePython:
matrix = [[1, 2], [3, 4]]Java:
int[][] matrix = { {1, 2}, {3, 4} };Use arrays when size is known and constant.
Common MistakeTrying to resize → arrays are fixed-size.
Lists (Dynamic Structures)
Ordered, resizable collections of elements.Example
Python Lists:
fruits = ["apple", "banana"]
fruits.append("cherry") # add
fruits.remove("banana") # remove
for f in fruits: # traverse
print(f)Java ArrayLists:
import java.util.ArrayList;
ArrayList<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add("banana");
fruits.remove("banana");
for (String f : fruits) {
System.out.println(f);
}Lists are best when data size changes.
Common MistakeConfusing Python lists (dynamic arrays) with Java arrays (static).