Iterating & Manipulating Arrays
Iterating & Manipulating Arrays
You already know how to declare and fill an array. Now comes the practical skill: walking through every element to read, modify, or search it, and using the built-in java.util.Arrays utility class so you do not have to re-implement common operations from scratch.
Looping Over Arrays
Java gives you three natural ways to loop over an array. Understanding when to use each one is what separates readable code from messy code.
1. Classic for loop — use this when you need the index (position number).
scores.length always returns the total number of elements. Notice the condition is i < scores.length, not i <= scores.length — valid indices run from 0 to length - 1.
scores.length or any negative index. Always double-check your loop boundary.
2. Enhanced for loop (for-each) — use this when you only need the value, not the position.
This reads as "for each score in scores". It is shorter and eliminates the off-by-one risk entirely. The trade-off is that you cannot modify the array through the loop variable — changes to score inside the loop do not affect the original array.
3. Modifying elements — you must use the index form.
The Arrays Utility Class
The java.util.Arrays class ships with the JDK and provides ready-made, highly optimised methods for the most common array tasks. Import it once at the top of your file:
Arrays.toString — Printing an Array
If you try System.out.println(scores) directly, Java prints something like [I@6d06d69c — a memory address, not the values. Arrays.toString() gives you a readable representation:
Arrays.sort — Sorting an Array
Arrays.sort() sorts an array in place (it modifies the original array). For primitive types it uses a dual-pivot quicksort — extremely fast in practice.
You can also sort a sub-range without touching the rest of the array:
Arrays.fill — Setting All Elements to One Value
Instead of looping manually to initialise every cell, Arrays.fill() does it in one call:
Arrays.copyOf — Creating a Resized Copy
Arrays.copyOf(original, newLength) creates a brand-new array. If newLength is larger than the original, the extra slots are zero-filled; if smaller, the result is truncated.
original.length after the fact. Arrays.copyOf() is the standard pattern to "resize" an array by producing a new one with the data you want.
Arrays.binarySearch — Searching a Sorted Array
Once an array is sorted, you can search it in O(log n) time with binary search instead of scanning every element:
If the value is not found, binarySearch returns a negative number. The array must already be sorted before you call this method — calling it on an unsorted array gives unpredictable results.
Arrays.binarySearch() on an unsorted array and getting a wrong or negative result. If you are unsure whether the array is sorted, call Arrays.sort() first.
Putting It All Together — A Small Example
Summary
Use the classic for loop when you need the index; use the enhanced for-each when you only need values. The Arrays utility class gives you sort, fill, copyOf, toString, and binarySearch — all optimised and tested, so prefer them over writing your own loops for these tasks. In the next lesson you will step up to multidimensional arrays and see how these same tools apply to rows and columns of data.