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
what are the two outcomes of searching a collection and how might we express this in code
the two outcomes of this are:
* The search succeeds after an indefinte number of iterations
* The search fails after the collection has been exhausted
both must be taken into account during the search and so the code may look as follows
Int index = 0 Boolean searching = True; While(searching && index < file.size())
the syntax for this is
string.concat(string2)
@param string2 the string to append to the first string
what is the syntax for the string method
concat()
this will remove the element it last passed
describe the iterator method
remove()
what two things must be specified when declaring a collection
when declaring this we must specify:
1.The type of the collection (I.e ArrayList)
2.The type of the elements that will be stored in the collection (generic type parameter)
this is an object that allows us to iterate over different types of collections
what is an
iterator
describe
definite iteration
this is when we know beforehand how many iterations will occur.
for-each loops are a perfect application for this type of iteration as we know it will iterate as many times as the size of a collection
when iterating a collection using this it will itself keep track of where it is and so using our own index is unnecessary
when using an iterator do we require our own index value
this can be a more concise way to write code. and invloves a method returning an anonymous object that can be used by the next method call
example:
String A = Method1()
String B = A.Method2()
String C = B.Method3()
String c = method1().method2().method3()
Here each method returns an anonymous object instead of the first method where we used a reference for each returned object
describe
chaining methods
write a method that returns the parameterized type ArrayList<String>
which will hold the first two objects from the collection named library
the code to achieve this is:
public ArrayList<String> firstTwo(){ ArrayList<String> temp = new ArrayList<>(); temp.add(library.get(0)); temp.add(library.get(1)); return temp; }
this gets its name from the way it is read:
~~~
For each element in collection do{
loop body
}
~~~
how does the
for-each loop get its name
when might we see the fllowing exception
ConcurrentModificationException
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
what is the wording if we have the following
1.Collection = ArrayList
2.Objects / Elements it will hold = String
~~~
ArrayList<String>;
~~~</String>
this can be read as
an “ArrayList of String”
this will return the next element of the collection and then move past it
describe the iterator method
next()
what does the following validation code ensure:
If(index >=0 && index < collection.size()) { We have a valid index }
what code can be used as validation which
ensures we do not recieve an IndexOutOfBoundsException
this can be achieved using:
declarationArrayList<String> someString;
initialisationsomeString = new ArrayList<>();
(la) 1. declare anArrayListof some type of element
2. then initialise anArrayListusing the diamond notation
what check can be made to avoid
null errors
to avoid these use a check such as:If (var == null)
(la) 1. declare anArrayListof some type of element
2. then initialise anArrayListusing the diamond notation
this can be achieved using:
declarationArrayList<String> someString;
initialisationsomeString = new ArrayList<>();
why do we not require our own index when using an iterator
this is not required because the iterator is itself aware and keeping track of its position within a collection
describe the
do-while loop
and how it operates
this is a variant of the while loop and operates as follows:
1. It will first execute a block of code (do)
2. Then it will check if a condition is true (while)
3. If the condition is true it will again execute the do block of code
4. This continues until the condition is false
these include:
* .add(object)
* .get(index)
* .remove(index)
* .size()
name 4 methods of the ArrayList
positive
Boolean searching = True; While(searching)
negative
Boolean found = False; While(!found)
both are equivalent but they are expressed differently and is something that must be kept in mind for readability of the problem
give examples of positive and negative boolean expressions
this is considered incomplete because for it to function it must be passed a second type as a parameter
providing a second type will complete this
why is a generic type class considered incomplete and how would we complete it
describe the ArrayList method
.get(index)
this is used to access an element in the ArrayList
what is an important note concerning iterating a collection using an iterator
an important note when performing this is:
that all interaction with the collection is done using the iterator we never directly access the collection
what exception will be shown if we try and
access an index of a collection that is equal to or greater than its size
in this case we will see the exception:
IndexOutOfBoundsException
to avoid these use a check such as:If (var == null)
what check can be made to avoid
null errors
what is
garbage collection
this is a runtime system that reclaims memory from objects that have no references to them (anonymous objects)
describe the keyword
null
- this is a reserved java keyword
- it essentially means: no object or empty