Skip to main content
Java Basics
CHAPTER 09 Beginner

Arrays in Java

Updated: May 17, 2026
5 min read

# CHAPTER 9

Arrays in Java

1. Introduction

What if you need to store the marks of 100 students? Creating 100 separate variables is impractical. Arrays solve this by storing multiple values of the same type in a single, indexed container.

2. Learning Objectives

  • Declare and initialize arrays.
  • Access elements by index.
  • Traverse arrays using loops.
  • Work with multidimensional arrays.
  • Use the Arrays utility class.

3. Declaring and Initializing Arrays

java
123456789
// Method 1: Declare and allocate
int[] scores = new int[5];      // 5 elements, default 0

// Method 2: Declare and initialize
int[] marks = {90, 85, 78, 92, 88};

// Method 3: Declare, then allocate
int[] ages;
ages = new int[3];

4. Accessing Array Elements

Arrays use zero-based indexing: first element is at index 0.
java
12345
int[] fruits = {10, 20, 30, 40, 50};
System.out.println(fruits[0]);  // 10 (first)
System.out.println(fruits[4]);  // 50 (last)
fruits[2] = 99;                 // Modify element at index 2
System.out.println(fruits.length); // 5 (array size)
12
Index:   [0]  [1]  [2]  [3]  [4]
Values:   10   20   99   40   50

5. Traversing Arrays

java
1234567891011
int[] data = {10, 20, 30, 40, 50};

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

// for-each loop
for (int value : data) {
    System.out.println("Value: " + value);
}

6. Multidimensional Arrays

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

System.out.println(matrix[0][0]); // 1
System.out.println(matrix[1][2]); // 6

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

7. The Arrays Utility Class

java
12345678910111213
import java.util.Arrays;

int[] nums = {5, 2, 8, 1, 9};

Arrays.sort(nums);                        // Sort ascending
System.out.println(Arrays.toString(nums)); // [1, 2, 5, 8, 9]

int index = Arrays.binarySearch(nums, 5);  // Search for 5
System.out.println("Found at index: " + index);

int[] copy = Arrays.copyOf(nums, nums.length); // Copy array
boolean equal = Arrays.equals(nums, copy);      // Compare arrays
Arrays.fill(nums, 0);                           // Fill with 0s

8. Mini Project: Student Marks System

java
12345678910111213141516171819202122232425262728293031
import java.util.Scanner;
import java.util.Arrays;

public class StudentMarks {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("How many students? ");
        int n = sc.nextInt();
        int[] marks = new int[n];

        for (int i = 0; i < n; i++) {
            System.out.print("Enter marks for Student " + (i + 1) + ": ");
            marks[i] = sc.nextInt();
        }

        int sum = 0, max = marks[0], min = marks[0];
        for (int mark : marks) {
            sum += mark;
            if (mark > max) max = mark;
            if (mark < min) min = mark;
        }

        System.out.println("\n=== RESULTS ===");
        System.out.println("Marks: " + Arrays.toString(marks));
        System.out.println("Total: " + sum);
        System.out.printf("Average: %.2f%n", (double) sum / n);
        System.out.println("Highest: " + max);
        System.out.println("Lowest: " + min);
        sc.close();
    }
}

9. Common Mistakes

  • ArrayIndexOutOfBoundsException: Accessing arr[5] when the array has 5 elements (max index is 4).
  • Forgetting zero-based indexing: First element is arr[0], not arr[1].
  • Arrays are fixed size: Once created, the size cannot change. Use ArrayList for dynamic sizing.

10. MCQ Quiz with Answers

Question 1

What is the index of the first element?

Question 2

How do you get the length of array arr?

Question 3

What happens if you access an invalid index?

Question 4

Which creates a 2D array?

Question 5

What is the default value of int array elements?

Question 6

Which method sorts an array?

Question 7

Can arrays change size after creation?

Question 8

What does Arrays.toString() do?

Question 9

int[] a = new int[3]; creates how many elements?

Question 10

For-each loop syntax for arrays?

11. Interview Questions

  • Q: What is the difference between an Array and an ArrayList?
  • Q: How do you find the largest element in an array without sorting?
  • Q: What is the time complexity of searching an unsorted array?

12. Summary

Arrays store multiple values of the same type with fixed size. They use zero-based indexing. The Arrays utility class provides sort, search, copy, and comparison methods. For dynamic sizing, use ArrayList (covered in Chapter 20).

13. Next Chapter Recommendation

In Chapter 10: Strings in Java, we'll explore one of Java's most used classes — the String class and its extensive manipulation methods.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·