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
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()) ```
26
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()**
27
this will remove the element it last passed
describe the iterator method **remove()**
28
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)
29
this is an object that allows us to iterate over different types of collections
what is an **iterator**
30
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
31
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
32
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**
33
write a method that returns the parameterized type `ArrayList` which will hold the first two objects from the collection named library
the code to achieve this is: ``` public ArrayList firstTwo(){ ArrayList temp = new ArrayList<>(); temp.add(library.get(0)); temp.add(library.get(1)); return temp; } ```
34
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**
35
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
36
what is the wording if we have the following **1.Collection = ArrayList 2.Objects / Elements it will hold = String** ``` ArrayList; ```
this can be read as **an “ArrayList of String”**
37
this will return the next element of the collection and then move past it
describe the iterator method **next()**
38
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**
39
this can be achieved using: **declaration** `ArrayList someString;` **initialisation** `someString = new ArrayList<>();`
(la) 1. declare an ArrayList of some type of element 2. then initialise an ArrayList using the diamond notation
40
what check can be made to avoid **null errors**
to avoid these use a check such as: `If (var == null)`
41
(la) 1. declare an ArrayList of some type of element 2. then initialise an ArrayList using the diamond notation
this can be achieved using: **declaration** `ArrayList someString;` **initialisation** `someString = new ArrayList<>();`
42
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
43
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
44
these include: * .add(*object*) * .get(*index*) * .remove(*index*) * .size()
name 4 methods of the ArrayList
45
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
46
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
47
describe the ArrayList method **.get(*index*)**
this is used to access an element in the ArrayList
48
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
49
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**
50
to avoid these use a check such as: `If (var == null)`
what check can be made to avoid **null errors**
51
what is **garbage collection**
this is a runtime system that reclaims memory from objects that have no references to them (anonymous objects)
52
describe the keyword **null**
1. this is a reserved java keyword 2. it essentially means: no object or empty
53
this is not required because the iterator is itself aware and keeping track of its position within a collection
why do we not require our own index when using an iterator
54
give examples of positive and negative boolean expressions
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
ignore
[glossary quiz](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014897)
56
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
what is a generic class
57
this is used to removes an element from the given index
describe the ArrayList method **.remove(*index*)**
58
what is the **syntax of the for-each loop**
syntax is: ``` For (ElementType element : collection) { Loop body } ``` example is: ``` For (String fileName : files) { System.out.println(fileName); } ```
59
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
describe the **do-while loop** and how it operates
60
this will return the number of elements in the ArrayList
describe the ArrayList method **.size()**
61
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
this can be thought of never really being at an index of an element but always in between. --- 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
how can we use an ArrayList
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
in this case we will see the exception: **IndexOutOfBoundsException**
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
this is a runtime system that reclaims memory from objects that have no references to them (anonymous objects)
what is **garbage collection**
65
this can be thought of never really being at an index of an element but always in between. --- 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
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
describe the ArrayList method **.add(*object*)**
this is used to add elements to the ArrayList
67
to begin using this we must import the iterator class that is part of the java standard library using: `Import java.util.Iterator;`
how can we start using an **iterator object ** in our program
68
describe the iterator method **next()**
this will return the next element of the collection and then move past it
69
are the following two fields the same type, explain the answer ``` Private ArrayList members; Private ArrayList machines; ```
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
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
describe **indefinite iteration**
71
describe the ArrayList method **.remove(*index*)**
this is used to removes an element from the given index
72
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
why does **ArrayList include a generic type parameter**
73
(la) use an import statement to gain access to a library class
This can be achieved using the keyword "import" followed by the qualified name of the package
74
This can be achieved using the keyword "import" followed by the qualified name of the package
(la) use an import statement to gain access to a library class
75
the syntax for this is: ``` while(booleanCondition) { Loop body } ```
what is the **syntax of a while loop**
76
the purpose of this is so that we can iterate over a collection and perform actions on every element inside the collection
describe the purpose of the **for-each loop**
77
describe **chaining methods**
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
what is the naming convention when using a getter method to get a boolean value
the convention for this is to prepend the word "is" to the methods name
79
A type given between angle brackets to make a generic type specific.
what are **generic type parameters**
80
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 is an important note concerning iterating a collection using an iterator
81
give psuedocode that declares and initialise an iterator object
declaration `Iterator it;` initialisation `it = collection.iterator();` equivalent `Iterator it = collection.iterator();`
82
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
describe **definite iteration**
83
this is also referred to as a **parameterized type** because it receives a type as a parameter when it is declared.
what is a **generic class also referred to as**
84
why is a generic type class considered incomplete and how would we complete it
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
this can be read as **an “ArrayList of String”**
what is the wording if we have the following **1.Collection = ArrayList 2.Objects / Elements it will hold = String** ``` ArrayList; ```
86
give two properties of searching a collection
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
describe the iterator method **hasNext()**
this will check whether the collection has a next element note: important to use this before moving to the next element
88
(la) explain the purpose of a collection
the purpose of this is to hold many references to objects
89
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
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
describe **indefinite iteration**
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
describe the iterator method **remove()**
this will remove the element it last passed
92
the code to achieve this is: ``` public ArrayList firstTwo(){ ArrayList temp = new ArrayList<>(); temp.add(library.get(0)); temp.add(library.get(1)); return temp; } ```
write a method that returns the parameterized type `ArrayList` which will hold the first two objects from the collection named library
93
what is a **specific type**
this is the combination of a generic type and a generic type parameter to create a new type
94
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)
what two things must be specified when declaring a collection
95
what is the syntax for the string method **concat()**
the syntax for this is *string*.concat(*string2*) @param string2 the string to append to the first string
96
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
name 2 **benefits the while loop has over the for-each loop**
97
syntax is: ``` For (ElementType element : collection) { Loop body } ``` example is: ``` For (String fileName : files) { System.out.println(fileName); } ```
what is the **syntax of the for-each loop**
98
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
are the following two fields the same type, explain the answer ``` Private ArrayList members; Private ArrayList machines; ```
99
describe the purpose of the **for-each loop**
the purpose of this is so that we can iterate over a collection and perform actions on every element inside the collection
100
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)
give three **properties of ArrayList**
101
give three **properties of ArrayList**
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
why does **ArrayList include a generic type parameter**
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
the convention for this is to prepend the word "is" to the methods name
what is the naming convention when using a getter method to get a boolean value
104
(la) understand how a generic type relates to a specific type and a generic type parameter
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
this will retuen an iterator object for that collection
what will the **iterator() methodof collections return**
106
this is used to access an element in the ArrayList
describe the ArrayList method **.get(*index*)**
107
this method appends (concatenate) a string to the end of another string.
describe the string method **concat()**
108
give two benefits that give an iterator its power and usefullness
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
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;**
how can we use an ArrayList
110
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
(la) understand how a generic type relates to a specific type and a generic type parameter
111
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 } ```
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
what is the syntax for the **do-while loop**
the syntax for this is: ``` do { // code block to be executed} while (condition); ```
113
this will either: * hold a generic type parameter * or be left empty if we have already declared the type parameter
how is the **diamond notation** used
114
what is an **iterator**
this is an object that allows us to iterate over different types of collections
115
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
what are **class libraries**
116
name 2 **benefits the while loop has over the for-each loop**
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
the purpose of this is to hold many references to objects
(la) explain the purpose of a collection
118
give to points to keep in mind when **using a for-each loop**
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
what is the **syntax of a while loop**
the syntax for this is: ``` while(booleanCondition) { Loop body } ```
120
what is a generic class
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
describe the ArrayList method **.size()**
this will return the number of elements in the ArrayList
122
this will return the character at the specified index of the string
describe the **String.charAt(index)** method
123
this exception might be seen if we tried to call a method on a variable that has not been initialised with an object
when might we see the exception **NullPointerException**
124
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
give two benefits that give an iterator its power and usefullness
125
describe the **String.charAt(index)** method
this will return the character at the specified index of the string
126
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()) ```
what are the two outcomes of searching a collection and how might we express this in code
127
name 3 methods that an iterator will hold
these include: 1. hasNext() 2. next() 3. remove()
128
what are **class libraries**
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
name 4 methods of the ArrayList
these include: * .add(*object*) * .get(*index*) * .remove(*index*) * .size()
130
this will check whether the collection has a next element note: important to use this before moving to the next element
describe the iterator method **hasNext()**