Chapter 4 - Grouping objects Flashcards

1
Q

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

A

give to points to keep in mind when using a for-each loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

this is used to add elements to the ArrayList

A

describe the ArrayList method
.add(object)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

these include:
1. hasNext()
2. next()
3. remove()

A

name 3 methods that an iterator will hold

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

this is an object that stores a grouping of object references

A

what is a
collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

when might we see the fllowing exception
ConcurrentModificationException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

when might we see the exception
NullPointerException

A

this exception might be seen if we tried to call a method on a variable that has not been initialised with an object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

when this is not initialised the default value it will be given is null

A

if we declare an object field but do not initialise it within the constructor what value will it be given by default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. this is a reserved java keyword
  2. it essentially means: no object or empty
A

describe the keyword
null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

describe the string method
concat()

A

this method appends (concatenate) a string to the end of another string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how can we start using an
**iterator object **
in our program

A

to begin using this we must import the iterator class that is part of the java standard library using:

Import java.util.Iterator;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

declaration
Iterator<ElementTypeInCollection> it;

initialisation
it = collection.iterator();

equivalent
Iterator<ElementTypeInCollection> it = collection.iterator();

A

give psuedocode that
declares and initialise an iterator object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
A

ignore

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is a
collection

A

this is an object that stores a grouping of object references

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

if we declare an object field but do not initialise it within the constructor what value will it be given by default

A

when this is not initialised the default value it will be given is null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what will the
iterator() methodof collections return

A

this will retuen an iterator object for that collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

this is the combination of a generic type and a generic type parameter to create a new type

A

what is a
specific type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

how is the
diamond notation
used

A

this will either:
* hold a generic type parameter
* or be left empty if we have already declared the type parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what is a
generic class also referred to as

A

this is also referred to as a
parameterized type
because it receives a type as a parameter when it is declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

the syntax for this is:

do {  
    // code block to be executed}
while (condition);
A

what is the syntax for the
do-while loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

what are
generic type parameters

A

Atypegiven betweenangle bracketsto make ageneric typespecific.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

how does the
for-each loop get its name

A

this gets its name from the way it is read:
~~~
For each element in collection do{
loop body
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

what code can be used as validation which
ensures we do not recieve an IndexOutOfBoundsException

A

what does the following validation code ensure:

If(index >=0 && index < collection.size())
{
    We have a valid index
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
  1. this involves an indefinite number of iterations
  2. this implies that a while loop would be better suited than a for-each loop
A

give two properties of searching a collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

when using an iterator do we require our own index value

A

when iterating a collection using this it will itself keep track of where it is and so using our own index is unnecessary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

what are the two outcomes of searching a collection and how might we express this in code

A

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())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

the syntax for this is
string.concat(string2)

@param string2 the string to append to the first string

A

what is the syntax for the string method
concat()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

this will remove the element it last passed

A

describe the iterator method
remove()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

what two things must be specified when declaring a collection

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

this is an object that allows us to iterate over different types of collections

A

what is an
iterator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

describe
definite iteration

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

when iterating a collection using this it will itself keep track of where it is and so using our own index is unnecessary

A

when using an iterator do we require our own index value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

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

A

describe
chaining methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

write a method that returns the parameterized type ArrayList<String>
which will hold the first two objects from the collection named library

A

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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

this gets its name from the way it is read:
~~~
For each element in collection do{
loop body
}
~~~

A

how does the
for-each loop get its name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

when might we see the fllowing exception
ConcurrentModificationException

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

what is the wording if we have the following
1.Collection = ArrayList
2.Objects / Elements it will hold = String

~~~
ArrayList<String>;
~~~</String>

A

this can be read as
an “ArrayList of String”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

this will return the next element of the collection and then move past it

A

describe the iterator method
next()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

what does the following validation code ensure:

If(index >=0 && index < collection.size())
{
    We have a valid index
}
A

what code can be used as validation which
ensures we do not recieve an IndexOutOfBoundsException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

this can be achieved using:
declaration
ArrayList<String> someString;
initialisation
someString = new ArrayList<>();

A

(la) 1. declare anArrayListof some type of element
2. then initialise anArrayListusing the diamond notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

what check can be made to avoid
null errors

A

to avoid these use a check such as:
If (var == null)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

(la) 1. declare anArrayListof some type of element
2. then initialise anArrayListusing the diamond notation

A

this can be achieved using:
declaration
ArrayList<String> someString;
initialisation
someString = new ArrayList<>();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

why do we not require our own index when using an iterator

A

this is not required because the iterator is itself aware and keeping track of its position within a collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

describe the
do-while loop
and how it operates

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

these include:
* .add(object)
* .get(index)
* .remove(index)
* .size()

A

name 4 methods of the ArrayList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

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

A

give examples of positive and negative boolean expressions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

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

A

why is a generic type class considered incomplete and how would we complete it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

describe the ArrayList method
.get(index)

A

this is used to access an element in the ArrayList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

what is an important note concerning iterating a collection using an iterator

A

an important note when performing this is:

that all interaction with the collection is done using the iterator we never directly access the collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

what exception will be shown if we try and

access an index of a collection that is equal to or greater than its size

A

in this case we will see the exception:
IndexOutOfBoundsException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

to avoid these use a check such as:
If (var == null)

A

what check can be made to avoid
null errors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

what is
garbage collection

A

this is a runtime system that reclaims memory from objects that have no references to them (anonymous objects)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

describe the keyword
null

A
  1. this is a reserved java keyword
  2. it essentially means: no object or empty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

this is not required because the iterator is itself aware and keeping track of its position within a collection

A

why do we not require our own index when using an iterator

54
Q

give examples of positive and negative boolean expressions

A

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

55
Q

ignore

56
Q

this is a class that does not define a single type but can be many types.

this is because it is actually composed of two types being:
1. itself
2. and a second type we provide it

when a generic type is declared and given a second type, the combination makes a new type

we then refer to this as a specific type

A

what is a generic class

57
Q

this is used to removes an element from the given index

A

describe the ArrayList method
.remove(index)

58
Q

what is the
syntax of the for-each loop

A

syntax is:
~~~
For (ElementType element : collection) {
Loop body
}
~~~

example is:
~~~
For (String fileName : files) {
System.out.println(fileName);
}
~~~

59
Q

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

A

describe the
do-while loop
and how it operates

60
Q

this will return the number of elements in the ArrayList

A

describe the ArrayList method
.size()

61
Q

describe how an iterator positions itself within a collection and give three points concerning
1. where it starts
2. how it moves to the next element
3. what happens when we ask the iterator to remove an element from the collection

A

1.It sets itself before the first element
2.When next() is called it returns the element in front of it then moves past it but stays before the second element
3.If we told this to remove an item it would remove the item it last passed

62
Q

how can we use an ArrayList

A

this is not strictly part of java but is included in the java standard library so to use it we must import it using:
import java.util.ArrayList;

63
Q

in this case we will see the exception:
IndexOutOfBoundsException

A

what exception will be shown if we try and

access an index of a collection that is equal to or greater than its size

64
Q

this is a runtime system that reclaims memory from objects that have no references to them (anonymous objects)

A

what is
garbage collection

65
Q

1.It sets itself before the first element
2.When next() is called it returns the element in front of it then moves past it but stays before the second element
3.If we told this to remove an item it would remove the item it last passed

A

describe how an iterator positions itself within a collection and give three points concerning
1. where it starts
2. how it moves to the next element
3. what happens when we ask the iterator to remove an element from the collection

66
Q

describe the ArrayList method
.add(object)

A

this is used to add elements to the ArrayList

67
Q

to begin using this we must import the iterator class that is part of the java standard library using:

Import java.util.Iterator;

A

how can we start using an
**iterator object **
in our program

68
Q

describe the iterator method
next()

A

this will return the next element of the collection and then move past it

69
Q

are the following two fields the same type, explain the answer

Private ArrayList<Person> members;
Private ArrayList<TicketMachine> machines;
A

these are not the same type:
1. they are composed of the same generic type (ArrayList)
2. but the generic type paramaters are differrent and so we have created two different specific types

70
Q

this is when we are uncertain about how many iterations will happen. while loops are suited to this type of iteration as we can base the iteration on a condition

A

describe
indefinite iteration

71
Q

describe the ArrayList method
.remove(index)

A

this is used to removes an element from the given index

72
Q

ArrayList includes this because it is a general purpose class or “generic type” this means it is not restricted in what types it can store but we must include the type via this

A

why does
ArrayList include a generic type parameter

73
Q

(la) use animportstatement to gain access to a library class

A

This can be achieved using the keyword “import” followed by the qualified name of the package

74
Q

This can be achieved using the keyword “import” followed by the qualified name of the package

A

(la) use animportstatement to gain access to a library class

75
Q

the syntax for this is:

while(booleanCondition) {
    Loop body
}
A

what is the
syntax of a while loop

76
Q

the purpose of this is so that we can iterate over a collection and perform actions on every element inside the collection

A

describe the purpose of the
for-each loop

77
Q

describe
chaining methods

A

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

78
Q

what is the naming convention when using a getter method to get a boolean value

A

the convention for this is to prepend the word “is” to the methods name

79
Q

Atypegiven betweenangle bracketsto make ageneric typespecific.

A

what are
generic type parameters

80
Q

an important note when performing this is:

that all interaction with the collection is done using the iterator we never directly access the collection

A

what is an important note concerning iterating a collection using an iterator

81
Q

give psuedocode that
declares and initialise an iterator object

A

declaration
Iterator<ElementTypeInCollection> it;

initialisation
it = collection.iterator();

equivalent
Iterator<ElementTypeInCollection> it = collection.iterator();

82
Q

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

A

describe
definite iteration

83
Q

this is also referred to as a
parameterized type
because it receives a type as a parameter when it is declared.

A

what is a
generic class also referred to as

84
Q

why is a generic type class considered incomplete and how would we complete it

A

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

85
Q

this can be read as
an “ArrayList of String”

A

what is the wording if we have the following
1.Collection = ArrayList
2.Objects / Elements it will hold = String

~~~
ArrayList<String>;
~~~</String>

86
Q

give two properties of searching a collection

A
  1. this involves an indefinite number of iterations
  2. this implies that a while loop would be better suited than a for-each loop
87
Q

describe the iterator method
hasNext()

A

this will check whether the collection has a next element

note: important to use this before moving to the next element

88
Q

(la) explain the purpose of a collection

A

the purpose of this is to hold many references to objects

89
Q

an iterator has been initialised with the identifier “it”
use it to iterate the collection it is from using a while loop
note: in pseudocode

A

this can be achieved using:

While (it.hasNext()) {
    Call it.next() to get the next element and hold it in variable
    Do something to that element
}
90
Q

describe
indefinite iteration

A

this is when we are uncertain about how many iterations will happen. while loops are suited to this type of iteration as we can base the iteration on a condition

91
Q

describe the iterator method
remove()

A

this will remove the element it last passed

92
Q

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;
}
A

write a method that returns the parameterized type ArrayList<String>
which will hold the first two objects from the collection named library

93
Q

what is a
specific type

A

this is the combination of a generic type and a generic type parameter to create a new type

94
Q

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)

A

what two things must be specified when declaring a collection

95
Q

what is the syntax for the string method
concat()

A

the syntax for this is
string.concat(string2)

@param string2 the string to append to the first string

96
Q

these include:
* A while loop does not have to be tied to a collection (we can loop on any condition as long as we can create a boolean expression)
* If we are using a while loop to iterate a collection we can abort early in an elegant way

A

name 2
benefits the while loop has over the for-each loop

97
Q

syntax is:
~~~
For (ElementType element : collection) {
Loop body
}
~~~

example is:
~~~
For (String fileName : files) {
System.out.println(fileName);
}
~~~

A

what is the
syntax of the for-each loop

98
Q

these are not the same type:
1. they are composed of the same generic type (ArrayList)
2. but the generic type paramaters are differrent and so we have created two different specific types

A

are the following two fields the same type, explain the answer

Private ArrayList<Person> members;
Private ArrayList<TicketMachine> machines;
99
Q

describe the purpose of the
for-each loop

A

the purpose of this is so that we can iterate over a collection and perform actions on every element inside the collection

100
Q

these include:
1.Ordered - meaning the objects can be accessed by using the position they are placed in
2.all elements are of same type - all items (elements) stored in this collection object will be of the same type
3.Infinite capacity - many elements can be added without a limit (except the hardware itself)

A

give three
properties of ArrayList

101
Q

give three
properties of ArrayList

A

these include:
1.Ordered - meaning the objects can be accessed by using the position they are placed in
2.all elements are of same type - all items (elements) stored in this collection object will be of the same type
3.Infinite capacity - many elements can be added without a limit (except the hardware itself)

102
Q

why does
ArrayList include a generic type parameter

A

ArrayList includes this because it is a general purpose class or “generic type” this means it is not restricted in what types it can store but we must include the type via this

103
Q

the convention for this is to prepend the word “is” to the methods name

A

what is the naming convention when using a getter method to get a boolean value

104
Q

(la) understand how a generic type relates to a specific type and a generic type parameter

A
  1. A generic type is a class that requires a type as a parameter.
  2. When the type is passed to it it becomes a specific type.
  3. We pass the type to the generic type within angle brackets known as diamond notation.
  4. What is passed inside the angle brackets is known as the generic type parameter
105
Q

this will retuen an iterator object for that collection

A

what will the
iterator() methodof collections return

106
Q

this is used to access an element in the ArrayList

A

describe the ArrayList method
.get(index)

107
Q

this method appends (concatenate) a string to the end of another string.

A

describe the string method
concat()

108
Q

give two benefits that give an iterator its power and usefullness

A

these include:
1. While we can iterate an array list using our own index there are other collections where providing our own index is either impossible or inefficient this is where the power of the iterator comes into play
2.Using an iterator allows us to safely remove elements from a collection while we iterate it. A for-each loop would throw an error if we used a collections remove method this is not the case when we use an iterator to do the removal for us

109
Q

this is not strictly part of java but is included in the java standard library so to use it we must import it using:
import java.util.ArrayList;

A

how can we use an ArrayList

110
Q
  1. A generic type is a class that requires a type as a parameter.
  2. When the type is passed to it it becomes a specific type.
  3. We pass the type to the generic type within angle brackets known as diamond notation.
  4. What is passed inside the angle brackets is known as the generic type parameter
A

(la) understand how a generic type relates to a specific type and a generic type parameter

111
Q

this can be achieved using:

While (it.hasNext()) {
    Call it.next() to get the next element and hold it in variable
    Do something to that element
}
A

an iterator has been initialised with the identifier “it”
use it to iterate the collection it is from using a while loop
note: in pseudocode

112
Q

what is the syntax for the
do-while loop

A

the syntax for this is:

do {  
    // code block to be executed}
while (condition);
113
Q

this will either:
* hold a generic type parameter
* or be left empty if we have already declared the type parameter

A

how is the
diamond notation
used

114
Q

what is an
iterator

A

this is an object that allows us to iterate over different types of collections

115
Q

these are libraries that come with a programming language and contain hundreds or thousands of classes that have become useful to the language. In Java these classes are contained in what are called packages and are used in the same way we would use our own classes

A

what are
class libraries

116
Q

name 2
benefits the while loop has over the for-each loop

A

these include:
* A while loop does not have to be tied to a collection (we can loop on any condition as long as we can create a boolean expression)
* If we are using a while loop to iterate a collection we can abort early in an elegant way

117
Q

the purpose of this is to hold many references to objects

A

(la) explain the purpose of a collection

118
Q

give to points to keep in mind when using a for-each loop

A

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

119
Q

what is the
syntax of a while loop

A

the syntax for this is:

while(booleanCondition) {
    Loop body
}
120
Q

what is a generic class

A

this is a class that does not define a single type but can be many types.

this is because it is actually composed of two types being:
1. itself
2. and a second type we provide it

when a generic type is declared and given a second type, the combination makes a new type

we then refer to this as a specific type

121
Q

describe the ArrayList method
.size()

A

this will return the number of elements in the ArrayList

122
Q

this will return the character at the specified index of the string

A

describe the
String.charAt(index)
method

123
Q

this exception might be seen if we tried to call a method on a variable that has not been initialised with an object

A

when might we see the exception
NullPointerException

124
Q

these include:
1. While we can iterate an array list using our own index there are other collections where providing our own index is either impossible or inefficient this is where the power of the iterator comes into play
2.Using an iterator allows us to safely remove elements from a collection while we iterate it. A for-each loop would throw an error if we used a collections remove method this is not the case when we use an iterator to do the removal for us

A

give two benefits that give an iterator its power and usefullness

125
Q

describe the
String.charAt(index)
method

A

this will return the character at the specified index of the string

126
Q

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())
A

what are the two outcomes of searching a collection and how might we express this in code

127
Q

name 3 methods that an iterator will hold

A

these include:
1. hasNext()
2. next()
3. remove()

128
Q

what are
class libraries

A

these are libraries that come with a programming language and contain hundreds or thousands of classes that have become useful to the language. In Java these classes are contained in what are called packages and are used in the same way we would use our own classes

129
Q

name 4 methods of the ArrayList

A

these include:
* .add(object)
* .get(index)
* .remove(index)
* .size()

130
Q

this will check whether the collection has a next element

note: important to use this before moving to the next element

A

describe the iterator method
hasNext()