Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Question about "Enhanced for loop"

807607Nov 11 2006 — edited Nov 11 2006
public class NewLoopTest{
 	public NewLoopTest(){ 
		int result=0; 		 		
 		int[] a=new int[20]; 		
 		for(int i=0;i<a.length;i++){
 			a=i++;
}
for(int i:a){
System.out.println("i="+i+";"+"a["+i+"]="+a[i]+";result="+result+"+"+i+"="+(result+i));
result+=i;
}
System.out.println("-------------");
result=0;
for(int i=0;i<a.length;i++){
System.out.println("i="+i+";"+"a["+i+"]="+a[i]+";result="+result+"+"+i+"="+(result+i));
result+=i;
}
}
}

This code counts sum of the elements of a array.
At first I use the enhanced for loop and at second I use the traditional for.
Enhanced for loop in sdk1.5 returns only even elements of array, am I right?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 9 2006
Added on Nov 11 2006
1 comment
130 views