Follow Us
Select Medium / माध्यम चुनें:
Eng (English) Hindi (हिन्दी)
ICSE • Class X • Information Technology • Ch 7
Estimated Time: 45 Mins
Study Progress: In Progress

Arrays

Exhaustive masterclass on Single-Dimensional and Two-Dimensional Arrays in Java for ICSE Class 10. Complete coverage of array allocation and indexing, Linear Search, Binary Search, Bubble Sort, Selection Sort, 2D matrix manipulation, diagonal and boundary summation, and board programming solutions.

Why This Chapter Matters

Exhaustive masterclass on Single-Dimensional and Two-Dimensional Arrays in Java for ICSE Class 10. Complete coverage of array allocation and indexing, Linear Search, Binary Search, Bubble Sort, Selection Sort, 2D matrix manipulation, diagonal and boundary summation, and board programming solutions.

Chapter Roadmap & Progression

1 1. Array Data Structure: Memory Mod...
2 2. Linear Search Algorithm: Mechani...
3 3. Binary Search Algorithm: Divide-...
4 4. Bubble Sort Algorithm: Pairwise...
5 5. Selection Sort Algorithm: Minimu...
6 6. Two-Dimensional (2D) Arrays: Mat...
7 7. Complete ICSE Board Program: Arr...
8 8. Complete ICSE Board Program: 2D...

Complete Concept Guide (100% Curriculum Coverage)

1. Array Data Structure: Memory Model & Indexing Rules

Array Architecture
The Nature of Arrays in Java:

An Array is an indexed collection of elements of the same data type (homogeneous) allocated in contiguous memory blocks on the Heap. In Java, arrays are true objects, instantiated using the new operator.

Three Stages of Array Creation:
  1. Declaration: int[] arr; (Creates a reference variable on the Stack; currently null).
  2. Instantiation: arr = new int[5]; (Allocates contiguous Heap memory for 5 integers initialized to 0).
  3. Initialization: Populating values individually (arr[0] = 10;) or via inline array literal (int[] arr = {10, 20, 30, 40, 50};).
ArrayIndexOutOfBoundsException: The valid index bounds are strictly $0$ to $\text{length} - 1$. Attempting to read or write arr[-1] or arr[arr.length] causes an immediate runtime crash.

2. Linear Search Algorithm: Mechanics & Trace Walkthrough

Linear Search
Sequential Scanning Logic:

Linear Search compares the target search key sequentially against every element in the array from index $0$ up to $N - 1$. It requires NO precondition (the array can be completely unsorted).

public static int linearSearch(int[] arr, int key) {
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == key) {
            return i; // Target found at index i!
        }
    }
    return -1; // Target not present in array
}
Algorithmic Complexity:
  • Best Case: $O(1)$ (Key found at index 0 on first comparison).
  • Worst Case: $O(n)$ (Key located at the final index or absent from the array).

3. Binary Search Algorithm: Divide-and-Conquer Mechanics

Binary Search
Divide-and-Conquer Search on Sorted Data:

Binary Search is an exceptionally fast searching algorithm that operates on the fundamental precondition that the array MUST be sorted (either in ascending or descending order). In each iteration, it compares the target key with the middle element ($mid = (low + high) / 2$):

  • If key == arr[mid]: Search terminates successfully.
  • If key < arr[mid]: Search space is restricted to the left half (high = mid - 1).
  • If key > arr[mid]: Search space is restricted to the right half (low = mid + 1).
public static int binarySearch(int[] arr, int key) {
    int low = 0, high = arr.length - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (arr[mid] == key) return mid;
        else if (key < arr[mid]) high = mid - 1;
        else low = mid + 1;
    }
    return -1; // Not found
}

Efficiency: Time complexity is $O(\log_2 n)$. In an array of $1,000,000$ elements, binary search requires at most $20$ comparisons, whereas linear search requires up to $1,000,000$!

4. Bubble Sort Algorithm: Pairwise Exchange Mechanics

Bubble Sort
Bubble Sort Principle:

In Bubble Sort, the array is traversed multiple times. In each pass, adjacent elements are compared ($arr[j]$ and $arr[j+1]$); if they are out of order ($arr[j] > arr[j+1]$), they are swapped. As a result, the largest element in the unsorted segment "bubbles up" to its final position at the end of the array.

public static void bubbleSort(int[] arr) {
    int n = arr.length;
    for (int i = 0; i < n - 1; i++) {           // Outer loop: n - 1 passes
        for (int j = 0; j < n - 1 - i; j++) {   // Inner loop: comparisons
            if (arr[j] > arr[j + 1]) {
                // Swap adjacent elements
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

5. Selection Sort Algorithm: Minimum Index Exchange

Selection Sort
Selection Sort Principle:

In Selection Sort, the algorithm divides the array into a sorted prefix and an unsorted suffix. In each pass $i$, it scans the unsorted segment to find the index of the minimum element and swaps it with the element at position $i$.

public static void selectionSort(int[] arr) {
    int n = arr.length;
    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j; // Update index of minimum
            }
        }
        // Swap minimum element with element at index i
        int temp = arr[minIndex];
        arr[minIndex] = arr[i];
        arr[i] = temp;
    }
}

6. Two-Dimensional (2D) Arrays: Matrices & Traversal Geometry

2D Matrix Operations
Matrix Structure in Java:

A 2D array is an array of arrays representing a grid with $M$ rows and $N$ columns:

int[][] matrix = new int[4][4]; // 4x4 matrix
Geometrical Index Formulas for Square Matrices ($N \times N$):
Matrix Geometrical ZoneIndex ConditionVisual Description
Left (Primary) Diagonali == jTop-left $(0,0)$ to bottom-right $(N-1, N-1)$
Right (Secondary) Diagonali + j == N - 1Top-right $(0, N-1)$ to bottom-left $(N-1, 0)$
Boundary Elementsi == 0 || i == N-1 || j == 0 || j == N-1Outer perimeter border cells
Non-Boundary Elementsi > 0 && i < N-1 && j > 0 && j < N-1Inner core cells

7. Complete ICSE Board Program: Array Searching & Sorting Pipeline

Board Class Implementation
Model Program 1: Input, Bubble Sort & Binary Search on 15 Students' Marks
import java.util.Scanner;

public class StudentMarksArray {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] marks = new int[15];

        System.out.println("Enter marks for 15 students:");
        for (int i = 0; i < marks.length; i++) {
            System.out.print("Student " + (i + 1) + ": ");
            marks[i] = sc.nextInt();
        }

        // 1. Sort using Bubble Sort
        for (int i = 0; i < marks.length - 1; i++) {
            for (int j = 0; j < marks.length - 1 - i; j++) {
                if (marks[j] > marks[j + 1]) {
                    int temp = marks[j];
                    marks[j] = marks[j + 1];
                    marks[j + 1] = temp;
                }
            }
        }

        System.out.println("
Sorted Marks (Ascending Order):");
        for (int m : marks) {
            System.out.print(m + " ");
        }
        System.out.println();

        // 2. Search using Binary Search
        System.out.print("
Enter mark to search: ");
        int searchKey = sc.nextInt();

        int low = 0, high = marks.length - 1, foundIndex = -1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (marks[mid] == searchKey) {
                foundIndex = mid;
                break;
            } else if (searchKey < marks[mid]) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }

        if (foundIndex != -1) {
            System.out.println("Search Successful! Mark " + searchKey + " found at sorted position " + (foundIndex + 1));
        } else {
            System.out.println("Search Unsuccessful: Mark " + searchKey + " is not present.");
        }
    }
}

8. Complete ICSE Board Program: 2D Matrix Diagonal & Transpose Analyzer

Board Class Implementation
Model Program 2: 4x4 Matrix Diagonals Summation & Transpose Display
import java.util.Scanner;

public class MatrixAnalysis {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[][] a = new int[4][4];

        System.out.println("Enter elements for 4x4 matrix:");
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                a[i][j] = in.nextInt();
            }
        }

        int leftDiagonalSum = 0;
        int rightDiagonalSum = 0;

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (i == j) leftDiagonalSum += a[i][j];
                if (i + j == 3) rightDiagonalSum += a[i][j];
            }
        }

        System.out.println("
Original Matrix:");
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(a[i][j] + "	");
            }
            System.out.println();
        }

        System.out.println("
Left Diagonal Sum : " + leftDiagonalSum);
        System.out.println("Right Diagonal Sum: " + rightDiagonalSum);

        System.out.println("
Transpose of Matrix (Rows swapped with Columns):");
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(a[j][i] + "	"); // Row-column transposed!
            }
            System.out.println();
        }
    }
}

Common Misconceptions & Examiner Traps

Common Misconception

Writing arr.length() with parentheses when finding array size

Scientific Reality & Correction

Array length is a field, NOT a method. Write 'arr.length' (no parentheses).

Common Misconception

Executing binary search on an unsorted array

Scientific Reality & Correction

Binary search logic relies strictly on sorted order. You must sort the array first if it is unsorted.

Common Misconception

Looping from index 0 to arr.length (inclusive) with '<='

Scientific Reality & Correction

Valid indices end at arr.length - 1. Using 'i <= arr.length' causes an ArrayIndexOutOfBoundsException.

Common Misconception

Confusing Right Diagonal condition in a square matrix

Scientific Reality & Correction

The secondary diagonal condition is 'i + j == N - 1', NOT 'i + j == N'.

Architectural Blueprint : Arrays

ICSE Class 10 Java : 1D & 2D Array Memory Model, Searching & Sorting Pipelines 1D Array Memory Indexing : int[] arr = new int[6]; arr[0] = 14 arr[1] = 27 arr[2] = 39 arr[3] = 45 arr[4] = 68 arr[5] = 82 Searching Algorithms Comparison • Linear Search: Works on unsorted arrays. Sequential scan. O(n) time. • Binary Search: Requires sorted array. Divide & conquer (mid). O(log n) time. Sorting Algorithms Comparison • Bubble Sort: Compares adjacent elements arr[j] > arr[j+1]. Sinks largest to end. • Selection Sort: Finds minimum element in unsorted range; swaps into position i. 2D Array Matrices & Boundary Analysis • Declaration: int[][] mat = new int[M][N]; (M rows, N columns, zero-indexed). • Left Diagonal Condition: i == j; Right Diagonal Condition: i + j == N - 1. • Boundary Elements: i == 0 || i == M - 1 || j == 0 || j == N - 1.

Chapter Summary & 10 Key Takeaways

Takeaway 1
An Array is a composite, indexed data structure storing a fixed number of homogeneous (same type) elements in contiguous Heap memory locations.
Takeaway 2
Arrays are zero-indexed: valid indices for an array of size $N$ range strictly from $0$ to $N - 1$; attempting to access index $N$ throws `ArrayIndexOutOfBoundsException`.
Takeaway 3
The length of an array is determined by its public, read-only field `arr.length` (note: field without parentheses, unlike `str.length()`).
Takeaway 4
Linear Search inspects elements sequentially from index 0 to $N - 1$; it works on unsorted data with average time complexity $O(n)$.
Takeaway 5
Binary Search requires the array to be pre-sorted; it repeatedly halves the search interval by testing the midpoint ($mid = (low + high)/2$), achieving $O(\log n)$ complexity.
Takeaway 6
Bubble Sort operates by comparing adjacent pairs ($arr[j] > arr[j+1]$) and swapping them if out of order, bubbling the largest value to the end in each pass.
Takeaway 7
Selection Sort locates the smallest element in the unsorted subarray and swaps it with the element at the beginning index $i$, performing exactly $N - 1$ outer passes.
Takeaway 8
Two-Dimensional (2D) Arrays represent matrices stored in row-major order: `arr[i][j]` accesses row $i$ and column $j$.
Takeaway 9
In a square matrix of size $N \times N$, elements on the Left (Primary) Diagonal satisfy $i == j$, while elements on the Right (Secondary) Diagonal satisfy $i + j == N - 1$.
Takeaway 10
Arrays in Java are reference objects created dynamically using the `new` operator; array variables hold memory references on the Stack.

Check Your Understanding (Diagnostic Practice Questions)

Diagnostic questions testing core conceptual clarity. Answers are hidden initially — solve each problem first, then click to reveal the step-by-step verified solution.

1
What is an Array in Java? Why are array indices zero-based?
Reveal Answer & Explanation
Answer: An Array is a composite, indexed, reference data structure that stores a fixed-size sequential collection of elements of the same data type in contiguous memory locations on the Heap. Array indices are zero-based because the index represents the memory offset (displacement) from the base memory address of the first element. The address of element i is calculated directly as: 'Base_Address + (i * element_size)'. For the first element, offset is 0, hence index is 0.
2
Differentiate between Linear Search and Binary Search in terms of prerequisites and performance.
Reveal Answer & Explanation
Answer:
  1. Prerequisite: Linear Search works on any array regardless of order (unsorted or sorted); Binary Search strictly requires the array to be sorted prior to searching. 2. Mechanism: Linear Search checks elements sequentially one by one from start to finish; Binary Search uses divide-and-conquer by repeatedly halving the search space. 3. Complexity: Linear Search has worst-case time complexity O(n); Binary Search has worst-case time complexity O(log n), making it exponentially faster for large datasets.

3
Explain the working principle of Bubble Sort. Why does the inner loop terminate at (n - 1 - i)?
Reveal Answer & Explanation
Answer: Bubble Sort compares adjacent pairs of elements across multiple passes, swapping them if the left element is greater than the right element. In each pass, the largest remaining element sinks to its correct position at the end. The inner loop terminates at 'n - 1 - i' because after 'i' outer passes, the last 'i' elements are already guaranteed to be in their final sorted positions, making further comparisons on them redundant.
4
Explain how Selection Sort works. State the number of comparisons made in an array of N elements.
Reveal Answer & Explanation
Answer: Selection Sort works by dividing the array into sorted and unsorted regions. In each pass i from 0 to N-2, it scans the unsorted region (from i+1 to N-1) to locate the index of the minimum element, and then performs a single swap placing that minimum element into index i. The total number of comparisons is fixed at N(N - 1)/2 for an array of size N, resulting in O(N^2) time complexity.
5
What is the difference between 'arr.length' and 'str.length()' in Java?
Reveal Answer & Explanation
Answer: 'arr.length' is a public, final property (data field) of an array object that stores the fixed capacity of the array (written without parentheses). In contrast, 'str.length()' is a member method of the String class that dynamically computes and returns the character count of the string (written with parentheses).
6
Write the index condition for accessing Left Diagonal and Right Diagonal elements in an N x N matrix.
Reveal Answer & Explanation
Answer: In an N x N square matrix: 1. Left (Primary) Diagonal condition: 'i == j' (where row index equals column index). 2. Right (Secondary) Diagonal condition: 'i + j == N - 1' (where row index and column index sum to N - 1).
7
What exception is thrown when accessing an invalid array index? Give an example.
Reveal Answer & Explanation
Answer: The runtime exception thrown is 'ArrayIndexOutOfBoundsException'. For example, if an array is declared as 'int[] arr = new int[5];', valid indices are 0, 1, 2, 3, 4. Attempting to execute 'arr[5] = 50;' or 'arr[-1] = 10;' immediately crashes the program with this exception.
8
What is the transpose of a matrix? How is it displayed using a 2D array?
Reveal Answer & Explanation
Answer: The transpose of a matrix is a new matrix formed by interchanging its rows into columns (i.e., element at row i, column j moves to row j, column i). Given a 2D array 'a[M][N]', its transpose can be printed without creating a new array by swapping loop indices during display: 'System.out.print(a[j][i] + "\t");' inside a nested loop where outer loop iterates j from 0 to N-1 and inner loop iterates i from 0 to M-1.
Finished Studying This Chapter?
READY TO PRACTICE?

Timed CBT Practice Tests (Exam Simulator)

Put your concepts to the test with official curriculum-aligned Foundation and Advanced practice tests. Get instant accuracy scores, time metrics, and step-by-step verified explanations.