MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Unit 0: The First Program
  • Unit 1: Using Objects and Methods
  • Unit 2: Selection and Iteration
  • Unit 3: Class Creation
  • Unit 4: Data Collections
    • Part 1: Arrays
      • Ethical and Social Issues Around Data Collection
      • Introduction to Using Data Sets
      • Array Creation and Access
      • Array Traversals
      • Implementing Array Algorithms
      • Unit 4 Part 1 Slides
    • Part 2: ArrayList
    • Part 3: 2D Arrays and Recursion

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.

Typed Collections
  • 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.

Static Sizing
  • Unlike a ArrayList (you will learn later) that can grow, an array object has a fixed size. The length attribute provides the number of slots available in that specific object.
  • The length Attribute: The length of an array can be accessed through the length attribute (e.g., myArray.length).
  • Note: length is 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
Automatic Initialization
  • 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.

Inline Creation
  • 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"};
Array Indexing Diagram

Indexing: Access and Modification

Square brackets [ ] are used to access and modify an element in a 1D array using an index.

Zero-Based Indexing
  • The first element is always at index 0. The last element is always at index length - 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.

Boundary Enforcement
  • Java checks every array access. Using an index that is negative or >= length will 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.

Privacy Policy | Terms & Conditions