Java provides multiple mechanisms for array iteration. In the last lesson we saw that it is quite cumbersome to manipulate each element individually. Iteration mechanisms are much simpler to use and can be quite useful not only in terms of runtime efficiency but also for increasing maintainability of code.
Ways of Iterating over Java Arrays
To iterate over a Java array you can use –
- The traditional for loop
- The for-each mechanism introduced in Java 5 (Tiger)
- The while loop
- The do-while loop
We have already covered all these loops in a generic context. If you have followed the above lessons, you should have absolutely no trouble in applying these loops to Java arrays.
Java Array Iteration Example
Code below demonstrates usage of all of these loops –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package com.codingraptor; public class Arrays3 { public static void main(String[] args) { // initialize the elements of array // no need to declare the length explicitly String[] website = { "Coding", // website[0] "Raptor", // website[1] ".com ", // website[2] "is ", // website[3] "great", // website[4] "." // website[5] }; // the traditional for loop for (int index = 0; index < website.length; index++) { System.out.println("From traditional for loop: At location " + index + " value is: " + website[index]); } // the read only for-each loop for (String aString : website) { System.out.println("From for-each loop: " + aString); } // the traditional while loop int index = 0; while(index < website.length) { System.out.println("From while loop: " + website[index]); // index++ can be put alongside print statement // but this gives extra clarity index++; } // and the do-while loop index = 0; //reset the point where we begin iterating do { System.out.println("From do-while loop: " + website[index]); index++; } while(index < website.length); } } |
The output of the above program is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | From traditional for loop: At location 0 value is: Coding From traditional for loop: At location 1 value is: Raptor From traditional for loop: At location 2 value is: .com From traditional for loop: At location 3 value is: is From traditional for loop: At location 4 value is: great From traditional for loop: At location 5 value is: . From for-each loop: Coding From for-each loop: Raptor From for-each loop: .com From for-each loop: is From for-each loop: great From for-each loop: . From while loop: Coding From while loop: Raptor From while loop: .com From while loop: is From while loop: great From while loop: . From do-while loop: Coding From do-while loop: Raptor From do-while loop: .com From do-while loop: is From do-while loop: great From do-while loop: . |
Conluding Java Arrays
We have discussed Java arrays in five lessons – What are Java Arrays, Declaration and Initialization of Java Arrays, Multidimensional Arrays in Java, Manipulating individual elements of array and this lesson on iteration.
The reason for being so thorough with Arrays is not because Arrays are a popular data structure. In fact, apart from the String[] in the main method you might never encounter any arrays. Using Java arrays is not encouraged because Java has another data structure called ArrayList which has the same runtime characteristic and does not suffer from size restrictions.
The main reason for being thorough with arrays is that a surprising chunk of job interview questions are based on arrays. This is the simplest data structure and everyone should be able to solve proramming problems on arrays. If somebody doesn’t they are likely to get weeded out of the process pretty soon. Arrays are therefore a good filtering mechanism. They are especially effective in Java because arrays in Java are not so popular. An average Java programmer does not deal with arrays on a day to day basis. So, discussing Java arrays has proven to be very effective at spotting unprepared (and probably non-serious) job candidates. If somebody claims to be a Java programmer, they need to back it up with a sound theoretical knowledge and practical applications of concepts.