🔢 Data Structures

Arrays (Single & Double Dimensional)

⏱ 5 hr4 topicsInteractive
🎯 By the end: You can create and use 1-D and 2-D arrays, run selection/bubble sort and linear/binary search on an SDA, and compute row/column/diagonal sums on a DDA.

An array stores many values of the same type under one name, reached by an index (position). Arrays are the foundation of searching, sorting and tables. This is a major ICSE Class 10 topic.

1Single dimensional arrays (SDA)

A single dimensional array is a row of values. Indices start at 0:

int[] marks = new int[5];      // 5 boxes: marks[0]..marks[4]
marks[0] = 92;                 // store
int x = marks[0];              // read

int[] a = {10, 20, 30};        // declare + initialise together

for (int i = 0; i < a.length; i++)   // a.length is the size
  System.out.println(a[i]);
For an array of size n, the valid indices are 0 to n-1. a.length gives the number of elements. Going outside this range causes an error.
Key points
  • An array stores many same-type values under one name, reached by an index starting at 0.
  • For size n, valid indices are 0 to n-1; a.length gives the size.
  • You can declare-and-initialise together: int[] a = {10, 20, 30};

2Searching: linear & binary

Searching looks for a value in an array (SDA):

Linear searchBinary search
HowCheck each element in turnRepeatedly halve a SORTED array
Array must beAny orderSorted
SpeedSlower for big arraysMuch faster
// Linear search
for (int i = 0; i < a.length; i++)
  if (a[i] == key) { found = i; break; }

// Binary search (a must be sorted)
int lo = 0, hi = a.length - 1;
while (lo <= hi) {
  int mid = (lo + hi) / 2;
  if (a[mid] == key) { found = mid; break; }
  else if (a[mid] < key) lo = mid + 1;
  else hi = mid - 1;
}
Key points
  • Linear search checks each element in turn; it works on any order.
  • Binary search repeatedly halves a SORTED array, so it's much faster.
  • Binary search needs the array sorted first; linear search does not.

3Sorting: selection & bubble

Sorting arranges an SDA in order. Two classic methods:

  • Selection sort — repeatedly find the smallest remaining element and place it at the front.
  • Bubble sort — repeatedly compare neighbouring pairs and swap if out of order, so big values "bubble" to the end.
// Bubble sort (ascending)
for (int i = 0; i < n - 1; i++)
  for (int j = 0; j < n - 1 - i; j++)
    if (a[j] > a[j + 1]) {
      int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;   // swap
    }
Both use a nested loop and swap elements. Bubble sort compares adjacent pairs; selection sort selects the minimum each pass.
Key points
  • Selection sort selects the smallest remaining element each pass and moves it to the front.
  • Bubble sort compares adjacent pairs and swaps them, bubbling large values to the end.
  • Both use nested loops and swap elements; swapping needs a temporary variable.

4Double dimensional arrays (DDA)

A double dimensional array is a grid (rows × columns) — like a table or matrix. You use two indices: a[row][col].

int[][] m = new int[3][3];     // 3 rows, 3 columns
m[0][0] = 5;

for (int i = 0; i < 3; i++)
  for (int j = 0; j < 3; j++)
    System.out.print(m[i][j] + " ");

m.length gives the number of rows; m[0].length the number of columns. Common DDA tasks:

  • Sum of a row — fix the row, loop the columns.
  • Sum of a column — fix the column, loop the rows.
  • Diagonal elements — where the row index equals the column index (i == j).
Key points
  • A double dimensional array is a grid; use a[row][col] with two indices.
  • m.length is the number of rows; m[0].length is the number of columns.
  • Diagonal elements are those where the row index equals the column index (i == j).

★ Practical: array programs

In BlueJ, write programs that:

  1. Read 5 numbers into an SDA and print the largest using a loop.
  2. Sort the array in ascending order using bubble sort.
  3. Search for a value with linear search and report its index.
  4. Fill a 3×3 DDA and print the sum of its diagonal elements (where i == j).

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.