1/63
Looks like no tags are added yet.
What is an array?
Ordered list of items of a given data type
What is an element?
Each item in an array
What is an array index?
Each element’s location number
What do array indexes start with?
0
How to assign an element to an array index?
arrayName[#] = value
What is the last element’s index?
NumElements - 1
How does array reference variable declaration and array allocation look like?
dataType[] arrayName = new dataType[numElements];
What does the new keyword do?
Creates space in memory to store the array with the specific number of elements
How to declare array reference first and then allocate array?
dataType[] arrayName;
arrayName = new dataType[numElements];
What is the default value all int array elements are initialized to?
0
How to find a certain element in a list? (for example the 7th element)?
arrayName[n - 1]
ex. arrayName[7 -1]
What function accesses the amount of elements in an array?
arrayName.length
What is the default value for int and floating-point data type arrays?
0
What is the default value for boolean data type arrays?
False
How to set non-default values to an array?
dataType[] arrayName = {value, value, value};
What is a common loop structure when iterating through arrays?
for(i = 0; i < arrayName.length; ++i)
When using a loop to determine max value in array, how to set up max variable?
max variable initialized before loop, initialize to one of the elements of the loop
What happens if you try to access an array with index that is out of the array’s index range?
Results in runtime error that causes program to terminate
How to pass array reference to a method?
public static void methodName(arrayDataType[] arrayReference)
How is an array stored in a method?
Elements of the array stored indirectly in the heap and the reference to the array is stored in the stack frame
What will happen when you change an array in a method and why?
It will also change the array. This happens because Java passes a copy of the reference to the method. It has the same reference to the same array so altering it would change the array just as a regular reference would.
Can you change an array reference inside a method?
No
What can be changed for an array inside a method and what cannot be changed?
The elements can be changed.
The array length cannot be changed.
Can you swap array references in a method?
No
What happens when you assign array parameter in method?
It only updates local reference (cnages what it is referencing to, so it is not referencing the array anymore) and doesn’t change the array reference
What are related arrays?
Element at an index of one array corresponds to the other array element at the same index
What is a common way to swap two variables values?
Use temporary variable that briefly stores a value
temp = x
x = y
y = temp
Can you use a loop to modify elements of an array?
Yes!
What would be a basic loop to copy the elements of one array to another array?
for(int i = 0; i < arrayName.length; ++i){
copiedArray[i] = arrayName[i];
}
How many items can a method return?
Only one item
How to modify an array’s size in a method?
Create new array with the modified size
Copy existing array elements to new array
return new constructed array
In main, assign original array reference to the new array returned by method
ex. arrayName = methodName(arrayName);
What are three common purposes of a method that uses arrays?
Calculate a value based on array, leave array unchanged
Change array’s contents but not size
Create new array or change arrays size
When creating a method that changes array’s contents but not the size, what is the return type?
Void return type
When creating a method that creates a new array or changes array’s size, what is the return type? How would the method header look like?
return array reference
ex. public static dataType[] methodName()
In a method, when you need an array to calculate something but you want to avoid modifying the array what can you do?
Make a copy of the array and perform the calculation using the copy
What is an Arrays method that copies an array? What would be the syntax to successfully copy to another array reference?
dataType[] arrayCopy = Arrays.copyOf(arrayName, arrayName.length);
What would be the strategy to reversing an array?
Make a series of swaps. Swap first element with last element, second item with second to last item, etc.
What would a standard reversal array code look like?
for (i = 0; i < (userVals.length / 2); ++i) {
tempVal = userVals[i];
userVals[i] = userVals[userVals.length - 1 - i];
userVals[userVals.length - 1 - i] = tempVal;
}How to declare a two-dimensional array?
int[][] arrayName = new int[R][C]
R = # of rows
C = # of columns
How to find total elements of a two-dimensional array?
R * C
What does this code access and assign value to? (row and column) -
numVals[4][2] = 99;
Assigned to the 5th row and 3rd column
(assuming the actual row number starts at 1)
How to initialize a two-dimensional array during declaration?
int[] [] arrayName = {
{value, value, value}, //notice the comma!!
{value, value, value}
};
What is the standard code for using a loop to iterate through 2-dimensional array?
for (i = 0; i < arrayName.length; i++){
for (j = 0; j < arrayName[i].length; j++){
arrayName[i][j] = scnr.nextInt();
}
}
What is an enhanced for loop/ for each loop?
A for loop that iterates through each element in an array
What does an enhanced for loop declare?
It declares a new loop variable
How is an enhanced for loop formatted?
for(dataType loopVariable : arrayName) {
What is the the loop variable assigned to in an enhanced for loop?
Loop variable assigned with each successive element of an array
i.e. The first iteration will assign loopVariable with the first element of arrayName, second iteration will assign loopVariable with second element, etc.
How are elements accessed in for each loop?
Only accessed forward from first element to last element
When you use an enhanced for loop, is the array modified?
No, can’t modify array with loop variable
What is a perfect size array?
Array where number of elements is exactly equal to the memory allocated
How is a perfect size array created?
Created when you declare and initialize an array at the same time
When is it appropriate to use perfect size arrays?
When the size of the array is constant and won’t change
What is an oversize array?
Array where the number of elements used is less than or equal to the memory allocated
What variable do you need for an overssize array?
Need variable for how many array elements are currently used, the current size
When is an oversize array appropriate?
When size of the array is not known in advance or it varies over time
What are the three variables typically used for oversize array?
Array reference, current size, and constant variable for the array capacity (which is put into array reference size)
When using methods for oversized arrays, what must be used as parameters?
array reference and current size
If a method returns an array reference, what type of array must it be an why?
Must be perfect size array because an oversize array must return the current size of array (and method can only return one thing)
If a method has no parameter for the size of the array, what array must it be and why?
Must be perfect size array. Oversize arrays need this parameter because it must be updated in the method
For printing an oversize array, what is the expression of the for loop?
i < arraySize
(use the variable for size not .length)
When updating an oversize array in a method, what should you first check? What would the code look like?
First check if unused element is available in array reference
if(currentSize == arrayRef.length) → return currentSize
When adding array element to an oversize array, what would the assigning process look like?
arrayRef[currentSize] = element;
++currentSize
When adjusting the size in a method, what should you do to the size variable in main?
Assign it to the method!!
ex. currentSize = methodName(…)
For an oversize array, what is the return type in a method?
Int
(because it returns the array size)