Lesson 5A: Arrays

An array is a type of variable, but instead of containing a single value, an array can contain many values stored in separate elements. It is useful when we want to store similar data using only one variable.

An array stores a sequence of values of the same data type.

Examples of data that have different values but of the same type:

Exam scores, Stock prices, Grades


We want to store these values, but also access each individual value. If we have N values, then we can think of these values as being indexed from 0 to N-1. By indexing each value, we can directly refer to the stored value by its index.

An array called “var” that has 10 elements or capacity to hold 10 values.




int var1 = 5;
int var2 = 6;
int var3 = 7;
int var4 = 8;
int var5 = 9;


Can be replaced by declaring a 1-dimensional array with 5 elements:

int[] var = new int[5]; // 1-dimensional array with 5 elements
var[0] = 5; // set element 0 to be “5”
var[1] = 6; // set element 1 to be “6”
var[2] = 7; // set element 2 to be “7”
var[3] = 8; // set element 3 to be “8”
var[4] = 9; // set element 4 to be “9”


int[] var = {5, 6, 7, 8, 9 }; // 1-dimensional array initialized with 5 elements



Arrays allow you to group together similar data while referring to one variable name. The data in the variables are accessed by its index number.

Three steps to creating an array in Java:

  1. Declare the array name and data type
  2. Create the array
  3. Initialize the array values


double[] a;
a = new double[N];
for (int i = 0; i < N; i++) {
  a[i] = 0.0;


In-Class Exercise:

Write a small program that computes the dot product of two vectors x[N] and y[N}, where N = 3. The dot product is the sum of the products of their corresponding components.

Two arrays, x[] and y[], each of length 3 (or having 3 elements). The dot product is the expression:

x[0]*y[0] + x[1]*y[1] + x[2]*y[2]

values to use:

x: 0.30, 0.60, 0.10
y: 0.50, 0.10, 0.40

public class DotProduct {
  public static void main(String[] args) {
    int N = 3;
    double[] x = new double[N];
    double[] y = new double[N];

    x[0] = 0.30;
    x[1] = 0.60;
    x[2] = 0.10;

    y[0] = 0.50;
    y[1] = 0.10;
    y[2] = 0.40;

    for (i = 0; i < N; i++) {
      sum += x[i] * y[i];


Zero-based indexing

Notice that we always refer to the first element in the array as var[0], the second element as var[1], the third element as var[2], and so on. The index always starts at zero (0) and is the convention used in most modern programming languages.

BE CAREFUL: Misunderstanding this convention leads to “off-by-one-errors” that are difficult to debug.


Array Length

Once the array has been created, the array size is fixed and cannot change. Arrays are created at runtime and not at compile time. The Java compiler does not know how much space (or memory) to reserve for the array at compile time.

By convention, we can keep the size of an array in a variable whose value can be changed at runtime.

Java has a standard mechanism to refer to the length of an array a[] with the code:



[Question]: How do you refer to the last element of the array a[] if you do not know the length]


a[a.length = 1]


Memory Representation

Arrays have a direct correspondence with the memory systems on all computers. The elements of an array are stored consecutively in memory so that it is easy to quickly access any array value.

Memory on modern computers uses hardware as a sequence of indexed memory locations that can be accessed with an appropriate index. We can view memory as a giant array. When referring to computer memory, we refer to a locations index as its address.


Memory Allocation

When you use the keyword new to create an array, Java reserves space in memory for it. This process is called memory allocation. All variables use the same process of memory allocation.

You must always use the keyword new to allocation memory for an array before accessing any of its elements. A compile time error “uninitialized variable error” will occur, if not.

error: variable a might not have been initialized

Java automatically initializes all of the values in an array when the array is created. The time required to create an array is proportional to the length of the array you are creating.


Bounds Checking

Be careful when referring to the elements of an array. Accessing an element in an array that is greater than the length of the array, or less than 0 (negative index), will create an ArrayIndexOutOfBounds runtime exception.

Legal values of an array are 0 to N-1, so your program must only access values of an array between these bounds.

These buffer overflow conditions are not checked by the system and occur during the execution of the program. Buffer overflow conditions can be exploited by hackers to take control of a system.


Setting Array Values at Compile Time


String[] suit = { “Hearts”, “Spades”, “Diamonds”, “Clubs” };


String[] rank =
  “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”,
  “Jack”, “Queen”, “King”, “Ace” 

Initializing the values of an array at compile time makes sense if the size of your array is not too large. Notice that the above code does not use the new keyword. Doing so implies array creation, so the new keyword is not needed.


Setting Array Values at Runtime

A more typical approach is when we wish to compute the values to be stored in an array.


String[] deck = new String[suit.length * rank.length];
for (int i = 0; i < suit.length; i++) {
  for (int j = 0; j < rank.length; j++) {
    deck[rank.length * i + j] = rank[i] + “ of “ + suit[j]