Need a clarification in the "for" loop

P

Praveen

Hi,

I came across the following piece of code in java

String []a;
String []b;

for (String x:a, String y:b) {
// do something

}

Is the above piece of code correct?
If so can anyone explain me what does it do?
 
J

Jack

Hi,

can't you try by yourself ? Juste oaste the code in any java file!!

Eclispe reject your code, so seems it's incorrect (with jdk1.5).


Praveen a écrit :
 
A

Andreas Leitgeb

Jack said:
I came across the following piece of code in java
String []a; String []b;
for (String x:a, String y:b) { /* do something */ }
Is the above piece of code correct?
can't you try by yourself ? Just paste the code in any java file!!
Eclipse reject your code, so seems it's incorrect (with jdk1.5).

Just a sidenote: Perhaps someone mixed up tcl with java here,
since tcl's foreach-loop does support parallel iteration of
multiple lists.

Perhaps the code was actually a sample for a "wishlist item" :)
 
L

Lew

(Top-posting corrected. Please do not top-post.)

Praveen a écrit :
I came across the following piece of code in java

String []a;
String []b;

for (String x:a, String y:b) {
// do something

}

Is the above piece of code correct?

No, as others have mentioned.
can't you try by yourself ? Juste oaste the code in any java file!!

Eclispe reject your code, so seems it's incorrect (with jdk1.5).

One could always check out the JLS, since we know Eclipse can and does report
as errors things that are actually legal Java.
The enhanced for statement has the form:

EnhancedForStatement:
for ( VariableModifiersopt Type Identifier: Expression) Statement

QED.

Language syntax questions can pretty much always be resolved by reference to
the JLS. Library questions can often be resolved by reference to the Javadocs.

Faster, more completely and more reliably than by Usenet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top