Chapter 4 - Grouping objects Flashcards
2 points on its usage are:
* you may not remove or add elements while this is iterating a collection. however, we may change the state of an object
* the proper use of this is always with definite iteration. in general we should not be breaking out of this early
give to points to keep in mind when using a for-each loop
this is used to add elements to the ArrayList
describe the ArrayList method
.add(object)
these include:
1. hasNext()
2. next()
3. remove()
name 3 methods that an iterator will hold
this is an object that stores a grouping of object references
what is a
collection
this exception may be seen when we try and modify a collection while it is being iterated. such as when using a for-each loop
when might we see the fllowing exception
ConcurrentModificationException
when might we see the exception
NullPointerException
this exception might be seen if we tried to call a method on a variable that has not been initialised with an object
when this is not initialised the default value it will be given is null
if we declare an object field but do not initialise it within the constructor what value will it be given by default
- this is a reserved java keyword
- it essentially means: no object or empty
describe the keyword
null
describe the string method
concat()
this method appends (concatenate) a string to the end of another string.
how can we start using an
**iterator object **
in our program
to begin using this we must import the iterator class that is part of the java standard library using:
Import java.util.Iterator;
declarationIterator<ElementTypeInCollection> it;
initialisationit = collection.iterator();
equivalentIterator<ElementTypeInCollection> it = collection.iterator();
give psuedocode that
declares and initialise an iterator object
what is a
collection
this is an object that stores a grouping of object references
if we declare an object field but do not initialise it within the constructor what value will it be given by default
when this is not initialised the default value it will be given is null
what will the
iterator() methodof collections return
this will retuen an iterator object for that collection
this is the combination of a generic type and a generic type parameter to create a new type
what is a
specific type
how is the
diamond notation
used
this will either:
* hold a generic type parameter
* or be left empty if we have already declared the type parameter
what is a
generic class also referred to as
this is also referred to as a
parameterized type
because it receives a type as a parameter when it is declared.
the syntax for this is:
do { // code block to be executed} while (condition);
what is the syntax for the
do-while loop
what are
generic type parameters
Atypegiven betweenangle bracketsto make ageneric typespecific.
how does the
for-each loop get its name
this gets its name from the way it is read:
For each element in collection do{ loop body }
what code can be used as validation which
ensures we do not recieve an IndexOutOfBoundsException
what does the following validation code ensure:
If(index >=0 && index < collection.size()) { We have a valid index }
- this involves an indefinite number of iterations
- this implies that a while loop would be better suited than a for-each loop
give two properties of searching a collection
when using an iterator do we require our own index value
when iterating a collection using this it will itself keep track of where it is and so using our own index is unnecessary