Tina Comston Comp-111 Week 10 Franklin University |
||||
Week 10 | ||||
Preparation
|
||||
Guided
Learning Activity |
||||
Assignment
Enhanced FOR loop
When looping through an array or ArrayList using the FOR loop you have to:
The format is: for (dataType objName: ArrayListName) { System.out.println(objName);
}What this special type of FOR loop does it access each element in the ArrayList. When it accesses the element, it puts it into the objName variable. Then within the loop you access that objName variable. Ok - so let's compare the regular FOR loop with the enhanced FOR loop. for (int i = 0, i < stuName.size(); i++) { System.out.println(stuName.get(i)); } for (String tmpName: stuName) { System.out.println(tmpName) } In the enhanced For loop, a variable is defined of the same data type as the ArrayList. As each element in the ArrayList is accessed the element is put into the variable. Within the loop this variable is then referenced. YouTube video on enhanced for loops https://www.youtube.com/watch?v=aKzlcqFAoNc
Two-Dimensional Arrays Up till now, the focus has been on one dimensional arrays and ArrayLists. Java also supports multi-dimensional arrays. When specifying a multi-dimensional array you must specify both the columns and the rows. int newArray[][] = new int[row][column]; You would access the two dimensional array specifying both the row and the column. Often nested loops are used: for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) }{ System.out.println(newArray[i][j]); } In the above example, the array elements in row 0 will be processed, looping through all of the columns, then row 1 looping through all the columns, then row 2 and so on. YouTube video on 2D and 3D arrays https://www.youtube.com/watch?v=1q3brvLgYbs
|
||||
Exam 2
|
||||
Lab 4
|