Alice in Action with Java
Chapter 12
Arrays and Lists in Java
Arrays Are Data Structures
• An array is a data structure that occupies a
contiguous group of memory locations.
• Each element of an array must be of the same data
type.
• When an array is declared, it must be given a name
and a type (of primitives or of objects).
Alice in Action with Java
2
Name of array
(note that all
elements of this
array have the
same name, c)
Index (or subscript) of
the element in array c
c[ 0 ]
-45
c[ 1 ]
6
c[ 2 ]
0
c[ 3 ]
72
c[ 4 ]
1543
c[ 5 ]
-89
c[ 6 ]
0
c[ 7 ]
62
c[ 8 ]
-3
c[ 9 ]
1
c[ 10 ]
6453
c[ 11 ]
78
A 12-element array.
Alice in Action with Java
3
• Subscript
– Also called an index
– Position number in square brackets
– Must be integer or integer expression
a = 5;
b = 6;
c[ a + b ] = 2;
• Assign 2 to c[ 11 ]
• Subscripted array name is an lvalue
Alice in Action with Java
4
• Examine array c
– c is the array name
– c.length accesses array c’s length
– c has 12 elements ( c[0], c[1], … c[11] )
• The value of c[0] is –45
• The brackets ([]) are in highest level of precedence
in Java
Alice in Action with Java
5
Declaring and Allocating Arrays
• Declaring and Allocating arrays
– Arrays are objects that occupy memory
– Allocated dynamically with operator new
int c[] = new int[ 12 ];
– Equivalent to
int c[];
c = new int[ 12 ];
// declare array
// allocate array
• We can allocate arrays of objects too
String b[] = new String[ 100 ];
Alice in Action with Java
6
Array Indexing
The first element in a Java array is the zeroth
element.
The 1st element of array test is test [0]
The 3rd element of array test is test [2]
The number in the brackets is called the index (or
subscript)
Alice in Action with Java
7
Populating an Array
Method 1: Populate the array when it is declared
int loans [ ] = {39,44,45,65,72,93,14,55,67,27,46,82};
String monthNames [ ] =
{“Jan”,“Feb”,“Mar”,“Apr”,“May”, “Jun”,“Jul”,
“Aug”,“Sep”,“Oct”, Nov”,“Dec”};
Alice in Action with Java
8
Populating an Array
Method 2: Use some sort of a loop and populate
the
array as the program executes.
int temp = new int [ 10 ];
for (int i = 0; i < temp.length; i++ )
temp[ i ] = 2 * i;
Alice in Action with Java
9
Java Arrays
• General pattern for defining (declaring) an array
– Item[] anArray = new Item[N];
• Item: specifies the items type
• Brackets tell compiler that anArray is an array handle
• new operator: allocates memory for the array
• N: an integer that specifies the array length (size)
• An example of an array definition: double[]
readings = new double [NUM_READINGS];
– NUM_READINGS is an integer constant = 5
– readings is a handle to a 5 unit double type array
Alice in Action with Java
10
Java Arrays (continued)
• Element: indexed unit variable in an array
• Item: value stored in an array element
• Items are initialized to default values for array’s type
– Example: default value for item in double type is 0.0
• Creating an array parameter
– Place brackets between parameter’s type and its name
– General form: public ReturnType methodName(
Item[] parameterName ){…
– Example: public static double
average(double [] anArray)
Alice in Action with Java
11
Java Arrays (continued)
Alice in Action with Java
12
Java Arrays (continued)
• Components needed to access an array’s elements
– Array’s handle, item’s index, subscript operator ([])
• Pattern for accessing an element: anArray[i]
– anArray is the handle to the array
– i: index value, which must be a non-negative integer
– An index value out of bounds throws an exception
• Example of an array access: readings[0]
– Accesses the first element of the readings array
– Note: index is off by one relative to item’s cardinal order
• length property: returns number of items in an array
Alice in Action with Java
13
Java Arrays (continued)
• for loop provides convenient access to an array
• Example of an array traversal using a for loop
– for (int i = 0; i < arr.length; i++){
System.out.println("Reading #" +
(i+1) + ": " + arr[i]);}
• for each loop
– Special loop used to read each item in an array
– Limitation: cannot be used to write to array’s elements
• Example of array traversal using a for each loop
– for (double item : anArray){sum += item;}
Alice in Action with Java
14
Arrays and Memory
• An array is a random access data structure
– Contiguous elements are accessed in constant time
• Subscript operation computes an element’s address
• How to compute the address of an array element
– Multiply the index i by the size of an item
– Add the resulting product to the starting address
– Example: anArray[4]= (anArray+4*itemSize)
• Insertion and removal are linear time operations
– Up to length–1 items are shifted in each operation
– Many of these operations can slow down a program
Alice in Action with Java
15
Arrays and Memory (continued)
Alice in Action with Java
16
Arrays and Memory (continued)
Alice in Action with Java
17
Multidimensional Arrays
• Dimension: axis used to specify element’s location
• length determines space for one-dimensional array
• You can create arrays with multiple dimensions
– Example: two-dimensional array to model a table
• How to declare an N-dimensional array handle
– Use N pairs of brackets to declare
• Example of declaring a two-dimensional array
– private double [][] myTable = null;
Alice in Action with Java
18
Multidimensional Arrays (continued)
Alice in Action with Java
19
Multidimensional Arrays (continued)
Alice in Action with Java
20
Multidimensional Arrays (continued)
• Defining a multidimensional array with default values
– Use the new operator and specify dimensions
– Ex: myTable = new double[rows][columns];
• Defining multidimensional array with non-default values
– Use an initialization list
• Accessing a multidimensional array element
– Use N subscript operators for N dimensions
– Example: myTable[row][col] = item;
• Processing multidimensional arrays
– Use N for loops to traverse array with N dimensions
Alice in Action with Java
21
Multidimensional Arrays (continued)
Alice in Action with Java
22
© Copyright 2026 Paperzz