Tutorials Logic, IN info@tutorialslogic.com

Arrays in Java 1D, 2D, Sorting

Creating and Accessing Arrays

Arrays can be created with values directly or with a fixed size. Access elements using square brackets and a zero-based index.

Array Basics

Array Basics
public class ArrayBasics {
    public static void main(String[] args) {
        int[] marks = {85, 90, 76};
        System.out.println(marks[0]);

        marks[1] = 95;
        System.out.println("Length: " + marks.length);
    }
}

Looping Through Arrays

Use a normal for loop when you need the index. Use enhanced for when you only need values.

Array Traversal

Array Traversal
public class ArrayTraversal {
    public static void main(String[] args) {
        String[] names = {"Asha", "Ravi", "Meera"};

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

        for (String name : names) {
            System.out.println(name.toUpperCase());
        }
    }
}

Searching, Sorting, and Copying

The Arrays utility class provides helper methods for sorting, searching, copying, comparing, and printing arrays.

Arrays Utility Class

Arrays Utility Class
import java.util.Arrays;

public class ArraysUtilityDemo {
    public static void main(String[] args) {
        int[] numbers = {5, 1, 9, 3};
        Arrays.sort(numbers);

        System.out.println(Arrays.toString(numbers));
        System.out.println(Arrays.binarySearch(numbers, 9));

        int[] copy = Arrays.copyOf(numbers, numbers.length);
        System.out.println(Arrays.equals(numbers, copy));
    }
}
  • binarySearch requires the array to be sorted first.

2D and Jagged Arrays

A 2D array is an array of arrays. Rows can have equal length like a matrix, or different lengths like a jagged table.

2D Array

2D Array
public class MatrixDemo {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6}
        };

        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < matrix[row].length; col++) {
                System.out.print(matrix[row][col] + " ");
            }
            System.out.println();
        }
    }
}
Before you move on

Arrays in Java 1D, 2D, Sorting Mastery Check

4 checks
  • Arrays can be created with values directly or with a fixed size.
  • Access elements using square brackets and a zero-based index.
  • Use a normal for loop when you need the index.
  • Use enhanced for when you only need values.

Array Failure Boundary

  • Invalid index

    Java arrays use indexes from zero through length - 1. Guard calculated indexes and test empty arrays before reading the first or final element.

Core Java Questions Learners Ask

Indexes start at zero, so an array of length n ends at n - 1.

No. Use ArrayList when the number of elements must change.

It creates a new array and copies as many elements as fit.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.