Posts

Showing posts with the label How to iterate through Java List

How to iterate over Array in Java?Six (6) ways to loop over an Array in Java

Iteration is a technique used to sequence through a block of code perpetually until a concrete condition either subsist or no longer subsists. Iterations are a very prevalent approach utilized with loops. In this article, we will show you  Six (6) ways to loop over an Array in Java. 1.  Using  Simple For loop  2.  Using  Enhanced For loop  3.  Using  While loop 4.  Using  Do - while loop 5. Using Stream.of() and forEach 6. Using Arrays.stream() and forEach Example 1: Using Simple For loop     // create an array String [] users = { "alpha" , "beta" , "giga" , "gama" , "tesla" }; // Simple For loop for ( int i = 0 ; i < users.length; i++) { System.out.println(users[i]); } Example 2:  Using Enhanced For loop  The enhanced for loop is introduced since J2SE 5.0. It is used to traverse the array or collection elements.  ...