iterate through 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: iterator for array java
import java.util.Arrays;
import java.util.Iterator;
public class ArrayToIterable {
public static void main(String args[]){
Integer[] myArray = {897, 56, 78, 90, 12, 123, 75};
Iterator<Integer> iterator = Arrays.stream(myArray).iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Example 5: 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 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));
}
}