Multi-Dimensional Array Operations and Common Algorithms in Java

When declaring a recatngular matrix, the top-level reference prints as a nested type signature, row reefrences print as one-dimensional signatures, and scalar elements yield thier default values.

int[][] grid = new int[3][4];
System.out.println(grid);       // e.g., [[I@6d06d69c
System.out.println(grid[0]);    // e.g., [I@7852e922
System.out.println(grid[0][0]); // 0

The same rules apply to double[][]: row references display as [D..., and every cell defaults to 0.0. For reference types such as String[][], each slot defaults to null.

String[][] pairs = new String[2][3];
System.out.println(pairs[0]);    // [Ljava.lang.String;...
System.out.println(pairs[0][0]); // null

A ragged array allocates only the first dimension initially, leaving inner references as null until explicitly constructed.

String[][] ragged = new String[4][];
System.out.println(ragged[1]); // null

ragged[1] = new String[]{"x", "y"};
ragged[2] = new String[2];
ragged[2][0] = "a";

Aggregating an Irregular Matrix

int[][] data = {
    {3, 5, 8},
    {12, 9},
    {7, 0, 6, 4}
};

int total = 0;
for (int[] row : data) {
    for (int v : row) {
        total += v;
    }
}
System.out.println("Sum: " + total); // 54

Pascal’s Triangle via Dynamic Row Allocation

int rows = 10;
int[][] pascal = new int[rows][];

for (int i = 0; i < rows; i++) {
    pascal[i] = new int[i + 1];
    pascal[i][0] = 1;
    pascal[i][i] = 1;

    for (int j = 1; j < i; j++) {
        pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
    }
}

for (int[] row : pascal) {
    for (int n : row) {
        System.out.print(n + " ");
    }
    System.out.println();
}

Statistical Analysis on Random Data

Populate an array with values from 10 through 99, then derive basic metrics:

int[] samples = new int[10];
for (int i = 0; i < samples.length; i++) {
    samples[i] = (int) (Math.random() * 90) + 10;
}

int max = samples[0];
int min = samples[0];
int sum = 0;

for (int n : samples) {
    if (n > max) max = n;
    if (n < min) min = n;
    sum += n;
}
double mean = (double) sum / samples.length;

System.out.println("Max:  " + max);
System.out.println("Min:  " + min);
System.out.println("Sum:  " + sum);
System.out.println("Mean: " + mean);

Reference Assignment versus Deep Copy

Assigning one array variable to another creates an alias pointing to the same heap object:

int[] seq = {2, 3, 5, 7, 11, 13, 17, 19};
int[] alias = seq; // same reference
alias[0] = 0;      // visible through seq

To produce an independent duplicate, allocate fresh storage and copy the elements:

int[] clone = new int[seq.length];
for (int i = 0; i < seq.length; i++) {
    clone[i] = seq[i];
}

Reversal and Search Patterns

Reverse a one-dimensional array in-place using converging indices:

String[] labels = {"A", "B", "C", "D", "E", "F"};

for (int i = 0, j = labels.length - 1; i < j; i++, j--) {
    String tmp = labels[i];
    labels[i] = labels[j];
    labels[j] = tmp;
}

A linear scan checks every position until the target is found:

String target = "D";
int index = -1;
for (int i = 0; i < labels.length; i++) {
    if (target.equals(labels[i])) {
        index = i;
        break;
    }
}

Binary search operates on a sorted array by repeatedly halving the search interval:

int[] sorted = {-98, -34, 2, 34, 54, 66, 79, 105, 210, 333};
int key = -34;
int lo = 0, hi = sorted.length - 1;
int found = -1;

while (lo <= hi) {
    int mid = lo + (hi - lo) / 2;
    if (sorted[mid] == key) {
        found = mid;
        break;
    } else if (sorted[mid] < key) {
        lo = mid + 1;
    } else {
        hi = mid - 1;
    }
}

java.util.Arrays Utilities

import java.util.Arrays;

int[] a = {4, 2, 9, 1};
int[] b = {4, 2, 9, 1};

boolean same = Arrays.equals(a, b);
System.out.println(same);               // true

System.out.println(Arrays.toString(a)); // [4, 2, 9, 1]

Arrays.fill(a, 7);
System.out.println(Arrays.toString(a)); // [7, 7, 7, 7]

int[] c = {9, 3, 7, 1};
Arrays.sort(c);
System.out.println(Arrays.toString(c)); // [1, 3, 7, 9]

int[] d = {-20, -5, 3, 15, 40};
int pos = Arrays.binarySearch(d, 15);
System.out.println(pos); // 3

Common Runtime Exceptions

Accessing an index outside the legal range raises ArrayIndexOutOfBoundsException:

int[] nums = {10, 20, 30};
// int x = nums[3]; // exception

Dereferencing a null array handle or an uninitialized row triggers NullPointerException:

int[][] matrix = new int[3][];
// matrix[0][0] = 5; // exception because matrix[0] is null

String[] words = {"X", "Y"};
words[0] = null;
// words[0].length(); // exception

Tags: java Arrays Multi-dimensional Arrays algorithms Binary Search

Posted on Sun, 27 Sep 2026 16:50:06 +0000 by jingcleovil