Module 3

Module 3: Arrays and Data Structures

🧩 Module 3: Arrays and Data Structures

3.1 Introduction

In programming, data often comes in collections — lists of numbers, names, or objects. Java provides arrays and a set of data structures to store and organize this data efficiently.

In this module, you’ll learn:

  • How to declare, initialize, and use arrays
  • Looping through arrays
  • Multidimensional arrays
  • Array utilities and sorting
  • Introduction to Java’s collection classes

3.2 What Is an Array?

An array is a fixed-size collection of elements of the same type. Each element is accessed by an index, starting at 0.

// Declaring an array
int[] numbers = new int[5];   // array of 5 integers

// Assigning values
numbers[0] = 10;
numbers[1] = 20;

// Accessing elements
System.out.println(numbers[0]); // Output: 10

3.3 Array Initialization

You can initialize arrays directly with values:

int[] scores = {90, 85, 88, 95, 76};
String[] names = {"Alice", "Bob", "Charlie"};

Access elements by index:

System.out.println(scores[1]);  // Output: 85

Find the array length:

System.out.println(scores.length); // Output: 5

3.4 Looping Through Arrays

Use a for loop or the enhanced for-each loop to process arrays.

int[] scores = {90, 85, 88, 95, 76};

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

// Enhanced for loop
for (int score : scores) {
    System.out.println(score);
}

3.5 Finding the Largest Element

int[] nums = {12, 7, 22, 5, 19};
int max = nums[0];

for (int n : nums) {
    if (n > max) {
        max = n;
    }
}
System.out.println("Largest number: " + max);

3.6 Multidimensional Arrays

Java supports 2D arrays, useful for representing tables or grids.

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

// Access elements
System.out.println(matrix[1][2]); // Output: 6

// Loop through rows and columns
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();
}

3.7 The Arrays Utility Class

The java.util.Arrays class provides useful static methods for working with arrays.

import java.util.Arrays;

int[] numbers = {4, 2, 9, 1, 5};

Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)); // [1, 2, 4, 5, 9]

int index = Arrays.binarySearch(numbers, 4);
System.out.println("Found 4 at index: " + index);

3.8 Copying Arrays

You can copy arrays using methods or loops.

// Using loop
int[] original = {1, 2, 3};
int[] copy = new int[original.length];

for (int i = 0; i < original.length; i++) {
    copy[i] = original[i];
}

// Using Arrays.copyOf
int[] copy2 = Arrays.copyOf(original, original.length);

3.9 Introduction to Data Structures

Beyond arrays, Java offers dynamic data structures through the java.util package.

  • ArrayList – dynamic list
  • HashSet – collection of unique elements
  • HashMap – key-value pairs

ArrayList Example

import java.util.ArrayList;

ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");

System.out.println(fruits);
fruits.remove("Banana");
System.out.println("After removal: " + fruits);

HashMap Example

import java.util.HashMap;

HashMap<String, Integer> ages = new HashMap<>();
ages.put("Alice", 25);
ages.put("Bob", 30);

System.out.println("Alice's age: " + ages.get("Alice"));

3.10 Practice Exercises

  1. Average Calculator: Store 5 test scores in an array and compute the average.
  2. Min & Max Finder: Find both the smallest and largest numbers in an array.
  3. Matrix Sum: Add two 3×3 matrices.
  4. Array Reversal: Print an array in reverse order.
  5. List Manager: Use an ArrayList to add, remove, and display strings.

3.11 Summary

  • Arrays store fixed-size data collections of the same type.
  • Use loops and Arrays utilities to process and manage arrays.
  • Multidimensional arrays represent tables or grids.
  • ArrayList, HashSet, and HashMap are flexible, built-in data structures.

Next up: Module 4: Methods and Functions — you’ll learn how to modularize your code and reuse logic efficiently.

Latest Notes

View Archive [ -> ]