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])

14 Apr 2010 - 7:15pm by Anonymous (not verified)

You are lucky the compiler caught this. Think about what happens if xyz[][] is a Boolean. no compiler error.
I recommend you review this till you understand why you are seeing the error. Think about what happens if you type
if ( x==y==z) {...}

is it any different to

if ((x==y)==z) {....}

Rule 42: Simplify your if's or you get but's!!!

Python is the only language I know that can do x==y==z (or x==y==z etc) the same as mathmaticians.

26 Apr 2010 - 1:48pm by Iterating an Array Using the while Statement (not verified)

import java.util.ArrayList;
import java.util.Iterator;

public class Library
{
private ArrayList items;
public Library()
{
items = new ArrayList();
}

public void removeItem(int catalogId)
{
Iterator it = items.iterator();
while(it.hasNext())
{
LibraryItem item = it.next();
if (item.getCatalogId() == catalogId){
it.remove();
}
}
}
}

18 May 2010 - 5:01am by Anonymous (not verified)

(a == b) is a boolean, true or false, so you cannot write : a == b == c unless c is a boolean.

In your case maybe xyz[0][0] contains an integer or a float or anything else, so you cannot write xyz[0][0] == true, they are incompatible types.

you have to write (xyz[0][0]==xyz[1][1] && xyz[0][0]==xyz[2][2])
They are two comparisons of objects of the same type, resulting in booleans, and then you can use '&&' between those booleans.

That's an example of how the compiler does execute your code :
1) xyz[0][0]==xyz[1][1] ---> true
2) xyz[0][0]==xyz[2][2] ---> false
3) true && false ---> false

23 Jan 2012 - 9:53pm by Ch Narendra (not verified)

How the threads will be allocated when 2 jvm's run in one system

29 Mar 2012 - 10:49am by sandeep (not verified)

Hi java fellows,I need one solution for the problem.
I am working on java mvc project using ibm rad software and jsp.
The problem is:
I have to retrieve two column datas from db. The one column name is level and another is unit.
For example the data's in table may look like this
Level unit
1 402
1 403
2 404
2 406
3 407
3 408

Here you have to check max level and number of units.
For the above the column the data should be displayed like below on browserm
3-407 3-408
2-404 2-406
1-402 1-403
I want display like above.whereever there is no data blank space should be displayed else the data whatever retrieved from db has to be displayed.so how to find the missed spaces.
Actually I have used hashmap to find the number of levels and units.here I got stuck.so please whoever reading this article,help me.thanks in advance...

29 Mar 2012 - 10:59am by sandeep (not verified)

I have mentioned spaces comes here where ever the datas are not there.the db is retrieving only 6 values but I want to show the result in a table format which has 3 rows and 3 columns.
For example the data's in table may look like this
Level unit
1 402
1 403
2 404
2 406
3 407
3 408
Here you have to check max level and number of units.
For the above the column the data should be displayed like below on browserm
3-407 3-408 here space comes
2-404 here space comes 2-406
Here space comes 1-402 1-403
I want display like above.whereever there is no data blank space should be displayed else the data whatever retrieved from db has to be displayed.so how to find the missed spaces.
Actually I have used hashmap to find the number of levels and units.here I got stuck.so please whoever reading this article,help me.thanks in advance...

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.