loop throw array Java code example

Example 1: loop through array java

import java.util.*;

public class HelloWorld {
  public static void main(String[] args) {
    
    // you can define the type of list you want to init - String int etc inside <yourType> eg. <String>
    List<String> family = new ArrayList();
    
    family.add("Member1");
    family.add("Member2");
    family.add("Member3");
    family.add("Member4");
    family.add("Member5");
    
    // print array 
    for(String name : family)
    {
    	System.out.println(name);
    }
    
 
  }
}

Example 2: for loop in java as long as array

for(i = 0; i < arrayName.length; i++{
  	//print statement will output current index's value
 	System.out.println(arrayName[i]); 
}

Tags:

Java Example