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