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.