Docsity

CSIS 110 Lecture 13: While Loops and ArrayList Manipulation - Prof. Brian F. Hanks, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 - Lecture 13
Announcements
Program due Wednesday.
Quiz next Monday.
Next Program?
Last time: foreach loop
Use classroom presenter to ask students to write for each loops for some ArrayLists.
Limitations of foreach loop:
- Can only go through collection in one direction
- Can't modify the collection inside the loop, because you don't have an index
- Have to go through every element in the collection โ€“ not easy to stop in the
middle.
While loop
Java provides another variety of loop statement โ€“ the while statement.
while ( boolean-expr ) {
statements;
}
The while statement works this way:
1. Evaluate the boolean expression. If it is true go to step 2. Otherwise, go to step 4.
2. Execute the statements in the while-loop body.
3. Go to step 1
4. Execute the statement following the while loop.
[Draw a flow chart]
Add a listAllNotes method to the notebook project โ€“ it needs this while loop
int index = 0;
while (index < notes.size() ) {
String nextNote = notes.get( index );
System.out.println( nextNote );
index++;
}
Discuss practical parts of this loop:
pf3

Partial preview of the text

Download CSIS 110 Lecture 13: While Loops and ArrayList Manipulation - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 13

Announcements Program due Wednesday. Quiz next Monday. Next Program? Last time : foreach loop Use classroom presenter to ask students to write for each loops for some ArrayLists. Limitations of foreach loop:

  • Can only go through collection in one direction
  • Can't modify the collection inside the loop, because you don't have an index
  • Have to go through every element in the collection โ€“ not easy to stop in the middle. While loop Java provides another variety of loop statement โ€“ the while statement. while ( boolean-expr ) { statements; } The while statement works this way:
  1. Evaluate the boolean expression. If it is true go to step 2. Otherwise, go to step 4.
  2. Execute the statements in the while-loop body.
  3. Go to step 1
  4. Execute the statement following the while loop. [Draw a flow chart] Add a listAllNotes method to the notebook project โ€“ it needs this while loop int index = 0; while (index < notes.size() ) { String nextNote = notes.get( index ); System.out.println( nextNote ); index++; } Discuss practical parts of this loop:
  1. Preparation for first iteration
  2. While statement w/ boolean expression
  3. Statements that we want to execute repetitively
  4. Preparation for next iteration Compare above while with foreach loop. Compare while loop with if statement โ€“ draw flow charts. Emphasize that if statement executes only once, while statement allows for repetitive actions.
  • Let's modify above to print index along with the note. (contrast with foreach)
  • Let's modify it to print the notes in reverse order. Removing an item from a Collection Weโ€™ve seen how to add and access items in collections - what about removing items? ArrayList includes a remove method: remove the item at a given index from the collection. Again, index must be in the range 0 through size() - 1. So, to remove the first note, we would do this: notes.remove( 0 ); How would we remove the second note in our notebook? How about the last note in the notebook? [Draw a notebook ArrayList with four notes, and then ask what happens after we call remove(1) ] Discuss how the indices of the elements change after we do a remove. Add a remove method to the notebook, and show how it works. Try to write a method that removes all of the elements in a list.