• Develop code used to traverse the elements in a 1D array and determine the result of these traversals

Assignment


Traversal with for Loops

Loops are good for repeating things. Traversing an array is an example of a repeated action. Loops are good for traversing arrays.

1
2
3
4
5
String[] names = {"Jamal", "Emily", "Destiny", "Mateo", "Sofia"};

for (int i = 0; i < names.length; i++) {
    System.out.println(names[i]);
}

The text has a slew of example and exercises that show different ways to mess with this basic structure, but the basic idea doesn’t change much.

The Enhanced for Loop

If you want to type even less, you can use an enhanced for loop.

1
2
3
4
5
String[] names = {"Jamal", "Emily", "Destiny", "Mateo", "Sofia"};

for (String name : names) {
    System.out.println(name);
}

This shorthand version will go through each element and grant you read-only access to the values. That’s the trade-off. And that it has to process each element. No starting somewhere in the middle.

So, this does have drawbacks, but it is technically quicker to write, and more importantly, less prone to errors. Use what you want, but you need to be able to read and write the enhanced version.