Tuesday 19 July 2011

look every thing is ok with older for loop ... but today everything is changing ....
so what is new in new for loop ...

now you are engineer .... so please call the new for loop as  " for   each " loop .

so what is good in this new for loop .
so start ... 1 . are you bored with increment thing in for loop  .
                2 . are you happy with size declaration .
                3 . are you happy with initialization .

if you are happy than stay and remain in older state  . if not than join me ... i will show you some goodness of java ...

for each loop presentation ...

for ( int x : array_ name )
 {
   ------------------
   ------------------
}

1.  question in your mind ... is it ok ... than you will try and it will perform correctly ...
2.  question in your mind ... its tough for me  ... and how this for loop finds the size ...
3.  and how this loop is incrementing ...

for every answer just open Iterate class .. there is a iterator mehtod ...

this method is used to incrementing ... and how to find the next value ....for this there is another method
hasNext() by the help of this method you can find the next element ... but how it initialize ....

for solving this ... the syntax play a important role ... ( int  x : array_name )  here "int x" the type with variable is necessary ....
so what this type do .... in this for each loop ....
we just find the next value and just copy it into " x " so after it we can easily use it ...

but in this world for every goodness there is a badness ... and this badness is also here .. so 

read carefully.... generally it works on original array 
                int [] arr = { 1 , 2 , 3 };
for( int x : arr )
{
System.out.println(x);
x++;
}
for( int y : arr )
{
System.out.println(y);
}
the output : 1,2,3;
                   1,2,3;

so you can call it also as a read only loop


No comments: