cars
array, using a “for-each” loop.
The example can be read like this: for each String
element (called i
- as in index) in cars
, print out the value of i
.
If you compare the for
loop and for-each loop, you will see that the for-each method is easier to write, it does not require a counter (using the length
property), and it is more readable.
|
myNumbers
is now an array with two arrays as its elements.
To access the elements of the myNumbers
array, specify two indexes: one for the array, and one for the element inside that array.
This example accesses the third element (2) in the second array (1) of myNumbers
:
|
for
loop inside another for
loop to get the elements of a two-dimensional array (we still have to point to the two indexes):
|