how to loop array in java code example
Example 1: 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 2: 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 3: for loop in java as long as array
for(i = 0; i < arrayName.length; i++{
System.out.println(arrayName[i]);
}
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 array loop
public class Test {
public static void main(String args[]) {
String string = args[0];
System.out.println("last character: " +
string.substring(string.length() - 1));
}
}