Today, we learned about:
- Enhanced for loops
These allow you to traverse an array without using an increment variable.
For example:
for (int i : myIntArr) {
System.out.println(i); //prints out each int in the array in order
}
this would be equivalent to:
for (int i = 0; i < myIntArr.length, i++) {
System.out.println(myIntArr[i]);
}
note that in the first example, "i" is set to the actual value at that index in the array, while in the second example using a normal for loop i is the index at which an int is stored.
The enhanced for loop only gets data, so you can't use it to set elements in an array. Nor can you traverse the array in any other order.
We then practiced using enhanced for loops in some practice problems.
Here's the homework: https://docs.google.com/forms/d/e/1FAIpQLSfngsy3FoTryUv8K1QysFH15t3IaGX808KxP6lTUQO0rd1-uA/viewform?usp=sf_link