Java Program to Print an Array

Java Program to Print an Array

We can iterate array in two ways

Tradition for-loop

package javaapplication1;

 

public class JavaStringArrayExample {

 

 public static void main(String args[]) {

 

  String[] techLang= { "Java", "Android", "Kotlin", "Flutter" };

 

  // iterate all the elements of the array

  int size = techLang.length;

  System.out.println("The size of array is: " + size);

 

  for (int i = 0; i < size; i++) {

   System.out.println("Index[" + i + "] = " + techLang[i]);

  }

 }

}

 

Output

The size of array is: 4
Index[0] = Java
Index[1] = Android
Index[2] = Kotlin
Index[3] = Flutter

 

Enhanced for-each-loop

package javaapplication1;

 

public class JavaStringArrayExample {

 

 public static void main(String args[]) {

 

  String[] techLang= { "Java", "Android", "Kotlin", "Flutter" };

 

  // iterate all the elements of the array

  int size = techLang.length;

  System.out.println("The size of array is: " + size);

 

  for (String str : techLang) {

   System.out.println("Index[" + i + "] = " + str );

  }

 }

}

Output

The size of array is: 4
Index[0] = Java
Index[1] = Android
Index[2] = Kotlin
Index[3] = Flutter