Back

Activity 5.7 — Array Tools & Patterns

divider

Introduction

Activity 5.7

Array Tools & Patterns

Why this lesson?

  • These tools appear frequently in AP-style questions.
  • They explain many “weird bugs” students encounter.
  • They prepare you for ArrayLists and file input.

Enhanced For Loop

Used when you want to visit every element.

// Enhanced for loop (for-each)
for (int value : nums)
{
System.out.println(value);
}
  • You get the value, not the index.
  • You cannot change the array structure.

When NOT to use for-each

  • When you need the index
  • When you need to modify elements
  • When you need partial traversal

Aliasing vs Copying

Arrays are reference types.

// Aliasing (NOT a copy)
int[] a = {1, 2, 3};
int[] b = a;
// a and b refer to the SAME array

This does NOT create a new array.

Making a Real Copy

// True copy using a loop
int[] copy = new int[a.length];
for (int i = 0; i < a.length; i++)
{
copy[i] = a[i];
}
// Library copy
import java.util.Arrays;
int[] copy = Arrays.copyOf(a, a.length);

Sorting with Arrays.sort

// Sorting with Arrays.sort
import java.util.Arrays;
int[] nums = {4, 1, 9, 2};
Arrays.sort(nums);
// nums is now {1, 2, 4, 9}
  • Sorting permanently changes the array.
  • After sorting, indexes refer to different values.

Big Takeaways

  • for-each loops are for reading, not modifying
  • Aliasing can cause unexpected changes
  • Sorting changes the meaning of indexes

'F' → Fullscreen

Objectives

  • icon Use enhanced for loops to traverse arrays.
  • icon Explain the difference between aliasing and copying.
  • icon Create a true copy of an array.
  • icon Sort an array using Arrays.sort.
divider

Activity Tasks

  • icon Create a project named Activity5_7_ArrayTools.
  • icon Complete Tasks 1–3.
  • icon Focus on understanding behavior, not memorization.

Figures: Examples

Figure 1 — Traversal Styles
// Figure 1 — Enhanced for loop vs indexed loop
int[] nums = {3, 6, 9};
// Indexed
for (int i = 0; i < nums.length; i++)
{
System.out.println(nums[i]);
}
// For-each
for (int value : nums)
{
System.out.println(value);
}
Figure 2 — Aliasing
// Figure 2 — Aliasing example
int[] a = {5, 10, 15};
int[] b = a;
b[0] = 99;
System.out.println(a[0]); // prints 99
Figure 3 — Sorting Effect
// Figure 3 — Sorting changes the array
int[] nums = {7, 3, 8, 1};
Arrays.sort(nums);
// nums is now sorted permanently

Task 1: Enhanced For Loop

Task 1 — for-each loop
public class Program
{
public static void main(String[] args)
{
// Task 1 — Enhanced For Loop Practice
//
// 1) Print all values using a for-each loop
// 2) Compute the sum using a for-each loop
// 3) Try changing a value inside the loop — what happens?
int[] nums = {4, 7, 2, 9, 5};
int sum = 0;
for (int value : nums)
{
System.out.println(value);
sum += value;
// value = 100; // try this and observe
}
System.out.println("Sum: " + sum);
}
}

Task 2: Copy vs Alias

Task 2 — Aliasing vs Copying
public class Program
{
public static void main(String[] args)
{
// Task 1 omitted
// Task 2 — Aliasing vs Copying
//
// 1) Create an alias array and modify it
// 2) Create a true copy and modify it
// 3) Observe which changes affect the original
int[] original = {1, 2, 3};
int[] alias = original;
alias[0] = 99;
int[] copy = new int[original.length];
for (int i = 0; i < original.length; i++)
{
copy[i] = original[i];
}
copy[1] = 42;
System.out.println("Original:");
for (int v : original)
{
System.out.print(v + " ");
}
System.out.println("\nCopy:");
for (int v : copy)
{
System.out.print(v + " ");
}
}
}

Task 3: Sorting

Task 3 — Arrays.sort
import java.util.Arrays;
public class Program
{
public static void main(String[] args)
{
// Tasks 1 and 2 omitted
// Task 3 — Sorting and Reasoning
//
// 1) Sort the array
// 2) Print before and after
// 3) Answer the reflection questions
int[] scores = {88, 73, 95, 67, 81};
System.out.println("Before sort:");
printArray(scores);
Arrays.sort(scores);
System.out.println("After sort:");
printArray(scores);
}
public static void printArray(int[] arr)
{
for (int v : arr)
{
System.out.print(v + " ");
}
System.out.println();
}
}
divider

Sample Output

Sample Output
4
7
2
9
5
Sum: 27
Original:
99 2 3
Copy:
99 42 3
Before sort:
88 73 95 67 81
After sort:
67 73 81 88 95
divider

Reflection

  1. Why can’t a for-each loop modify array elements?
  2. What problem does aliasing cause?
  3. Why should you be careful after sorting an array?
divider

Submission

Submit your code and reflections to the appropriate dropbox.

Activity Complete