how to traverse an array in java code example
Example 1: 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")
}
/* Output:
1
2
3
4
*/
}
}
Example 2: java loop through array
String[] elements = {"a", "a", "a", "a"};
for (String s: elements) {
//Do your stuff here
System.out.println(s);
}