Lec 8: Arrays Flashcards
Syntax for declaring an array
datatype[] arrayRefVar;
Syntax for declaring an array of a specific size
dataType[] arrayRefVar = new dataType[arraySize];
ex: double[] myList = new double[10];
Syntax for getting the length of an array
array.length
What is the default value of a bool if not preset
false
what is the default value of a char if not preset
‘\u0000’
it is an invisible character
syntax for creating an array with specific double elements
double[] myList = new double[4]
myList[0] = 1.9;
myList[1] = 2.1;
or
double[] myList = {1.9, 2.9, 3.4, 3.5};
What is the syntax for a foreach loop that prints all the elements in an array of double values
for (double value : myList){
System.out.println(value);
}
Does list 2 = list 1 create a new list?
No, it only copied the reference to list 2
How to copy the elements of an array to another
you must copy the array’s individual elements to the other array
When passing an array to a method, what is passed to the method?
the reference of the array
What is the name of an array without an explicit reference variable?
anonymous array
True or false” For a parameter of a primitive type, the actual value is passed instead of a reference
True
True or false: Changing the value of the local parameter inside the method changes the value of the variable outside the method
false
True or false: for a parameter of an array type, the reference value is passed
True
True or false: Any changes to the array that occur inside the method body does not affect the original array that was passed as the arg
False
What is a heap
a region of memory used for dynamic memory allocation, where all Java objects are stored when they are created with the new keyword
The java virtual machine stores, for example a new array, inside the heap
What defines a ragged array?
An array with different row lengths
True or false: int a[] = new int[2]; is correct
True
True or false: int a = new int[2]; is correct
False
True or false: int[] a = new int(2); is correct
False
True or false: int[] a = new int[2]; is correct
True
What is the correct term for numbers[99]?
indexed variable
Define indexed variable
Any variable that is accessed through the index of an array
What method copies the sourceArray to the targetArray?
System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);