Iterating an Array Using the for Statement [5.0]

The for statement can be used to conveniently iterate over the elements of an array. The general syntax of the array-based for statement is:
for (type variable : array) { body-code }
The array-based for statement has four parts. The array is an expression that returns an array. The type specifies the type of variable which will be used to hold elements from the array. In particular, variable always holds the current element of the iteration and can be used by the code in body-code. body-code is code that will be executed once for every element in the array. Here is an example of the for statement.
// Returns the smallest integer in the supplied array public int getMin(int[] ints) { int min = Integer.MAX_VALUE; for (int num : ints) { if (num < min) { min = num; } } return min; }

Comments

22 Jan 2010 - 5:25am by Grace (not verified)

hi..im new in programming...can we use arrays in an if-else statement...
im trying this: if(xyz[0][0] == xyz[1][1] == xyz[2][2]) ..... it says incompatible types :( pls help me..thanks :D

23 Jan 2010 - 9:00am by alex.from.ohio.java@gmail.com (not verified)

You have syntax error. What is
xyz[0][0] == xyz[1][1] == xyz[2][2] ?

Do you mean this ?
(xyz[0][0] == xyz[1][1]) && (xyz[1][1] == xyz[2][2])

22 Feb 2010 - 1:43pm by Anonymous (not verified)

or this ? (xyz[0][0] == xyz[1][1]) && (xyz[1][1] == xyz[2][2]) && (xyz[0][0] == xyz[2][2])

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.