Topic 4.1 and 4.2: Creating and Using Arrays - Declare, instantiate, initialize and use a one-dimensional array and Declare, instantiate, initialize and use multi-dimensional arrays Flashcards
What is
array initialization
in Java?
This refers to assigning values to the elements of the array after it has been instantiated. It involves specifying the initial values of the array elements.
This sets numeric types to 0, booleans to false, and object references to null. If we do not explicitly initialize the array at the point of declaration, the elements of the array will have these default values.
What is
default initialization of an array
in Java?
What is an array in Java?
This is a data structure that stores a fixed-size sequence of elements of the same type.
What should you consider when choosing between a for loop and an enhanced for-each loop for looping over arrays?
When looping over arrays, consider whether you need access to the index or only the values held in the elements. If you require knowledge of the index, a for loop is the appropriate choice. However, if you only need the values of the array elements, you can opt for the enhanced for-each loop.
What is the
range of indices for accessing elements in an array?
This start from 0 and go up to (array length - 1). This means that the first element is accessed using index 0, and the last element is accessed using index (array length - 1).
to accomplish this, you use the array name followed by the index in square brackets.
For example, “int firstElement = numbers[0];
”
How do you
access elements in an array
in Java?
to accomplish this you specify the type of the elements it will hold, followed by square brackets ([]), and then the name of the array.
For example:
int[] numbers;
How do you declare an array in Java?
This refers to creating a new array object in memory.
It involves allocating memory space for the array and setting up the necessary data structures to hold the elements.
What does it mean to
instantiate an array
in Java?
How can you initialize a multi-dimensional array using explicit initialization?
To initialize a multi-dimensional array using this syntax, you can provide the values for each element of the array during declaration. Here’s an example:
int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
This refers to assigning values to the elements of the array after it has been instantiated. It involves specifying the initial values of the array elements.
What is
array initialization
in Java?
This involves specifying specific values for the elements of the array during instantiation using the new keyword.
For example, “int[] numbers = new int[]{1, 2, 3, 4, 5};
” explicitly initializes the array with the values 1, 2, 3, 4, and 5.
What is
explicit initialization
of an array in Java?
How do you
access elements in an array
in Java?
to accomplish this, you use the array name followed by the index in square brackets.
For example, “int firstElement = numbers[0];
”
This is a data structure that stores a fixed-size sequence of elements of the same type.
What is an array in Java?
This is carried out after declaring an array
you need to allocate memory for it using the new keyword. For example, “numbers = new int[5];
” creates an integer array of size 5 and assigns it to the “numbers” variable.
Alternatively, you can combine declaration and this into a single statement: “int[] numbers = new int[5];
”.
How do you
instantiate an array
in Java?
How do you
instantiate an array
in Java?
This is carried out after declaring an array
you need to allocate memory for it using the new keyword. For example, “numbers = new int[5];
” creates an integer array of size 5 and assigns it to the “numbers” variable.
Alternatively, you can combine declaration and this into a single statement: “int[] numbers = new int[5];
”.