Glossary
ArrayIndexOutOfBoundsException
A runtime error that occurs when a program attempts to access an array element using an index that is outside the valid range of indices for that array.
Example:
Trying to access myArray[5] when myArray was created with new int[5] (meaning valid indices are 0-4) will result in an ArrayIndexOutOfBoundsException.
Arrays
A data structure used to store a fixed-size sequential collection of elements of the same data type. In Java, arrays are reference types.
Example:
To store the high scores of 5 players, you could use int[] highScores = {980, 1020, 875, 1100, 950}; where highScores is an array.
Bracket notation
The use of square brackets `[]` with an index inside to retrieve or modify a specific element at a particular position within an array.
Example:
To get the second element from an array int[] scores = {100, 95, 80};, you would use scores[1] with bracket notation, which would return 95.
Constructor (for arrays)
A specific syntax used to initialize an array by specifying its data type and the number of elements it will hold, allocating memory for the array.
Example:
double[] dailyTemps = new double[7]; uses a constructor to create an array capable of holding 7 double values, all initialized to 0.0.
Fixed size
A characteristic of arrays meaning their capacity to hold elements is determined at the time of creation and cannot be changed later.
Example:
An array created with new boolean[5] has a fixed size of 5 elements; you cannot add a sixth element without creating an entirely new, larger array.
Pre-initialized arrays
A method of creating an array where elements are directly provided within curly braces at the time of declaration, automatically setting the array's size and initial values.
Example:
String[] weekdays = {"Mon", "Tue", "Wed", "Thu", "Fri"}; creates a pre-initialized array named weekdays with five String elements.
Reference types
In Java, a data type that stores a memory address pointing to an object, rather than the actual data value itself. Arrays are a prime example of reference types.
Example:
When you declare String[] names;, names is a reference type variable that will hold the memory address of an array of String objects, not the strings themselves.
Zero-indexed language
A programming language characteristic where the first element in a sequence (like an array or string) is accessed using an index of 0, rather than 1.
Example:
In Java, if you have an array char[] alphabet = {'a', 'b', 'c'};, the character 'a' is at index 0, demonstrating that Java is a zero-indexed language.
arrayName.length
An instance variable (not a method) that returns the total number of elements an array can hold, representing its fixed size.
Example:
If String[] fruits = {"apple", "banana", "cherry"};, then fruits.length would evaluate to 3, indicating the arrayName.length.
import java.util.Arrays;
A Java import statement required to use utility methods provided by the `Arrays` class, such as sorting or printing array contents.
Example:
To easily print an array's contents for debugging, you might use System.out.println(Arrays.toString(myArray)); which requires the **import java.util.Arrays;** statement at the top of your file.