Back

Activity 5.1 — Introduction to Arrays

divider

Introduction

Arrays

Storing many values under one name

Why Arrays Exist

  • Managing many related variables is messy
  • Arrays group related values together
  • Each value has a position

Creating an Array (Pattern 1)

// Creating an array with initial values
int[] scores = {84, 91, 76, 88, 95};
// Indexes start at 0
System.out.println(scores[0]); // 84
System.out.println(scores[2]); // 76

Use this when you already know the values.

Indexes vs Elements

// Index vs element (visualized)
// index: 0 1 2 3
// value: 12 25 30 18
int[] points = {12, 25, 30, 18};
System.out.println(points[1]); // 25
  • Index = position
  • Element = value at that position
  • Indexes start at 0

Creating an Array (Pattern 2)

// Creating an empty array of a given size
int[] quizScores = new int[4];
// Default values for int arrays are 0
System.out.println(quizScores[0]); // 0
quizScores[0] = 85;
quizScores[1] = 92;

This creates empty slots that you fill later.

Printing Arrays (New Concept)

// Why do we need this?
import java.util.Arrays;
// Arrays don't print nicely by default.
// Arrays.toString(...) converts the array into readable text.
int[] nums = {3, 6, 9};
System.out.println(Arrays.toString(nums));
  • import gives us access to helper tools
  • Arrays.toString makes arrays readable
  • This is why we use the import today

'F' → Fullscreen

Objectives

  • icon Create arrays using two different patterns.
  • icon Access and modify elements using indexes.
  • icon Use .length to reason about array size.
  • icon Explain what an import statement does.
divider

Activity Tasks

  • icon Create a project named Activity5_1_Arrays.
  • icon Complete Tasks 1–4 in order.
  • icon No loops yet — focus on positions and indexes.

Task 1: Create + Access

public class Program {
public static void main(String[] args) {
// Task 1 — Create + Access
String[] classes = {"Math", "English", "Science", "History"};
System.out.println("First class: " + classes[0]);
System.out.println("Last class: " + classes[3]);
System.out.println("Number of classes: " + classes.length);
// In a comment:
// If the length is 4, why is the last index 3?
}
}

Task 2: Modify Elements

import java.util.Arrays;
public class Program {
public static void main(String[] args) {
9 collapsed lines
// Task 1 — Create + Access
String[] classes = {"Math", "English", "Science", "History"};
System.out.println("First class: " + classes[0]);
System.out.println("Last class: " + classes[3]);
System.out.println("Number of classes: " + classes.length);
// In a comment:
// If the length is 4, why is the last index 3?
// Task 2 — Modify Elements
int[] temps = {72, 68, 75, 70, 69};
System.out.println("Temp at index 2: " + temps[2]);
temps[0] = 74;
temps[temps.length - 1] = 71;
System.out.println("Updated temps: " + Arrays.toString(temps));
}
}

Task 3: Empty Array

import java.util.Arrays;
public class Program {
public static void main(String[] args) {
19 collapsed lines
// Task 1 — Create + Access
String[] classes = {"Math", "English", "Science", "History"};
System.out.println("First class: " + classes[0]);
System.out.println("Last class: " + classes[3]);
System.out.println("Number of classes: " + classes.length);
// In a comment:
// If the length is 4, why is the last index 3?
// Task 2 — Modify Elements
int[] temps = {72, 68, 75, 70, 69};
System.out.println("Temp at index 2: " + temps[2]);
temps[0] = 74;
temps[temps.length - 1] = 71;
System.out.println("Updated temps: " + Arrays.toString(temps));
// Task 3 — Empty Array
int[] points = new int[3];
points[0] = 10;
points[1] = 15;
points[2] = 20;
System.out.println("Points: " + Arrays.toString(points));
}
}

Task 4: Index Math

import java.util.Arrays;
public class Program {
public static void main(String[] args) {
28 collapsed lines
// Task 1 — Create + Access
String[] classes = {"Math", "English", "Science", "History"};
System.out.println("First class: " + classes[0]);
System.out.println("Last class: " + classes[3]);
System.out.println("Number of classes: " + classes.length);
// In a comment:
// If the length is 4, why is the last index 3?
// Task 2 — Modify Elements
int[] temps = {72, 68, 75, 70, 69};
System.out.println("Temp at index 2: " + temps[2]);
temps[0] = 74;
temps[temps.length - 1] = 71;
System.out.println("Updated temps: " + Arrays.toString(temps));
// Task 3 — Empty Array
int[] points = new int[3];
points[0] = 10;
points[1] = 15;
points[2] = 20;
System.out.println("Points: " + Arrays.toString(points));
// Task 4 — Index Math
int[] nums = {4, 7, 2, 9};
int sum = nums[0] + nums[nums.length - 1];
int diff = nums[1] - nums[2];
System.out.println("First + Last: " + sum);
System.out.println("Second - Third: " + diff);
}
}
divider

Sample Output

Terminal window
First class: Math
Last class: History
Number of classes: 4
Temp at index 2: 75
Updated temps: [74, 68, 75, 70, 71]
Points: [10, 15, 20]
First + Last: 13
Second - Third: 5
divider

Reflection Questions

  1. What is the difference between an index and an element?
  2. Why does the last index equal length - 1?
  3. When would creating an empty array be useful?
  4. Why did we need an import statement today?
divider

Submission

Submit your completed activity and reflection answers.

Activity Complete