1/39
Flashcards on Java Arrays
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is an array in Java?
A special variable having one name, but storing a list of data items, with each item being directly accessible.
What data types can an array store in Java?
int, double, char, boolean, String, or any other type, but all values must be the same type.
What is the syntax for creating an array in Java?
Base-type[] name = new Base-type[length];
What does the 'new' operator do when creating an array in Java?
It allocates memory for the array and automatically initializes all of its elements to zeros.
What is the significance of 'length' when creating an array?
The total number of elements in a given array.
What is the two-step process to create an array in Java?
baseType[] arrayName; arrayName = new baseType[length];
What should you keep in mind during the two-step array creation process?
You cannot use the array until you inform the compiler of the size of the array in step #2.
What exception is thrown if you try to create an array with a negative size?
A NegativeArraySizeException.
Is it possible to create an array with zero elements?
true
What is the syntax for initializing an array at declaration time in Java?
int[] a = {1, 2, 3, 4};
What is a partially-filled array?
Not all elements of the array might receive values.
How do you get the capacity of an array?
a.length
What is the capacity of an array?
The number of array elements that are created when you define the array.
What is the rule about the 'length' variable for a Java array?
It is final, its value cannot be changed.
In context of arrays, what is an 'index'?
The lighter numbers outside the boxes (usually start from 0).
What is the index of the first element in a Java array?
The first element of array starts at 0.
How to you access an element in a 1-D array?
Use two square brackets [ ] operator.
What exception occurs when the index is negative or greater than the array's length -1?
ArrayIndexOutOfBoundsException.
Give one example of 'Creating and Accessing Arrays'?
Read 7 or N temperatures from the user and show which are above and which are below the average of the N temperatures.
What is the formula of getting the last index?
Last index is arrayName.length – 1.
What is a two-dimensional array used for?
A data structure that can represent a table with rows and columns.
What is the Syntax for Declaring and Creating a Two-Dimensional Array in Java?
BaseType[][] arrayName = new BaseType[ROWS][COLUMNS];
What are 'Ragged Arrays'?
Not necessary for all rows to be of the same length.
How do you Access Elements in Two-Dimensional Arrays?
You could use two for loops, one nested within the other.
What happen while copying arrays?
Copies the reference, but it doesn’t copy the array itself.
What does Assignment operator '=' copies?
Reference or memory address.
How do we actually copy the array, not just the reference?
Create a new array then, copy the elements from one to the other
What is array traversal?
Looping through the elements of an array.
When we send to a method a single element or indexed variable as a method’s argument, it is also be called?
Pass by-value; where we just pass a copy of the arguments value to the parameter in the method’s header.
When we send to a method the entire array as a method’s argument, it is also be called?
Pass by-reference; where we actually pass the actual memory address of an array to the method.
What is the syntax difference writing for passing the entire arrays as arguments to a method?
No square brackets are written when you pass an entire array as an argument to a method.
Is array resizable?
Resizing an array is possible!
Returning array from methods, what is being returned?
When a method returns an array, the reference of the array is returned (memory address!)
What does method 'equals' do?
Checks length and each individual pair of array elements
To actually copy the array, not just the reference:
Create a new array.
Then, copy the elements from one to the other
What is an array?
Collection of variables of the same type.
How are arrays created?
Object, created with operator new.
How elements are numbered?
Starting with 0, ending with 1 less than the length.
What does an int do in a partially filled arrays?
Used to track how many are used
Multidimensional arrays are implemented as?
Array of arrays