• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Question abt Enhanced For loop

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some one please give me the answer and reason?


class test
{
public static void main(String[] args)
{
test inst_test = new test();
String pig[][] = { {"one little piggy"}, {"two little piggies"}, {"three little piggies"} };
for ( Object []oink : pig )
{
for ( Object piggy : oink )
{
System.out.println(piggy);
}
}
}
}

a. one little piggy two little piggies three little piggies
b. Compile Error incompatible types.
c. java.lang.String;@187c6c7 java.lang.String;@187c6c8 java.lang.String;@187c6c9
( or something like that )
d. Runtime NullpointerException
e. Prints nothing
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer a is correct, what is your question?
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by priti manas duddilla:
Can some one please give me the answer and reason?



Hi Priti,

First off can you please use code tags (and all you other posters that don't). It makes the code examples *sooooo* much easier to read for the 100,000,000 readers of your post. I'm interested in either learning from or helping out with your posts, but am quickly turned off when tags aren't used, especially on lengthy examples.

Now, why the answer is 'a'?
On the outer for loop, the Object oink array is assigned to the double String array pig, which contains the 3 little pig String elements. The outer loop will iterate what's inside the outer block once for each element (in this case, 3 iterations) in pig.
The inner for loop iterates over the Object oink through the piggy Object. Since the Object oink only has one String object on each iteration, there is only one iteration of the inner for loop.

On the first outer loop iteration, oink is assigned the value "one little piggy".
The inner for loop runs and piggy is assigned the value of oink - "one little piggy", which in turn gets printed.
The inner loop completes at this point.

The second iteration of the outer loop, oink is assigned the value of "two little piggies".
The inner for loop runs and piggy is assigned "two little piggies" from oink and the value is printed. The inner loop completes.

The same happens for "three little piggies".

I hope this helps clear the air.

Aloha,
Doug
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THE answer is "a"
because when you say (object[] oink : pig) the "pig" array will be assign to "oink" and in the second for loop once again object[] oink will assign to object "piggy" which is legal.
thats it!!!
reply
    Bookmark Topic Watch Topic
  • New Topic