Unit 6: Array Flashcards
an array is __.
a data structure used to implement a collection (list) of primitive or object reference data
an element is __.
a single value in the array
an array’s length ___.
cannot be changed after it’s been created
initializing an array:
dataType [] arrayName = new dataType[element 3]
ex. int [] array = new int[5];
NOTE: there must be a nonnegative integer or 0 in the [ ]
when just initializing an array’s num of elements, each element is __.
(name for each of 4 data types)
0 for int, 0.0 for double, false for boolean, null for String
how to set array element to value:
array[index] = value
ex. array[0] = 5;
how to initialize array with object elements:
how to set object values:
className [] arrayName; OR
className[] arrayName = new arrayName [num of elements];
arrayName[index] = new className(attribute);
how to make array with known values:
dataType [] arrayName = {element1, element2, …};
NOTICE curly braces
only use [] after array name when___.
accessing a certain element, not initializing the entire array
for: each loop structure:
for( dateType variable: array)
{
}
how to print out all elements of an array with for: each loop
for (dataType variable: array){
System.out.prtinln(variable);
}
NOTE: variable is a copy of what’s in the array, so don’t need to put index of variable