Select a suitable data structure (1‑D or 2‑D array) to use for a given task.
Why Use Arrays?
Arrays store a collection of values of the same type in contiguous memory locations, allowing fast indexed access. The choice between a one‑dimensional (1‑D) and a two‑dimensional (2‑D) array depends on how the data is logically organised.
When to Choose a 1‑D Array
The data represents a simple list or sequence (e.g., scores, temperatures).
Each element can be identified by a single index \$i\$ where \$0 \le i < n\$.
Memory usage is minimal: \$n\$ elements require \$n \times \text{size\_of(element)}\$ bytes.
When to Choose a 2‑D Array
The data naturally forms a table, matrix, or grid (e.g., chessboard, image pixels).
Each element requires two indices \$i\$ and \$j\$, where \$0 \le i < m\$ (rows) and \$0 \le j < n\$ (columns).
Useful for algorithms that involve row‑wise or column‑wise operations, such as matrix multiplication.
Decision Checklist
Identify the logical dimensions of the data.
Ask: “Can each item be described with a single number?” – Yes → 1‑D array.
If a pair of numbers is needed (row and column) → 2‑D array.
Consider future extensions: a 2‑D array can be flattened to a 1‑D array if needed, but not vice‑versa without restructuring.
Comparison Table
Criterion
1‑D Array
2‑D Array
Indexing
Single index \$i\$
Two indices \$i\$, \$j\$
Typical Use‑case
Lists, queues, simple vectors
Grids, matrices, tables
Memory layout
Linear block of \$n\$ elements
Linear block of \$m \times n\$ elements (row‑major or column‑major)
Complexity of access
\$O(1)\$ using \$array[i]\$
\$O(1)\$ using \$array[i][j]\$ (after calculation of offset)
Implementation in Java
int[] scores = new int[10];
int[][] board = new int[8][8];
Illustrative Example
Suppose you need to store the weekly temperatures for a month (30 days). Each day has a single temperature value.
Best choice: 1‑D array because each temperature is identified by the day number.
double[] temps = new double[30]; // temps[0] = temperature on day 1
Now consider storing the scores of 5 players across 4 games. The data forms a table with rows = players and columns = games.
Best choice: 2‑D array.
int[][] scores = new int[5][4]; // scores[player][game]
Suggested diagram: Memory layout of a 2‑D array (row‑major order) showing how \$array[i][j]\$ maps to a single linear address.
Practice Questions
Which array type would you use to store the characters of a single word entered by a user? Explain.
A school wants to record the marks of 30 students in 6 subjects. Choose the appropriate array structure and write a declaration in Java.
Explain how a 2‑D array can be simulated using a 1‑D array. Provide the formula for converting indices \$(i, j)\$ to a single index \$k\$ assuming row‑major order.
Answer Key (for teacher use)
1‑D array; each character is accessed by its position in the word.
int[][] marks = new int[30][6]; // marks[student][subject]
Use a 1‑D array of size \$m \times n\$. The conversion is \$k = i \times n + j\$ (row‑major) where \$i\$ is the row index and \$j\$ is the column index.
Summary
Choosing the correct array dimension simplifies program logic and optimises memory usage. Use a 1‑D array for linear data and a 2‑D array for tabular or grid‑like data. Always verify the natural dimensionality of the problem before deciding.