Home
Subjects
Textbook solutions
Create
Study sets, textbooks, questions
Log in
Sign up
Upgrade to remove ads
Only $35.99/year
Iterating over collections
STUDY
Flashcards
Learn
Write
Spell
Test
PLAY
Match
Gravity
Terms in this set (22)
What is a while loop?
indefinite (unbounded) iteration
for when you can't predict how many loops are needed in advance
can be infinite e.g. website request for a page
What is a while loop like?
need more flexibility than a for-each loop gives (repeats for every object in a collection)
uses a boolean condition to decide whether or not to keep iterating- need to be careful the condition is not always met
not tied to collections
What is the format for a while loop?
while(loop condition){loop body}
What are some differences between for-each & while loops?
for each is shorter & clearer, while loop is easier to make logic errors with when writing (accidentally infinite), while loop is more flexible (can easily change index increments (do not have to process whole collection))
What are some elements of the while loop?
must:
declare an index variable
express the condition correctly
fetch each element
increment index variable explicitly (more flexibility when iterating over a collection in an unusual way)
What package is the String class defined in?
String class is defined in the java.lang package (implicitly imported)
What can the compiler do to Strings?
compiler can merge identical String literals in the program code- confusing if 2 strings contain 'abc' but then 1 string is changed to 'hi'
Side note: can't be done to String objects outside of the code e.g. from user input
What does strings are immutable mean?
strings are immutable- no methods change the string value of an object- creates a new object assigned to the variable instead- must use assignment to keep change stored
What should you use to determine if strings are equal?
use the .equals() method (checks equality) instead of == (checks identity)
What does == do for String objects?
checks variables point to the same object- same identity
What method do collections have?
iterator(method)- returns an iterator object (matches collection type)
What methods do iterator objects have?
has methods:
boolean hasNext()
E next()
void remove()
What does hasNext() do?
tells if the collection still has a next element, returns boolean
What does next() do?
iterator returns reference to the element it was pointing to (stored in local variable) & iterator points to the next element
What does myList.iterator() do?
call iterator method
gives iterator object- initialised to point to the 1st element
what type of method is iterator?
both an accessor & mutator
What does Element e = iterator.next() do?
stores the reference to the element in the local variable e
When does the iterator end?
when hasNext() returns false
When should use use an iterator object?
to stop part way through a collection
when using indexes are inefficient or impossible
to remove from a collection
How do you remove from a collection using an iterator?
it.remove();
remove method is called on the iterator, not the collection
iterator knows the object has been removes & updates accordingly
Can you use both an iterator & an index?
yes
How do you chain method calls?
each method in the chain is called on the object returned from the previous method call in the chain
evaluated left to right
object returned is the parameter for the next method call
Sets found in the same folder
4 Recursion
21 terms
Building GUIs, Part 1
55 terms
Classes & objects
20 terms
9 Working in Teams
13 terms
Other sets by this creator
10 Sorting Algorithms
21 terms
9 Search Tree Structures
10 terms
8 Maps, Hash Tables & Dictionaries
49 terms
7: Priority Queues & Heaps
44 terms