loop array java code example
Example 1: lopp array java
For(<DataType of array/List><Temp variable name> : <Array/List to be iterated>){
System.out.println();
}
Example 2: loop through array java
public class HelloWorld {
public static void main(String[] args) {
String[] family = new String[] {"Member1", "Member2", "Member3", "Member4", "Member5"};
for(String name : family)
{
System.out.println(name);
}
}
}
Example 3: loop through array java
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
List<String> family = new ArrayList();
family.add("Member1");
family.add("Member2");
family.add("Member3");
family.add("Member4");
family.add("Member5");
for(String name : family)
{
System.out.println(name);
}
}
}
Example 4: how to use for loop for array in java
import java.io.*;
class GFG {
public static void main(String args[]) throws IOException
{
int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int i, x;
for (i = 0; i < ar.length; i++) {
x = ar[i];
System.out.print(x + " ");
}
}
}
Example 5: 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")
}
}
}
Example 6: java loop through array
String[] elements = {"a", "a", "a", "a"};
for (String s: elements) {
System.out.println(s);
}