Sunday, May 25, 2025

The for-each loop and indexing basics

 Congratulations, you’ve made it this far! You’re about to take a huge step in your Java journey and meet the first of many “data structures”. A data structure, simply put, is any way to store or organize data more complex than just one variable—more than just one String, or float, or double, or int, or short, or Boolean. 

This first data structure is called an array. Arrays are simply collections of fixed sizes of like things. You might have an array of ints that has space for 10, or an array of bytes with space for a million, or an array of FluffyUnicorns with space for 57. 

I don’t know—nor do I really care—what your array contains. All I know is this:

  • You gave it a name when you created it
  • You defined the maximum number of things it can hold when you created it
  • You defined the type of thing it can hold, and it will only ever hold that type of thing inside it
  • All your integers or booleans or FluffyUnicorns or whatever, exist in one contiguous block of memory—all together, one right after the other. 

The declaration of an array looks like this

type[] name = new type[size]; 

More concretely, 

int[] nums = new int[100];

This line means “Give me an array of integers, called nums, which has space for 100 integers. Initially, they should all be 0.”

Similarly,

FluffyUnicorn[] fluffies = new FluffyUnicorn[20]; 

defines an array of FluffyUnicorn objects—whatever that means—and names it fluffies, and declares that fluffies can hold at most 20 unicorns.

This is precisely the construction you’ve been seeing and taking for granted in the signature of the main method:

public static void (this we’ll ignore for now) main (String[] args) : means main gets passed in an array of Strings named args!

Arrays may be empty, that is, they may have nothing inside them. If that’s the case, then they’ll have their default value populated in each cell: the representation of 0 (or 0.0) for the numeric types, false for booleans, and null for objects like FluffyUnicorns. I’ve been deliberately vague about objects and nulls for now, but we’ll get to them in a few articles. 

Arrays give addresses to each object inside of them. Unlike humans, who start counting at 1, arrays (and a lot of things in computer science—not just Java!) start counting at 0. Be careful! A lot of beginner programmers encounter off-by-one errors when they forget that we start at 0, not at 1. This also means that the biggest address (called an “index”) available is the length of the array minus 1. For an array of length 7, the biggest index is 6, and so on.

Let’s imagine I have an array of  Strings like so: String[] friends = new String[3];

There are two ways to populate my 3 friends. 

I can either say:

friends = {“Bob”, “Alice”, “Charlie”};


or I can say:

friends[0] = “Bob”;

friends[1] = “Alice”;

friends[2] = “Charlie”;

These two ways of declaring things are exactly equivalent. In either case, friends[0] now has Bob, friends[1] now has Alice, and friends[3] now has Charlie. 

Let’s now say there exists an array of integers called nums, with initial state {0, 2, 4, 5, 7, 9, 11, 12}. 

How would I go about changing, let’s say, the element at index 2 to the value 3, and the element at index 5 to the value 8? By exactly the same procedure as we set friends[0] and so on. 

The changes would happen with:

nums[2] = 3; 

nums[5] = 8;

That means “go into nums at position 2 and put into that box the value 3” and “go into nums at position 5 and put into that box the value 8.” Whatever else may have been in nums[2] or nums[5] will be overwritten. 

Let’s say now I want to update each number by the same rule. \

I could use the for-loop we know (or any while-loop) and say something like:

for(int i = 0; i < nums.length; i++){

         nums[i] = nums[i] + 1;

}


and this would successfully increase every value by 1. 

But in Java, for operating on collections of things, like arrays, by the same rule every time, we have a special kind of for-loop called the for-each loop. 

The for-loop above and the for-each loop you’re about to see are exactly equivalent:


for(int num: nums){

       num++;

}


Reading this out in English, you should say “for each int num in nums, …”


Notice here that since we’re doing the same thing to each element of nums, we don’t care about the index. For-each loops are especially designed to operate on groups of things, like arrays and other structures we’ll learn about in future articles. 


No comments:

Post a Comment

Switch

 Other than if/if-else/if-else if-else and the ternary operator, there is yet another common and important conditional expression in Java th...