pTutorial

New way to learn Programming

Java array programming questions

Java For each loop introduce in java 1.5. It is used to access the array and collection objects. It’s reduce the complicity of the iteration.

In for each loop there is no need to any condition at all so that its more readable the for loop.

Java print array using for each loop

// Access java array through for each loop
import java.util.Scanner;
public class foreach {
    public static void main(String...s){
        int x[]=new int[3];	
        Scanner uk=new Scanner(System.in);
        System.out.println("Enter 3 numbers");
        for(int l=0;l<x.length;l++){
            x[l]= uk.nextInt();
        }
        System.out.println("Result of for each loop");
        for(int h:x){
           System.out.println(h);
        }
    }
}




Output

Enter 3 numbers
38
7
35
Result of for each loop
38
7
35

See Also


QUOTES OF THE DAY

Wonder is the first cause of philosophy.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER