5.12.1 Arrays


Arrays

An array is a collection of numbered variables, always numbered from 0 to n−1 for some nonnegative integer n. The number of a variable in an array is called its index.

All of the variables in an array have the same type. For example, you can have an array of ints, an array of doubles, etc.

T [ ]

If A is an array whose variables have type T, then A has type T [ ].

new T [n]

Expression new T [n] yields a new array of n variables. For example,
  int[] A = new int[10];
creates an array of 10 integer variables, numbered from 0 to 9, and stores that array into A.

A[i]

Expression A [i] refers to the variable at index i in array A. Be sure to remember that the indices start at 0. For example,
  int[] A = new int[10];
  A[0] = 1;
  A[1] = 3;
creates an array of integers called A and stores 1 into A[0] and 3 into A[1].

The index can be given by any expression that yields an integer (of any integer type). For example, A [2*k + 1] is allowed, as long as k is some type of integer.


A.length

If A is an array then A.length is the number of variables in A. For example,
  double[] A = new double[7];
  int      n = A.length;
makes n = 7.

Watch out: array bounds errors

Be sure not to use an index that does not exist. Performing
  int[] A = new int[10];
  A[10] = 0;
throws exception ArrayIndexOutOfBoundsException.


Partially filled arrays

Sometimes you use only part of an array. Think of the array as a hotel and the variables in the array as rooms. On a given night, not all of the rooms are occupied.

Usually, you arrange for the occupied part of the array to start at index 0 and go up to index s−1 for some integer s called the logical size of the array — s is the number of occupied array cells. The actual size of array A — the number of variables that the array contains, yielded by A.length — is called the physical size of A.


Example

This example uses a partially filled array. The following main method reads a list of integers from the standard input until it sees integer 0. Then it writes the integers to the standard output backwards.

import java.util.Scanner;

public class Example
{

  static int maxNums = 100;

  public static void main(String[] args)
  {
    int[] A = new int[maxNums];
    int i, howmany;

    //----------------------------------------------
    // Read the numbers into array A, stopping at 0.
    // Refuse to put too many values into the array.
    // howmany is the logical size of the array and
    // maxNums is the physical size of the array.
    //----------------------------------------------

    Scanner scan = new Scanner(System.in);    
    for(i = 0; i < maxNums; i++)
    {
      A[i] = scan.nextInt();
      if(A[i] == 0) 
      {
        break;
      }
    }
    howmany = i;

    //----------------------------------------------
    // Write the numbers out backwards.
    //----------------------------------------------
    
    for(i = howmany - 1; i >= 0; i--)
    {
      System.out.println(A[i]);
    }
  }  
}


Passing arrays as parameters to methods

Pass an array as a parameter the way you would any other type of parameter. Here is a definition a method that fills an array with zeros.


Exercises

  1. How do you refer to the variable at index k in array W? Answer

  2. What happens if you use an index that is not an allowed index for an array? Answer

  3. If array B is created by

      double[] B = new double[4];
    
    what are the allowed indices for B? Answer

  4. Write statements that create an array of 100 ints and store 0 into each variable in the array. Answer