Working with Arrays in Java

Arrays are data structures used to store multiple values of the same type in contiguous memory locations, each accessible via a zero-based index. They eliminate the need for declaring numerous individual variables when handling collections of data, such as storing grades for multiple students.

Declaring and Initializing Arrays

Arrays can be declared and initialized dynamically or statically.

Dynamic Initialization: Specify the size at creation.

int[] numbers = new int[10];
String[] words = new String[5];

Static Initialization: Define elements directly, letting the compiler determine size.

int[] values = new int[]{10, 20, 30, 40};
double[] measurements = {1.5, 2.7, 3.9};

Static initialization requires element types to match the array declaration and can be split into steps, but the shorthand form must not be separated.

double[] data;
data = new double[]{2.1, 4.3}; // Valid
double[] items;
items = {5.6, 7.8}; // Invalid, causes compilation error

Uninitialized arrays receive default values based on thier type, such as 0 for integers or null for objects.

Accessing and Traversing Array Elements

Elements are accessed using indices within the range [0, length-1]. Accessing out side this range throws a ArrayIndexOutOfBoundsException, and using a null reference results in a NullPointerException.

Traversal Methods:

  1. Standard for-loop:
for (int i = 0; i < array.length; i++) {
    System.out.print(array[i] + " ");
}
  1. Enhanced for-loop (for-each):
for (int value : array) {
    System.out.println(value);
}

This method iterates all elements but does not provide index information. 3. Using Arrays.toString():

import java.util.Arrays;
int[] sample = {1, 2, 3};
String output = Arrays.toString(sample); // Output: [1, 2, 3]

A custom implementation can mimic this functionality:

public static String convertToString(int[] input) {
    if (input == null) return "null";
    if (input.length == 0) return "[]";
    StringBuilder result = new StringBuilder("[");
    for (int i = 0; i < input.length; i++) {
        result.append(input[i]);
        if (i != input.length - 1) result.append(", ");
    }
    return result.append("]").toString();
}

Memory Representation and Parameter Passing

In Java, arrays are objects stored in the heap. A reference variable in the stack holds the memory address of the array. For example:

int[] dataSet = new int[8];

This allocates heap space for eight integers and stores the address in dataSet.

When an array is passed to a method, the reference value (address) is copied, allowing modifications within the method to affect the original array, as both references point to the same memory.

Copying Arrays

Creating a copy involves allocating new memory and transferring elements. A basic approach uses a loop:

public static int[] duplicateArray(int[] original) {
    int[] copy = new int[original.length];
    for (int index = 0; index < original.length; index++) {
        copy[index] = original[index];
    }
    return copy;
}

This produces an independent array with identical content.

Tags: java Arrays programming data-structures Tutorial

Posted on Mon, 06 Jul 2026 17:50:20 +0000 by forgun