Java Tutorial » Chapter 4 — Arrays

Chapter 4 — Array in Java

Complete chapter: introduction, creating arrays, reading input, basic programs, types of arrays, built-in functions and practice problems.

1. Introduction

What is an Array?

An array in Java is a fixed-size collection of elements of the same data type. It lets you store multiple values in a single variable name.

Without Array


int m1 = 78;
int m2 = 85;
int m3 = 92;
int m4 = 67;
int m5 = 88;

With Array


int[] marks = {78, 85, 92, 67, 88};
  • All elements are of same type (e.g. int).
  • Size is fixed when array is created.
  • Index starts from 0 to length - 1.
2. Basics

Declaration and Creation

Declaration


// Recommended
int[] numbers;

// Also valid
int marks[];

Creation (Memory Allocation)


int[] numbers = new int[5];     // 5 integers, default 0
double[] prices = new double[3]; // 3 doubles, default 0.0
String[] names = new String[2];  // 2 references, default null
Valid index range is 0 to length - 1. Access outside this range throws ArrayIndexOutOfBoundsException.
3. Initialization

Initializing Arrays

Direct Initialization (Array Literal)


int[] nums = {10, 20, 30, 40};
String[] cities = {"Delhi", "Mumbai", "Pune"};

Index-wise Initialization


int[] nums = new int[4];

nums[0] = 10;
nums[1] = 20;
nums[2] = 30;
nums[3] = 40;
4. Input

Reading Array from User


import java.util.Scanner;

public class ReadArray {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter size of array: ");
        int n = sc.nextInt();

        int[] arr = new int[n];

        System.out.println("Enter " + n + " elements:");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println("You entered:");
        for (int i = 0; i < n; i++) {
            System.out.println("Index " + i + " = " + arr[i]);
        }

        sc.close();
    }
}
5. Traversing

Traversing an Array

Using for loop


int[] arr = {10, 20, 30, 40};

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

Using enhanced for-each loop


int[] arr = {10, 20, 30, 40};

for (int value : arr) {
    System.out.println(value);
}
6. Programs

Basic Programs on Arrays

1. Sum of elements


int[] arr = {5, 10, 15, 20};
int sum = 0;

for (int x : arr) {
    sum += x;
}

System.out.println("Sum = " + sum);

2. Maximum element


int[] arr = {3, 8, 2, 10, 6};

int max = arr[0];

for (int i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
        max = arr[i];
    }
}

System.out.println("Maximum = " + max);
7. Types

Types of Arrays

1. One-Dimensional Array


int[] arr = {10, 20, 30, 40};

2. Two-Dimensional Array (Matrix)


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

System.out.println(matrix[1][2]); // 6
8. Built-in

Built-in Functions (java.util.Arrays)

Utility methods in java.util.Arrays:

Arrays.toString()


import java.util.Arrays;

int[] arr = {10, 20, 30};
System.out.println(Arrays.toString(arr)); // [10, 20, 30]

Arrays.sort()


int[] arr = {40, 10, 20, 30};

Arrays.sort(arr);

System.out.println(Arrays.toString(arr)); // [10, 20, 30, 40]

Arrays.binarySearch()


int[] arr = {10, 20, 30, 40, 50};
int index = Arrays.binarySearch(arr, 30); // 2

Arrays.equals()


int[] a = {1, 2, 3};
int[] b = {1, 2, 3};

System.out.println(Arrays.equals(a, b)); // true
9. Practice

Practice Problems

  1. Reverse an array without using a second array.
  2. Count how many elements are even and odd.
  3. Find the second largest element without sorting.
  4. Check if an array is sorted in ascending order.
  5. Merge two arrays into a third array.
  6. Count frequency of a given number in an array.
⬅️ Previous: Basic Syntax Next: Index ➡️