Arrays

Intro to Arrays

Array: List of data elements

Creating Arrays

  1. Arrays are objects, so it needs an object reference.
// Declare reference to an array to hold ints
int[] numbers;
  1. Create array and assign to object reference.
// Create new array to hold 6 ints
numbers = new int[6];

In one step (declaring reference and creating array object in one statement):

float[] temperatures = new float[100];

Array Size:

Accessing Elements of an Array

Array is accessed by:

// Array elements can be treated like any other variable
numbers[0] = 20;

Off-by-One Errors

It’s very easy to by off-by-one when accessing arrays and get ArrayIndexOutOfBoundsException runtime error.

Array Initialization

We can use an initialization list if only a few items need to be initialized, like so:

// Using an initialization list
int[] days = {
    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
// Using a bunch of statements
days[0] = 31;
days[1] = 28;
days[2] = 31;
days[3] = 30;
days[4] = 31;
days[5] = 30;

Alternate Array Declaration

All the following is valid:

int[] numbers;
int numbers[];
int[] numbers, codes, scores;
int numbers[], codes[], scores[];

Array Length

Arrays are objects that have a public constant field named length that can be accessed like so:

int l = temperatures.length;

Note: For other objects, length is usually a method, such as String’s length() method.

Enhanced for Loop

for (datatype elementVariable : array)
    statement;

Example:

int[] numbers = {3, 6, 9};
// Enhanced (read-only)
for (int x : numbers) {
    System.out.println(x);
}
// Traditional (read/write)
for (int i = 0; i < numbers.length; i++)
{
    System.out.println(numbers[i]);
}

Copying Arrays

How NOT to copy an array (copying by reference):

int[] x = { 1, 2, 3 };
int[] y = x;

How to copy an array (copying by value):

int[] x = { 1, 2, 3 };
int[] y = new int[x.length];
for (int i = 0; i < x.length; i++)
    y[i] = x[i];

Passing Arrays as Arguments

Like any other object reference variable, they are passed by reference

Comparing Arrays

== will only check whether two references point to the same array object.

Pattern: Partially Filled Arrays

One way to hold an unknown amount of data is to just make one big array and keep track of the end of the data with a counter variable.

Returning an Array Reference

A method can return a reference to an array.

Arrays of Objects

Arrays aren’t limited to primitive data, they can also hold objects.

E.g.,

Rectangle[] x = new Rectangle[7];
for (int i = 0; i < x.length; i++)
    x[i] = new Rectangle();

String Arrays

E.g.,

String[] names = { "Plutonia", "Doom2", "MyHouse" }

Remember!: An array’s length is a field. A String’s length() is a method.

Two-Dimensional Arrays

Two-Dimensional Array: An array of arrays.

Example:

int[][] x = new int[3][4];
     | col 0 | col 1 | col 2 | col 3 |
— | — | — | — | |
row0 | scores[0][0] | scores[0][1] | scores[0][2] | scores[0][3] |
row1 | scores[1][0] | scores[1][1] | scores[1][2] | scores[1][3] |
row2 | scores[2][0] | scores[2][1] | scores[2][2] | scores[2][3] |
x[2][1] = 95;
     | col 0 | col 1 | col 2 | col 3 |
— | — | — | — | |
row0 | 0 | 0 | 0 | 0 |
row1 | 0 | 0 | 0 | 0 |
row2 | 0 | 95 | 0 | 0 |

The ArrayList Class

Unlike an array, an ArrayList object automatically expands and shrinks when items are added and removed.

Imported with:

import java.util.ArrayList;

Created with:

ArrayList<String> nameList = new ArrayList<String>();
// Alternative: Declare with different capacity than 10
ArrayList<String> nameList = new ArrayList<String>(100);

Common methods: