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