Chapter 7 - Fixed-size collections – arrays Flashcards

1
Q

describe the
increment operator

A

this will increment a variable by one and has the symbol ++

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

ignore

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

what two lengths can be found from a two-dimensioanl array

A
  1. we can find out how many rows the two dimensional array has (myArray.length)
  2. we can find out how many columns the multi-dimensional array has (myArray[0].length)

note:
when finding the columns. by convention we would get the first row and ask for its length

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

The type of the array variable should match the type of the array object.

A

What should the type of the array variable match with

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

this can be achieved by:

For (int i = 0; I < array.length; I++) {
    Statements to repaet
}

note:
Array has a field called length that will return the length of the array

A

write a
for loop that will access all elements of an array

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

name the 4 reasons of when to use a for loop

A

this should be used if the following reasons apply:
1.The loop is not related to a collection
2.If we know how many iterations will take place beforehand and this number of iterations will not change
3.If we need to use the loop counter within the body
4.With an iterator if we want to remove elements and examine every element

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

write the following type name
“array of int”

A

how would we say the following type name
int[]

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

write a for loop that accesses every column of the row with index 2 of a multi-dimensional array

A

to achieve this:

for (int i = 0; i < myArray[0].length; i++){
    System.out.print(myArray[2][i] + " ");
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

this is the memory address of the first element in an array.

A

What is the
base address in an array?

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

this should be used:
1.If we need to iterate over all elements in a collection (but do not require a counter)

A

when should we use a for-each loop

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

how would we say the following type name
int[]

A

write the following type name
“array of int”

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

what two components are included in the declaration of an array

A

to declare this data type we will include:
1. the type name: consisting of the base type and a pair of square brackets
2.The variable name that will hold the collection

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

ignore

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

recall 5 points about the main method

A

some points on this:
1. all java programs begin here
2. when java executes it will search for this
3. typically this is in its own class where the class is named as the object it creates followed by Main (i.e. GameMain)
4. statements in the class with this method are kept to aminimum and should not contain any logic for your actual prgram
5. this should create the object that is the starting point of your app and then call one of its instance methods

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

this can be achived with:

for (int i = 0; i < myArray.length; i++) {
    for (int j = 0; j < myArray[0].length; j++) {
        System.out.print(myArray[i][j] + " "); // next element in row
    }
System.out.println(); // move on to next row
}
A

using a for loop
write a loop that will process every element in a two-dimensional array

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

How do you access elements of an array in Java?

A

Elements of an array are accessed by indexing the array.

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

using a for-each loop
write a loop that will process every element in a two-dimensional array

A

this can be achieved with:

for (int[] row : myArray) {
    for (int col : row) {
        System.out.print(col + " "); // next element in row
    }
    System.out.println(); 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

to declare this data type we will include:
1. the type name: consisting of the base type and a pair of square brackets
2.The variable name that will hold the collection

A

what two components are included in the declaration of an array

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

this can be achieved with:

for (int[] row : myArray) {
    for (int col : row) {
        System.out.print(col + " "); // next element in row
    }
    System.out.println(); 
}
A

using a for-each loop
write a loop that will process every element in a two-dimensional array

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

write a for loop that uses an iterator to loop over the tracks collection

A

this can be achived as:

For (Iterator<Track> it = tracks.iterator(); it.hasNext; ) {
    Track track = it.next()
    Remove element if we wish
}

note:
the post-body action is omitted with this type of for loop use

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

What is the difference between creating an array object and a regular object?

A

Arrays do not have constructors and are initialized with square brackets to specify the length of the array, whereas regular objects use parentheses after the class name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
  1. we can find out how many rows the two dimensional array has (myArray.length)
  2. we can find out how many columns the multi-dimensional array has (myArray[0].length)

note:
when finding the columns. by convention we would get the first row and ask for its length

A

what two lengths can be found from a two-dimensioanl array

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

write the syntax for the for loop and the 5 steps of how it operates

A

syntax:

For (initialisation; condtion; post-body action) {
    Statements to be repeated
}

operations:
1.Initialisation - is executed once only at the start
2.The condition is assed and the loop entered only if the condition evaluates to true
3.The body is executed
4.The post-body action is executed
5.Back to step 2

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

What is the
base address in an array?

A

this is the memory address of the first element in an array.

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

to achieve this we must make use of two loops with one nested inside the other.

  1. The outer loop controls the rows of the two-dimensional array, and 2. the inner loop controls the columns of the two-dimensional array.
A

how do we process every element in a two dimensional array

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

When an array is first created, it is considered “empty” but has space allocated for the specified number of elements. The elements are filled with default values - for example if the base type is a class type, each element will hold a value of null, and if the base type is int, each element will hold a value of 0.

A

What happens to the elements of an array when it is first created?

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

An example of indexing an array on the right side of an assignment is : Double half = readings[0] / 2

A

write an example of indexing an array on the right side of an assignment?

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

write the
syntax of an array initialiser

A

syntax:

Array[] arrayName = {el1, el2, el3};

note:
The size of the array will be determined by the number of elements placed in the initialiser

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

name 2
differences between array and ArrayLists

A

differences between these include:
1. An array has a fixed size which must be declared at creation and cannot be changed and does not grow dynamically like an ArrayList
2. An array may store a primitive type or a class type

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

An example of indexing an array on the left side of an assignment is : Labels[5] = “hello”

A

write an example of indexing an array on the left side of an assignment?

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

this should be used if the following reasons apply:
1.The loop is not related to a collection
2.If we know how many iterations will take place beforehand and this number of iterations will not change
3.If we need to use the loop counter within the body
4.With an iterator if we want to remove elements and examine every element

A

name the 4 reasons of when to use a for loop

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

the two variants of this are:
1. prefix
2. postfix

A

name the
two variants of the increment operator

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

we achieve this by using a single index. This will return the whole row, which itself is a one-dimensional array.

Example:
myArray[0]

A

how do we access a row of a two dimensional array

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

describe the
prefix increment operator

A

this works by placing the operator before the variable the operation then happens as follows
1. the variable the operator is attached to is incremented by 1
2. the assignment is then carried out (if any)

example:

int x = 5;
int y;
y = ++x; // y is now 6, x is now 6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

in terms of an array, what is the
base type

A

this is the data type of the elements of the array, like other collections we must specify what data type will be held by the collection

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

the header for this is:

public static void main(String[] args)

A

write the
header for the main method

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

in terms of an array
What does it mean when the indexing occurs on the right side of the assignment?

A

When the indexing occurs on the right side of the assignment, it is equivalent to an accessor (get method) and will retrieve the value that is held at the specified index.

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

some points on this:
1. all java programs begin here
2. when java executes it will search for this
3. typically this is in its own class where the class is named as the object it creates followed by Main (i.e. GameMain)
4. statements in the class with this method are kept to aminimum and should not contain any logic for your actual prgram
5. this should create the object that is the starting point of your app and then call one of its instance methods

A

recall 5 points about the main method

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

this can be achived as:

For (Iterator<Track> it = tracks.iterator(); it.hasNext; ) {
    Track track = it.next()
    Remove element if we wish
}

note:
the post-body action is omitted with this type of for loop use

A

write a for loop that uses an iterator to loop over the tracks collection

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

this can be achieved by:

for (int i = 0; i < myArray.length; i++){
    System.out.print(myArray[i][4] + " ");
}
A

using a for loop access column 4 of every row of a two-dimensional array

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

to achiev this:

Use the “new” keyword followed by the array type and square brackets with the length of the array, such as “new int[24];”

A

How do you create a new array object in Java?

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

this can be described as an array that holds a single row of elements

A

describe a one dimensional array

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

how do we process every element in a two dimensional array

A

to achieve this we must make use of two loops with one nested inside the other.

  1. The outer loop controls the rows of the two-dimensional array, and 2. the inner loop controls the columns of the two-dimensional array.
44
Q

write the
header for the main method

A

the header for this is:

public static void main(String[] args)

45
Q

this becomes suitable when:
1.We wish to execute a set of statements a fixed number of times (definite iteration)
2.We need a variable inside the loop whos value changes by a fixed amount each time. Usually incremented by 1

A

when is the
for loop particularly suitable

46
Q

describe a one dimensional array

A

this can be described as an array that holds a single row of elements

47
Q

this is an extension of a one-dimensional array where each element is itself an array. this is best thought of as a table of rows and columns

A

describe a two-dimensional array

48
Q

What should the type of the array variable match with

A

The type of the array variable should match the type of the array object.

49
Q

this works by placing the operator before the variable the operation then happens as follows
1. the variable the operator is attached to is incremented by 1
2. the assignment is then carried out (if any)

example:

int x = 5;
int y;
y = ++x; // y is now 6, x is now 6
A

describe the
prefix increment operator

50
Q

this can be achieved with:

for (int aRowElement : myArray[2]) { 
    System.out.print(aRowElement + " ");
}
A

write a for-each loop that accesses every column of the row with index 2 of a twodimensional array

51
Q

to access 1 from the table we would write

int var = myArray[0][2];

A

using the following info, get access to the number 1

int[][] myArray = {{4, 3, 1, 5, 3}, {9, 5, 3, 8, 2}, {4, 2, 9, 4, 3}};

52
Q

this is the data type of the elements of the array, like other collections we must specify what data type will be held by the collection

A

in terms of an array, what is the
base type

53
Q

write an example of indexing an array on the left side of an assignment?

A

An example of indexing an array on the left side of an assignment is : Labels[5] = “hello”

54
Q

What happens to the elements of an array when it is first created?

A

When an array is first created, it is considered “empty” but has space allocated for the specified number of elements. The elements are filled with default values - for example if the base type is a class type, each element will hold a value of null, and if the base type is int, each element will hold a value of 0.

55
Q

this should be used when we wish to loop over a collection and remove elements at the same time because

  1. a for each loop would not allow this
  2. with other loops we avoid an indexoutofboundsexception
A

when should an iterator be used

56
Q

name the
two variants of the increment operator

A

the two variants of this are:
1. prefix
2. postfix

57
Q

When the indexing occurs on the left side of the assignment, it is equivalent to a mutator (set method) and will set that index to the given value.

A

in terms of an array
What does it mean when the indexing occurs on the left side of the assignment?

58
Q

to achieve this:

for (int i = 0; i < myArray[0].length; i++){
    System.out.print(myArray[2][i] + " ");
}
A

write a for loop that accesses every column of the row with index 2 of a multi-dimensional array

59
Q

syntax:

For (initialisation; condtion; post-body action) {
    Statements to be repeated
}

operations:
1.Initialisation - is executed once only at the start
2.The condition is assed and the loop entered only if the condition evaluates to true
3.The body is executed
4.The post-body action is executed
5.Back to step 2

A

write the syntax for the for loop and the 5 steps of how it operates

60
Q

This is the name of the syntax that can be used to declare, initialise and populate the arrays elements in a single statement

A

what is an
array initialiser

61
Q

What are the two ways that elements of an array can be accessed?

A

Elements of an array can be accessed by indexing the array on either side of an assignment operator (=).

62
Q

in terms of an array
What does it mean when the indexing occurs on the left side of the assignment?

A

When the indexing occurs on the left side of the assignment, it is equivalent to a mutator (set method) and will set that index to the given value.

63
Q

example of this include:
1. int[] count
2. Person[] people

A

write a
declaration for an array

64
Q

reasons for this include:
1. Elements are stored in adjacent blocks of memory
2. each element takes up the same amount of memory, and
3. a simple arithmetic operation can be used to find the address of any other element using the base address.

A

what are the 3 factors that contribute to the efficiency of arrays

65
Q

this is a collection that has the number of elements it can hold fixed at the point of creation. Meaning it can neither grow nor shrink. An array is such a collection

A

describe a
Fixed-size collection

66
Q

write an example of indexing an array on the right side of an assignment?

A

An example of indexing an array on the right side of an assignment is : Double half = readings[0] / 2

67
Q

When the indexing occurs on the right side of the assignment, it is equivalent to an accessor (get method) and will retrieve the value that is held at the specified index.

A

in terms of an array
What does it mean when the indexing occurs on the right side of the assignment?

68
Q

we can achieve this by using the following arithmetic operation:

Base_address + b x k,

where x is the amount of memory used by each element
where k is an index of the array.

A

How can we find the memory address of an element in an array using the base address?

69
Q

name 3 reasons we would use a while loop

A

this should be used if any of the following apply:
1.The loop is not related to a collection
2.Preferred if we do not know the number of iterations at the start. This can be determined on the fly by a boolean condition
3.With an iterator if we want to remove elements from the collection and may end before reaching the end of the collection

70
Q

these exists in modern programming languages due to their high efficiency over flexible sized collections

A

what is the main reason that arrays have made there way into modern programming languages

71
Q

How do you create a new array object in Java?

A

to achiev this:

Use the “new” keyword followed by the array type and square brackets with the length of the array, such as “new int[24];”

72
Q

when is the
for loop particularly suitable

A

this becomes suitable when:
1.We wish to execute a set of statements a fixed number of times (definite iteration)
2.We need a variable inside the loop whos value changes by a fixed amount each time. Usually incremented by 1

73
Q

write a
for loop that will access all elements of an array

A

this can be achieved by:

For (int i = 0; I < array.length; I++) {
    Statements to repaet
}

note:
Array has a field called length that will return the length of the array

74
Q

this can be achieved with:

int[][] myArray = new int[3][5];

note:
By convention the first dimension in this case 3 represents the rows and the second dimension represents the columns. so we have 3 arrays that have a length of 5

A

write a statement that declares and initialises a two-dimensional array

75
Q

when should we use a for-each loop

A

this should be used:
1.If we need to iterate over all elements in a collection (but do not require a counter)

76
Q

Elements of an array are accessed by indexing the array.

A

How do you access elements of an array in Java?

77
Q

describe a
Fixed-size collection

A

this is a collection that has the number of elements it can hold fixed at the point of creation. Meaning it can neither grow nor shrink. An array is such a collection

78
Q

How can we find the memory address of an element in an array using the base address?

A

we can achieve this by using the following arithmetic operation:

Base_address + b x k,

where x is the amount of memory used by each element
where k is an index of the array.

79
Q

how do we access a row of a two dimensional array

A

we achieve this by using a single index. This will return the whole row, which itself is a one-dimensional array.

Example:
myArray[0]

80
Q

to achieve this:

String[] names = {"John", "Amy", "Ken", "Ahmed", "Amel", "Anton"};

A

using an array initialiser
create an array of String with length of 6

81
Q

syntax:

Array[] arrayName = {el1, el2, el3};

note:
The size of the array will be determined by the number of elements placed in the initialiser

A

write the
syntax of an array initialiser

82
Q

this can be achieved with:

int[][] myArray = {{4, 3, 1, 5, 3}, {9, 5, 3, 8, 2}, {4, 2, 9, 4, 3}};

A

using an array initialiser
write a statement that declares and initialises a two-dimensional array

83
Q

what is the main reason that arrays have made there way into modern programming languages

A

these exists in modern programming languages due to their high efficiency over flexible sized collections

84
Q

differences between these include:
1. An array has a fixed size which must be declared at creation and cannot be changed and does not grow dynamically like an ArrayList
2. An array may store a primitive type or a class type

A

name 2
differences between array and ArrayLists

85
Q

similarities between these include:
1. Both are indexed
2. Both contain references to objects
3. Both are themselves objects

A

name 3
similarities between array and ArrayLists

86
Q

Elements of an array can be accessed by indexing the array on either side of an assignment operator (=).

A

What are the two ways that elements of an array can be accessed?

87
Q

this works by placing the operator after the variable. the following operations then occur
1. the assignment is carried out (if any)
2. the variable the operator is attached to is incremented by 1

example:

int x = 5;
int y;
y = x++; // y is now 5, x is now 6
A

describe the
postfix increment operator

88
Q

how should class types held in an array be accesssed

A

class types held in array should only be accessed via the index of the array we should not copy the eference into a new variable

for strings this might be ok as they are immutable but for other class types we might make a change to the copy that is reflected in the array of which we did not intend to change

89
Q

class types held in array should only be accessed via the index of the array we should not copy the eference into a new variable

for strings this might be ok as they are immutable but for other class types we might make a change to the copy that is reflected in the array of which we did not intend to change

A

how should class types held in an array be accesssed

90
Q

what are the 3 factors that contribute to the efficiency of arrays

A

reasons for this include:
1. Elements are stored in adjacent blocks of memory
2. each element takes up the same amount of memory, and
3. a simple arithmetic operation can be used to find the address of any other element using the base address.

91
A

ignore

92
Q

using a for loop
write a loop that will process every element in a two-dimensional array

A

this can be achived with:

for (int i = 0; i < myArray.length; i++) {
    for (int j = 0; j < myArray[0].length; j++) {
        System.out.print(myArray[i][j] + " "); // next element in row
    }
System.out.println(); // move on to next row
}
93
Q

using an array initialiser
write a statement that declares and initialises a two-dimensional array

A

this can be achieved with:

int[][] myArray = {{4, 3, 1, 5, 3}, {9, 5, 3, 8, 2}, {4, 2, 9, 4, 3}};

94
Q

using an array initialiser
create an array of String with length of 6

A

to achieve this:

String[] names = {"John", "Amy", "Ken", "Ahmed", "Amel", "Anton"};

95
Q

this will increment a variable by one and has the symbol ++

A

describe the
increment operator

96
Q

describe the
postfix increment operator

A

this works by placing the operator after the variable. the following operations then occur
1. the assignment is carried out (if any)
2. the variable the operator is attached to is incremented by 1

example:

int x = 5;
int y;
y = x++; // y is now 5, x is now 6
97
Q

using a for loop access column 4 of every row of a two-dimensional array

A

this can be achieved by:

for (int i = 0; i < myArray.length; i++){
    System.out.print(myArray[i][4] + " ");
}
98
Q

write a
declaration for an array

A

example of this include:
1. int[] count
2. Person[] people

99
Q

when should an iterator be used

A

this should be used when we wish to loop over a collection and remove elements at the same time because

  1. a for each loop would not allow this
  2. with other loops we avoid an indexoutofboundsexception
100
Q

this should be used if any of the following apply:
1.The loop is not related to a collection
2.Preferred if we do not know the number of iterations at the start. This can be determined on the fly by a boolean condition
3.With an iterator if we want to remove elements from the collection and may end before reaching the end of the collection

A

name 3 reasons we would use a while loop

101
Q

name 3
similarities between array and ArrayLists

A

similarities between these include:
1. Both are indexed
2. Both contain references to objects
3. Both are themselves objects

102
Q

Arrays do not have constructors and are initialized with square brackets to specify the length of the array, whereas regular objects use parentheses after the class name.

A

What is the difference between creating an array object and a regular object?

103
Q

write a statement that declares and initialises a two-dimensional array

A

this can be achieved with:

int[][] myArray = new int[3][5];

note:
By convention the first dimension in this case 3 represents the rows and the second dimension represents the columns. so we have 3 arrays that have a length of 5

104
Q

write a for-each loop that accesses every column of the row with index 2 of a twodimensional array

A

this can be achieved with:

for (int aRowElement : myArray[2]) { 
    System.out.print(aRowElement + " ");
}
105
Q

using the following info, get access to the number 1

int[][] myArray = {{4, 3, 1, 5, 3}, {9, 5, 3, 8, 2}, {4, 2, 9, 4, 3}};

A

to access 1 from the table we would write

int var = myArray[0][2];

106
Q

what is an
array initialiser

A

This is the name of the syntax that can be used to declare, initialise and populate the arrays elements in a single statement

107
Q

describe a two-dimensional array

A

this is an extension of a one-dimensional array where each element is itself an array. this is best thought of as a table of rows and columns