Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Need a clarification in the "for" loop

Reply
Thread Tools

Need a clarification in the "for" loop

 
 
Praveen
Guest
Posts: n/a
 
      01-21-2008

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?
 
Reply With Quote
 
 
 
 
Jack
Guest
Posts: n/a
 
      01-21-2008
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 :
> 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?

 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      01-21-2008
Jack <(E-Mail Removed)> wrote:
>> 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"
 
Reply With Quote
 
Praveen
Guest
Posts: n/a
 
      01-21-2008
Thanks got it!!

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-21-2008
(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.

>> If so can anyone explain me what does it do?


Jack wrote:
> 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.
<http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2>
> 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.

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-21-2008
On Mon, 21 Jan 2008 01:19:42 -0800 (PST), Praveen
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>for (String x:a, String y:b) {


I think that was not written in Java.
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
need clarification with import statements Tool69 Python 2 12-15-2006 11:56 AM
Need clarification on 'typedef' keyword Krishna C++ 5 04-01-2005 05:21 PM
Need clarification on asp_wp.exe,aspnet_wp.exe and aspnet_isapi.dll prem ASP .Net 1 11-03-2004 05:32 AM
Need some clarification and/or help with java NIO sockets HDSkiFreak Java 2 05-11-2004 01:48 PM



Advertisments