Chapter 7 - Fixed-size collections – arrays Flashcards
describe the
increment operator
this will increment a variable by one and has the symbol ++
what two lengths can be found from a two-dimensioanl array
- we can find out how many rows the two dimensional array has (myArray.length)
- 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
The type of the array variable should match the type of the array object.
What should the type of the array variable match with
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
write a
for loop that will access all elements of an array
name the 4 reasons of when to use a for loop
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
write the following type name
“array of int”
how would we say the following type name
int[]
write a for loop that accesses every column of the row with index 2 of a multi-dimensional array
to achieve this:
for (int i = 0; i < myArray[0].length; i++){ System.out.print(myArray[2][i] + " "); }
this is the memory address of the first element in an array.
What is the
base address in an array?
this should be used:
1.If we need to iterate over all elements in a collection (but do not require a counter)
when should we use a for-each loop
how would we say the following type name
int[]
write the following type name
“array of int”
what two components are included in the declaration of an array
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
recall 5 points about the main method
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
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 }
using a for loop
write a loop that will process every element in a two-dimensional array
How do you access elements of an array in Java?
Elements of an array are accessed by indexing the array.
using a for-each loop
write a loop that will process every element in a two-dimensional array
this can be achieved with:
for (int[] row : myArray) { for (int col : row) { System.out.print(col + " "); // next element in row } System.out.println(); }
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
what two components are included in the declaration of an array
this can be achieved with:
for (int[] row : myArray) { for (int col : row) { System.out.print(col + " "); // next element in row } System.out.println(); }
using a for-each loop
write a loop that will process every element in a two-dimensional array
write a for loop that uses an iterator to loop over the tracks collection
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
What is the difference between creating an array object and a regular object?
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.
- we can find out how many rows the two dimensional array has (myArray.length)
- 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
what two lengths can be found from a two-dimensioanl array
write the syntax for the for loop and the 5 steps of how it operates
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
What is the
base address in an array?
this is the memory address of the first element in an array.
to achieve this we must make use of two loops with one nested inside the other.
- The outer loop controls the rows of the two-dimensional array, and 2. the inner loop controls the columns of the two-dimensional array.
how do we process every element in a two dimensional array
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.
What happens to the elements of an array when it is first created?
An example of indexing an array on the right side of an assignment is : Double half = readings[0] / 2
write an example of indexing an array on the right side of an assignment?
write the
syntax of an array initialiser
syntax:
Array[] arrayName = {el1, el2, el3};
note:
The size of the array will be determined by the number of elements placed in the initialiser
name 2
differences between array and ArrayLists
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
An example of indexing an array on the left side of an assignment is : Labels[5] = “hello”
write an example of indexing an array on the left side of an assignment?
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
name the 4 reasons of when to use a for loop
the two variants of this are:
1. prefix
2. postfix
name the
two variants of the increment operator
we achieve this by using a single index. This will return the whole row, which itself is a one-dimensional array.
Example:
myArray[0]
how do we access a row of a two dimensional array
describe the
prefix increment operator
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
in terms of an array, what is the
base type
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
the header for this is:
public static void main(String[] args)
write the
header for the main method
in terms of an array
What does it mean when the indexing occurs on the right side of the assignment?
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.
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
recall 5 points about the main method
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
write a for loop that uses an iterator to loop over the tracks collection
this can be achieved by:
for (int i = 0; i < myArray.length; i++){ System.out.print(myArray[i][4] + " "); }
using a for loop access column 4 of every row of a two-dimensional array
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];”
How do you create a new array object in Java?
this can be described as an array that holds a single row of elements
describe a one dimensional array
how do we process every element in a two dimensional array
to achieve this we must make use of two loops with one nested inside the other.
- The outer loop controls the rows of the two-dimensional array, and 2. the inner loop controls the columns of the two-dimensional array.
write the
header for the main method
the header for this is:
public static void main(String[] args)
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
when is the
for loop particularly suitable
describe a one dimensional array
this can be described as an array that holds a single row of elements
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
describe a two-dimensional array
What should the type of the array variable match with
The type of the array variable should match the type of the array object.
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
describe the
prefix increment operator
this can be achieved with:
for (int aRowElement : myArray[2]) { System.out.print(aRowElement + " "); }
write a for-each loop that accesses every column of the row with index 2 of a twodimensional array
to access 1 from the table we would write
int var = myArray[0][2];
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}};
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
in terms of an array, what is the
base type
write an example of indexing an array on the left side of an assignment?
An example of indexing an array on the left side of an assignment is : Labels[5] = “hello”
What happens to the elements of an array when it is first created?
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.
this should be used when we wish to loop over a collection and remove elements at the same time because
- a for each loop would not allow this
- with other loops we avoid an indexoutofboundsexception
when should an iterator be used
name the
two variants of the increment operator
the two variants of this are:
1. prefix
2. postfix
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.
in terms of an array
What does it mean when the indexing occurs on the left side of the assignment?
to achieve this:
for (int i = 0; i < myArray[0].length; i++){ System.out.print(myArray[2][i] + " "); }
write a for loop that accesses every column of the row with index 2 of a multi-dimensional array
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
write the syntax for the for loop and the 5 steps of how it operates
This is the name of the syntax that can be used to declare, initialise and populate the arrays elements in a single statement
what is an
array initialiser
What are the two ways that elements of an array can be accessed?
Elements of an array can be accessed by indexing the array on either side of an assignment operator (=).
in terms of an array
What does it mean when the indexing occurs on the left side of the assignment?
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.
example of this include:
1. int[] count
2. Person[] people
write a
declaration for an array
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.
what are the 3 factors that contribute to the efficiency of arrays
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
describe a
Fixed-size collection
write an example of indexing an array on the right side of an assignment?
An example of indexing an array on the right side of an assignment is : Double half = readings[0] / 2
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.
in terms of an array
What does it mean when the indexing occurs on the right side of the assignment?
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.
How can we find the memory address of an element in an array using the base address?
name 3 reasons we would use a while loop
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
these exists in modern programming languages due to their high efficiency over flexible sized collections
what is the main reason that arrays have made there way into modern programming languages
How do you create a new array object in Java?
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];”
when is the
for loop particularly suitable
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
write a
for loop that will access all elements of an array
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
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
write a statement that declares and initialises a two-dimensional array
when should we use a for-each loop
this should be used:
1.If we need to iterate over all elements in a collection (but do not require a counter)
Elements of an array are accessed by indexing the array.
How do you access elements of an array in Java?
describe a
Fixed-size collection
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
How can we find the memory address of an element in an array using the base address?
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.
how do we access a row of a two dimensional array
we achieve this by using a single index. This will return the whole row, which itself is a one-dimensional array.
Example:
myArray[0]
to achieve this:
String[] names = {"John", "Amy", "Ken", "Ahmed", "Amel", "Anton"};
using an array initialiser
create an array of String with length of 6
syntax:
Array[] arrayName = {el1, el2, el3};
note:
The size of the array will be determined by the number of elements placed in the initialiser
write the
syntax of an array initialiser
this can be achieved with:
int[][] myArray = {{4, 3, 1, 5, 3}, {9, 5, 3, 8, 2}, {4, 2, 9, 4, 3}};
using an array initialiser
write a statement that declares and initialises a two-dimensional array
what is the main reason that arrays have made there way into modern programming languages
these exists in modern programming languages due to their high efficiency over flexible sized collections
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
name 2
differences between array and ArrayLists
similarities between these include:
1. Both are indexed
2. Both contain references to objects
3. Both are themselves objects
name 3
similarities between array and ArrayLists
Elements of an array can be accessed by indexing the array on either side of an assignment operator (=).
What are the two ways that elements of an array can be accessed?
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
describe the
postfix increment operator
how should class types held in an array be accesssed
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
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
how should class types held in an array be accesssed
what are the 3 factors that contribute to the efficiency of arrays
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.
ignore
using a for loop
write a loop that will process every element in a two-dimensional array
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 }
using an array initialiser
write a statement that declares and initialises a two-dimensional array
this can be achieved with:
int[][] myArray = {{4, 3, 1, 5, 3}, {9, 5, 3, 8, 2}, {4, 2, 9, 4, 3}};
using an array initialiser
create an array of String with length of 6
to achieve this:
String[] names = {"John", "Amy", "Ken", "Ahmed", "Amel", "Anton"};
this will increment a variable by one and has the symbol ++
describe the
increment operator
describe the
postfix increment operator
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
using a for loop access column 4 of every row of a two-dimensional array
this can be achieved by:
for (int i = 0; i < myArray.length; i++){ System.out.print(myArray[i][4] + " "); }
write a
declaration for an array
example of this include:
1. int[] count
2. Person[] people
when should an iterator be used
this should be used when we wish to loop over a collection and remove elements at the same time because
- a for each loop would not allow this
- with other loops we avoid an indexoutofboundsexception
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
name 3 reasons we would use a while loop
name 3
similarities between array and ArrayLists
similarities between these include:
1. Both are indexed
2. Both contain references to objects
3. Both are themselves objects
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.
What is the difference between creating an array object and a regular object?
write a statement that declares and initialises a two-dimensional array
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
write a for-each loop that accesses every column of the row with index 2 of a twodimensional array
this can be achieved with:
for (int aRowElement : myArray[2]) { System.out.print(aRowElement + " "); }
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}};
to access 1 from the table we would write
int var = myArray[0][2];
what is an
array initialiser
This is the name of the syntax that can be used to declare, initialise and populate the arrays elements in a single statement
describe a two-dimensional array
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