NBKRIST Java Hub - Java Arrays Learning Guide

A Practical and Fun Way to Learn Arrays for Engineering Students

← Back to Index

Arrays in Java – Industry Insight

As a software engineer, I have relied on arrays extensively in real-world IT applications. Arrays provide fast, indexed access to data, making them ideal for performance-critical tasks. I have used arrays in projects for sorting, searching, and managing large datasets efficiently. In enterprise systems, arrays are invaluable for handling buffers, storing transaction data, and implementing algorithms. Even with advanced data structures available, arrays remain the backbone for both static and dynamic data handling. Understanding arrays helps developers write optimized, scalable, and maintainable Java code. Mastery of arrays is a key skill expected by the IT industry for any serious Java programmer.

Introduction

Arrays are containers that hold multiple values of the same type. They help manage data efficiently in Java.

class ArrayIntro {
  public static void main(String[] args) {
    int[] marks = {85, 90, 75};
    System.out.println("Total Students: " + marks.length);
  }
}
💡 Arrays make it easy to store and process large sets of related data efficiently.

Declaration and Initialization of Arrays

Arrays must be declared and initialized before use. You can assign values directly or after creation.

class ArrayDeclare {
  public static void main(String[] args) {
    int[] numbers = new int[3];
    numbers[0] = 10; numbers[1] = 20; numbers[2] = 30;

    int[] values = {5, 10, 15};
    System.out.println("Second Value: " + values[1]);
  }
}
🧠 Use new keyword for allocating memory, or initialize directly when values are known.

Storage of Array in Computer Memory

Arrays are stored in contiguous memory blocks. The variable holds a reference to the first element.

class ArrayStorage {
  public static void main(String[] args) {
    int[] data = {10, 20, 30};
    System.out.println("Reference of array: " + data);
  }
}
🧩 The array variable stores the memory address (reference), not the actual values directly.

Accessing Elements of Arrays

You can access array elements using their index, which starts at 0 in Java.

class AccessArray {
  public static void main(String[] args) {
    String[] cars = {"BMW", "Audi", "Tesla"};
    System.out.println("First car: " + cars[0]);
  }
}
🎯 Indexing starts at 0, so cars[0] gives the first element.

Operations on Array Elements

Common operations include traversing, summing, finding max/min, etc.

class ArrayOps {
  public static void main(String[] args) {
    int[] nums = {1, 2, 3, 4, 5};
    int sum = 0;
    for (int n : nums) sum += n;
    System.out.println("Sum = " + sum);
  }
}
⚙️ Use loops to process arrays easily with operations like sum or average.

Assigning Array to Another Array

Assigning one array to another copies the reference, not the actual data.

class AssignArray {
  public static void main(String[] args) {
    int[] a = {10, 20, 30};
    int[] b = a;
    b[1] = 99;
    System.out.println(a[1]); // Prints 99
  }
}
⚠️ Both arrays now refer to the same memory, so changes reflect in both.

Dynamic Change of Array Size

In Java, core arrays (the ones declared using []) have a fixed size once created. You cannot directly change their size dynamically like in some scripting languages (e.g., Python lists). However, you can simulate dynamic resizing by creating a new array with a different size and copying the old elements into it — this is what happens internally in classes like ArrayList.

public class RawDynamicArray {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3};  // initial fixed-size array
        System.out.println("Original array:");
        printArray(numbers);

        // Let's "expand" the array to hold one more element
        int newElement = 4;
        int[] temp = new int[numbers.length + 1]; // new larger array

        // Copy elements manually (you can also use System.arraycopy)
        for (int i = 0; i < numbers.length; i++) {
            temp[i] = numbers[i];
        }

        // Add new element
        temp[temp.length - 1] = newElement;

        // Reassign reference
        numbers = temp;

        System.out.println("After resizing:");
        printArray(numbers);
    }

    static void printArray(int[] arr) {
        for (int n : arr) System.out.print(n + " ");
        System.out.println();
    }
}
🚀 To resize, use Arrays.copyOf() or use ArrayList for flexible collections.

Arrays as Vectors

Arrays can be used to represent vectors in mathematical operations.

class ArrayVector {
  public static void main(String[] args) {
    int[] A = {1, 2, 3};
    int[] B = {4, 5, 6};
    int[] sum = new int[A.length];
    for (int i = 0; i < A.length; i++) sum[i] = A[i] + B[i];
    for (int s : sum) System.out.print(s + " ");
  }
}
📈 Arrays can represent mathematical vectors — useful in graphics, physics, and data analysis.