120 likes | 139 Views
Enhanced For Loops (For Each Loops). Less Code and Less Overhead for Writing For Loops. Enhanced For Loops. Enhanced For Loops make it easier to access items in an array or ArrayList or some other kind of list, but there are some limitations.
E N D
Enhanced For Loops(For Each Loops) Less Code and Less Overhead for Writing For Loops
Enhanced For Loops Enhanced For Loops make it easier to access items in an array or ArrayList or some other kind of list, but there are some limitations. Enhanced For Loops are also called “For Each” loops. This nickname has come about because of what this type of for loop does. It automatically gets each element in the list and stores it in the loop control variable so that it can be processed. The data type of the loop control variable is the same as the kind of data in the list (array or ArrayList).
Limitations of Enhanced For Loops The main limitation of Enhanced For Loops is that you cannot make changes to the elements inside the list. You can only access the elements in the list, but you can take them as you get them out and perform other operations. Specifically you cannot …
Limitations of Enhanced For Loops You cannot … • Move through an array in reverse, from the last position to the first position (a countdown loop that accesses indexes could do this) • Assign elements to particular positions in an array • Track the index position of the current element in an array (unless you use an extra counter variable) • Access any element other than the current element on each iteration of the loop
General Form of an Enhanced For Loop Here is the general form of an enhanced for loop where there are a list of values. for (type of values in list loop control variable : actual name of list in program) { ……… ……… } If the type of values in the list is represented by E, then we could write …. for (E element : list) { ……… ……… } where element is of type E and list is the name of the list in the program.
Enhanced For Loop for an array of ints int [ ] nums = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}; If we want to code an enhanced for loop that will print only the multiples of 10 in nums, then we need a loop control variable of type int, because there are int values in the array. for (int element : nums ) { if (element % 10 == 0) System.out.print( element + “ ”); } Compare this code to standard for loop code: for (int i = 0; i < nums.length; i++) { int element = nums [i]; if (element % 10 == 0) System.out.print( element + “ ”); } Everything to the left in blue is extra code you have to write!
Enhanced For Loop for an array of doubles double [ ] averages = {88.3, 75.4, 67.8, 93.5, 98.1, 82.7, 79.6, 73.4, 86.7}; If we want to code an enhanced for loop that will print only the values in averages that are greater than or equal to 80, then we need a loop control variable of type double, because there are double values in the array. for (double element : averages ) { if (element >= 80) System.out.print( element + “ ”); } Compare this code to standard for loop code: for (int i = 0; i < averages.length; i++) { double element = averages[i]; if (element >= 80) System.out.print( element + “ ”); } Everything to the left in blue is extra code you have to write!
Enhanced For Loop for an array of Objects String [ ] names = {“Obama”, “Bush”, “Clinton”, “Reagan”, “Carter”, “Ford” }; If we want to code an enhanced for loop that will print only the names that begin with a “C”, then we need a loop control variable of type String, because there are String values in the array. for (String element : names) { if (element.substring(0,1).equals(“C”)) System.out.print( element + “ ”); } Compare this code to standard for loop code: for (int i = 0; i < names.length; i++) { String element = names[i]; if (element .substring(0,1).equals(“C”)) System.out.print( element + “ ”); } Everything to the left in blue is extra code you have to write!
ArrayList <String> names = new ArrayList <String> (); // Code that places Obama, Bush, Clinton, Reagan, Carter, Ford, Nixon in list. If we want to code an enhanced for loop that will print only the names that begin with a “C”, then we need a loop control variable of type String, because there are String values in the array. for (String element : names) { if (element.substring(0,1).equals(“C”)) System.out.print( element + “ ”); } Compare this code to standard for loop code: for (int i = 0; i < names.length; i++) { String element = names[i]; if (element.substring(0,1).equals(“C”)) System.out.print( element + “ ”); } Enhanced For Loop for an ArrayList Note the code is not any different for an ArrayList! Everything to the left in blue is extra code you have to write!
ArrayList <Student> students = new ArrayList <Student> (); // Code that places students in list. If we want to code an enhanced for loop that will print the name and GPA of all students, then we need a loop control variable of type Student, because there are Student values in the ArrayList. for (Student element : students) { System.out.print( element.getName() + element.getGPA()); } Compare this code to standard for loop code: for (int i = 0; i < students.length; i++) { Student element = students[i]; System.out.print( element.getName() + element.getGPA()); } Enhanced For Loop for an ArrayList Everything to the left in blue is extra code you have to write!
Nested Enhanced For Loops for a 2D Array Here is an example of how to use nested enhanced for loops with a two-dimensional array of integers. // Sum the elements in a two-dimensional array int [ ] [ ] table = { {2, 3, 4} ,{2, 3, 4} , {2, 3, 4} }; int sum = 0; for (int [ ] row : table) for (intelement : row ) sum += element ; System.out.println(Sum: " + sum); }
Nested Enhanced For Loops for a 2D Array Here is an example of how to use an enhanced for loop with a two-dimensional array of Strings values. // concatentate all the string values in table into one string String [ ][ ] table = { {“A”, “B”, “C”}, {“D”, “E”, “F”} } String str = “”; for (String [ ] row : table) for (String element : row ) str +=element; }