Unit 4.3: Array Creation and Access
An array is a data structure used to store multiple values of the same type. It allows collections of related data to be represented using a single variable rather than multiple variables.
Array Elements
The values stored in an array can be either primitive values (such as int or double) or object references (such as String). All values in a single array must be of the same type.
- Arrays are homogeneous; once declared, an array can only hold values that match its specified type.
Array Length
The length of an array is established at the time of creation and cannot be changed.
- Unlike a ArrayList (you will learn later) that can grow, an array object has a fixed size. The
lengthattribute provides the number of slots available in that specific object.
- The
lengthAttribute: The length of an array can be accessed through thelengthattribute (e.g.,myArray.length). - Note:
lengthis an attribute, not a method, so it does not use parentheses.
Creating Arrays with new
When an array is created using the keyword new, all of its elements are initialized to the default values for the element data type.
| Element Data Type | Default Value |
|---|---|
int |
0 |
double |
0.0 |
boolean |
false |
| Reference Type | null |
- Java automatically wipes the memory for a new array and sets every slot to a safe "zero" state based on the type.
Task: Creating an array of Strings using the new keyword.
// In the main method ...
// Creates an array that can hold 4 String references, all initialized to null
String[] favoriteFruits = new String[4];
Initializer Lists
Initializer lists can be used to create and initialize arrays in a single statement.
- Use curly braces
{}to provide initial values. The compiler automatically determines the size of the array based on the number of items provided.
Task: Creating and initializing an array in one line using an initializer list.
// In the main method ...
// Creates an array of length 4 with the specified values
String[] favoriteFruits = {"mango", "banana", "apple", "grapes"};
Indexing: Access and Modification
Square brackets [ ] are used to access and modify an element in a 1D array using an index.
- The first element is always at index
0. The last element is always at indexlength - 1.
Task: Accessing and modifying specific array elements.
// In the main method ...
String fruit = favoriteFruits[0]; // Accesses "mango"
favoriteFruits[2] = "cherry"; // Changes "apple" to "cherry"
Index Bounds
The valid index values for an array are 0 through one less than the length of the array, inclusive.
- Java checks every array access. Using an index that is negative or
>= lengthwill immediately crash the program with ArrayIndexOutOfBoundsException.
- The Exception: Using an index value outside of this range will result in an
ArrayIndexOutOfBoundsException.
Task: Demonstrating an out-of-bounds error.
// In the main method ...
String[] favoriteFruits = {"mango", "banana", "apple", "grapes"}; // Valid indices: 0, 1, 2, 3
System.out.println(favoriteFruits[4]); // RUN-TIME ERROR: ArrayIndexOutOfBoundsException
Recall from Unit 1.1: You may have encountered this error earlier when learning about run-time errors or handling user input. Just as accessing a program argument that doesn't exist throws this exception (as seen in ExceptionExample in Unit 1.1), accessing an invalid array index here causes the exact same issue.