Java Tutorial - 26 Enhanced For Loop in Java - StudyViral
Enhanced for Loop in JAVA
Enhanced for loop is used to scan/iterate an Array instead of using traditional loops.
It was introduced in Java 1.5
We can also use it for iterate elements of Collection.
Enhanced is simple to use because it doesn't required condition to control the loop.
But it is inflexible beacause we can't use it to display individual element of array through index. It display all elements of array from first to last.
public class DemoEnchacedLoop { public static void main(String[] args) { String str[] = {"abc","def","ghi","jkl","mno","pqr"}; for(String s : str) { //System.out.println(s); } int ar[] = {1,2,3,4,5,6,7,8,9,10,11}; for(int i : ar) { //System.out.println(i); } int arr[][] = {{22,33,44},{55,66,77},{88,99,10}}; for(int a[]:arr) { for(int a1 : a) { System.out.print(a1+" "); } System.out.println(); } } }
0 comments:
Post a Comment