Fundamentals of Single and Two-Dimensional Arrays in Java

One-Dimensional Arrays

Concept

An array is a data structure that stores a contiguous block of homogeneous elements.

Static Declaration

Elements are assigned immediately upon creation.

String[] colors = {"Crimson", "Azure", "Emerald"};
String[] hues = new String[]{"Crimson", "Azure", "Emerald"};

Element Access

Values are retrieved or modified using a zero-based index. Requesting an invalid index results in an out-of-bounds exception.

String[] hues = {"Crimson", "Azure", "Emerald"};
System.out.println(hues[0]); // Outputs Crimson
hues[1] = "Gold";

Length Determination

The length property provides the total number of slots.

System.out.println("Total elements: " + hues.length);

Dynamic Declaration

Defines capacity and data type without immediate value assignment.

double[] metrics = new double[5];

Default Allocation

CategoryTypesDefault Value
Integer Primitivesbyte, short, int, long, char0
Floating-Pointfloat, double0.0
Booleanbooleanfalse
Reference TypesObjects, Arrays, Stringnull

Iteration

Sequential traversal is typically handled via loops for tasks like aggregation or searching.

int[] values = {10, 20, 30, 40};
for (int i = 0; i < values.length; i++) {
    System.out.println(values[i]);
}

Practical Implementations

Student Grade Analysis

public static void main(String[] args) {
    double[] grades = new double[5];
    java.util.Scanner input = new java.util.Scanner(System.in);
    for (int i = 0; i < grades.length; i++) {
        System.out.println("Input grade for student #" + (i + 1) + ":");
        grades[i] = input.nextDouble();
    }

    double totalScore = 0;
    double highest = grades[0];
    double lowest = grades[0];

    for (double grade : grades) {
        totalScore += grade;
        if (grade > highest) highest = grade;
        if (grade < lowest) lowest = grade;
    }

    double average = totalScore / grades.length;
    System.out.println("Average: " + average);
    System.out.println("Highest: " + highest);
    System.out.println("Lowest: " + lowest);
}

Peak Value Detection

public static void main(String[] args) {
    int[] measurements = {42, 8500, 12000, 21000, 9800, -12};
    int peak = measurements[0];
    for (int cursor = 1; cursor < measurements.length; cursor++) {
        if (measurements[cursor] > peak) {
            peak = measurements[cursor];
        }
    }
    System.out.println("Peak value: " + peak);
}

Deck Construction and Shuffling

public static void main(String[] args) {
    String[] deck = new String[54];
    String[] suits = {"♠", "♥", "♦", "♣"};
    String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

    int position = 0;
    for (String rank : ranks) {
        for (String suit : suits) {
            deck[position++] = suit + rank;
        }
    }
    deck[position++] = "Black Joker";
    deck[position] = "Red Joker";

    java.util.Random rng = new java.util.Random();
    for (int i = deck.length - 1; i > 0; i--) {
        int swapIdx = rng.nextInt(i + 1);
        String tmp = deck[i];
        deck[i] = deck[swapIdx];
        deck[swapIdx] = tmp;
    }

    for (String card : deck) {
        System.out.print(card + " ");
    }
}

Two-Dimensional Arrays

Concept

A two-dimensional array is essentially an array where each element is a one-dimensional array, forming a matrix-like structure.

Static Initialization

int[][] grid = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

Dynamic Initialization

String[][] seatingChart = new String[3][5];

Element Access

Data retrieval can occur directly via row and column indices, or by first extracting the inner array.

int[] targetRow = grid[1];
int val = targetRow[2]; // Retrieves 6
System.out.println(grid[1][2]); // Direct access retrieves 6

Iteration

Nested loops are required to traverse all elements.

for (int r = 0; r < grid.length; r++) {
    for (int c = 0; c < grid[r].length; c++) {
        System.out.print(grid[r][c] + " ");
    }
    System.out.println();
}

Matrix Scrambling

public static void main(String[] args) {
    int[][] puzzle = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    java.util.Random randomizer = new java.util.Random();

    for (int r = 0; r < puzzle.length; r++) {
        for (int c = 0; c < puzzle[r].length; c++) {
            int randR = randomizer.nextInt(puzzle.length);
            int randC = randomizer.nextInt(puzzle[r].length);
            int holder = puzzle[r][c];
            puzzle[r][c] = puzzle[randR][randC];
            puzzle[randR][randC] = holder;
        }
    }

    for (int[] row : puzzle) {
        for (int cell : row) {
            System.out.print(cell + "\t");
        }
        System.out.println();
    }
}

Tags: java Arrays Data Structures Two-Dimensional Arrays programming

Posted on Sat, 20 Jun 2026 16:32:55 +0000 by SpaceLincoln