iterate array using for loop in java code example

Example 1: loop through array java

public class HelloWorld {
  public static void main(String[] args) {
    
    // init array
    String[] family = new String[] {"Member1", "Member2", "Member3", "Member4", "Member5"};
    
    // print array 
    for(String name : family)
    {
    	System.out.println(name);
    }
    
 
  }
}

Example 2: java loop through array

class LoopThroughArray {
	public static void main(String[] args)
	{

		int[] myArr = {1, 2, 3, 4};

		for(int i : myArr){

			System.out.println(i + "\n")

		}

		/* Output:
		1
		2
		3
		4
		*/

	}
}