how to loop through an array in 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: 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 5: java loop through array
String[] elements = {"a", "a", "a", "a"};
for (String s: elements) {
System.out.println(s);
}
Example 6: 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));
}
}