Java Arrays: Allocation, Sorting, and Two-Dimensional Structures

Arrays

Arrays in Java are reference types, meaning array variables store objects rather than primitive values.

Dynamic Allocation

int scores[] = new int[5];
int[] data = new int[5];

Two-Step Declaration and Definition

int numbers1[];
int[] numbers2;  // Initially null
numbers1 = new int[10];

Getting Array Length

int size = values.length;

Static Initialization

double[] weights = {1.0, 2.0, 3.0, 4.0, 5.0};

Default Values

Unlike C++, uninitialized array elements receive default values automatically—integers become 0, floating-point types become 0.0, and object references become null.

Reference Behavior

When asigning one array variable to another, the target variable receives the address of the source array. Both variables then point to the same memory locasion. Modifying elements through one variable affects the other.

int[] source = {10, 20, 30};
int[] target = source;  // Shares the same memory

Array Copying

To create an independent copy, allocate new memory and transfer elements individually:

int[] source = {10, 20, 30};
int[] target = new int[source.length];
for (int i = 0; i < source.length; i++) {
    target[i] = source[i];
}

The key is allocating fresh memory with new.

Array Expansion

Using Java's automatic garbage collection, array resizing works by creating a larger array, copying contents, and reassigning the reference:

int[] source = {10, 20, 30};
int[] expanded = new int[source.length + 1];
for (int i = 0; i < source.length; i++) {
    expanded[i] = source[i];
}
expanded[source.length] = 40;
source = expanded;
for (int i = 0; i < source.length; i++) {
    System.out.print(source[i] + " ");
}

Bubble Sort

int[] data = {20, 49, 39, 48, 59, 20, 49, 38, 59, 48, 59, 49};
boolean swapped;
for (int i = 0; i < data.length - 1; i++) {
    swapped = false;
    for (int j = 0; j < data.length - i - 1; j++) {
        if (data[j] < data[j + 1]) {
            int temp = data[j];
            data[j] = data[j + 1];
            data[j + 1] = temp;
            swapped = true;
        }
    }
    if (!swapped) {
        break;
    }
}
for (int i = 0; i < data.length; i++) {
    System.out.print(data[i] + " ");
}

The outer loop runs length - 1 times because each element only needs comparison with adjacent values. The inner loop shrinks by one each iteration since the largest remaining elements bubble to the end. The swapped flag enables early termination when the array becomes sorted.

Linear Search

int[] collection = {23, 45, 67, 89, 12};
int target = 67;
int index = -1;
for (int i = 0; i < collection.length; i++) {
    if (collection[i] == target) {
        index = i;
        break;
    }
}

Two-Dimensional Arrays

Declaration Styles

int[][] matrix1;
int[] matrix2[];
int matrix3[][];

Inline Array Definition

String[] items = new String[]{"a", "b", "c"};
// Size specification is not allowed inside the brackets

Static Initialization

int[][] grid = {{20, 49}, {39, 48}, {59, 20}, {49, 38}, {59, 48}, {59, 49}};

Dynamic Initialization

int[][] array;
array = new int[2][3];

int[][] matrix = new int[2][3];

Traversing a 2D Array

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

Jagged Arrays

Since Java's outer array actually holds references to inner arrays, you can create arrrays with varying row lengths:

int[][] jagged = new int[3][];
for (int i = 0; i < jagged.length; i++) {
    jagged[i] = new int[i + 1];
    for (int j = 0; j < jagged[i].length; j++) {
        jagged[i][j] = i + 1;
    }
}

Without allocating an inner array, jagged[i] remains null. Static initialization also supports unequal row sizes.

Memory Structure

The outer array stores memory addresses pointing to individual inner arrays rather than the actual data values directly.

Tags: java Arrays bubble sort 2D arrays Sorting

Posted on Sat, 29 Aug 2026 16:45:40 +0000 by usamaalam