Unit 6 - Array Flashcards
Array
Data structure used to implement a collection or list of primitive or object reference data
Element
single value in an array
Index of an element
position of element in an array
1st element has an index of
0
Length of array
number of elements in an array
is a public final data member of an array
Last element of an array is at index
list.length-1
When an array of ints is created with keyword new, each element is initialized to
0
Accessing an index that doesn’t exist causes an
ArrayIndexOutOfBoundsException
Elements of a reference type are initialized to
reference value null
Objects are not automatically created
Elements of a double type are initialized to
0.0
Elements of a boolean type are initialized to
false
Initializer list
Used to put values into an array during initialization
Initialize an array of type boolean with 4 elements
boolean [ ] listOne = new boolean [4]
data type [ ] name = keyword new data type [number of elements]
Initializer lists structure
data type [ ] name = { parameters separated by commas }
Example
double [ ] grades = {70.5, 88.2, 93.7, 98.7}
Use of array objects allows multiple related items to be represented using
a single variable
Square brackets are used to
access and modify an element in a 1D array using an index
Valid index values
0 to list.length-1
How to retrieve a value at index x from an array
name [index x]
Traversing
Access each element of an array
Can be done by iteration statements
A for loop can also be
used to access some of elements
Be sure to check LCV initialization, conditions, and increment
When using loops to access array elements, we need to be careful to prevent an
ArrayIndexOutOfBoundsException
Enhanced for loops
Also known as a for-each loop
Only two components separated by a colon
What two components are in the parameter list of an enhanced for loop
first component -> type & name of variable that is a copy of value stored in structure
second component -> data structure being traversed with the loop
Enhanced for loops are able to
access the value stored in variable
Enhanced for loops are unable to
Assign into the variable defined in header
Don’t have access to indices of array or subscript notation
Structure of enhanced for loop
for (type declaration : structure) { // statement one; //statement two; // ... }
Algorithm to identify a max/min in an array
Method -> local variable needed to store current max/min values being compared to all values in array and assign value to be opposite extreme or first item
Loop -> standard or enhanced both work
-> compare current value against a local variable
currentValue is better, assign to temporary variable
Algorithm to calculate average value from objects in an array
Method -> local double variable is needed to store accumulated values
for loop -> traverse the array and add current total to variable
After accumulation need to divide total by number of items stored in array
Algorithm to shift array contents to the right
Make an empty array of the same size
Iterate over original array & properly copy values to adjusted index in new array
Assign new array back into original variable
Only use a standard for loop
Algorithm to shift array contents to the left
Shift using same array/ go to left with use of nested for loops
Outer loop -> execute # of times we shift
Inner loop -> copy value stored in first index then move all contents one spot left
Copy temp. variable back to end of array