Return to lecture notes index

15-100 Lecture 19 (Wednesday, October 25, 2006)

Introduction to Arrays

So far, whenever we've wanted to have a collection of objects, we've had to hardcode declare each of the objects individually, and reference each individually. There's been no relation between the members of the collection, and they cannot be referenced as a set. We had had no way of iterating through them. They are not linked. There was no way to automatically make an x number of Objects.


   String s1;
   String s2;
   String s3;
   String s4;

Random Access collection: want to make the 1 through 4 a referance intead of part of the reference variable's name. It would be the index.

Today, we're going to learn a data structure that can hold a collection of Objects or primitives - the array. An array of four Strings can be created as follow:


   String[] names;
   names = new String[4];

'names' is a reference variable to an array of Strings, not to an individual String. The "String []" is the type of object that "names" can refer to. The second line actually creates the array of 4 Strings, and assigns it to the reference variable. Note that you must specify a size of the array when you create it. This size is the number of cells in the array; the max number of Strings that the array 'names' can hold. Note that we use brackets in order to declare an array, it's size, and to reference the index (see below).

Because Strings are Objects, not primitives, names is actually a reference to an array of String references, not actual Strings. In order to access these individual String references, we use brackets after the array name to specify an index into the array. For example, to access the element of 'names' at index 0, we would use the syntax, "names[0]".

Java (like C and C++) indexes arrays starting with 0 for the first index. So for an array of size 3, the indexes range from 0 to 2. This is important to remember, because if you try to refer to the first element of the array by accessing names[1], you're actually getting the SECOND element, and if you try to access the last element by accessing names[3], an ArrayOutOfBoundsException will be thrown, because Java knows this element does not exist!

The array 'names' now looks like this:


             --------------------------------------------
  names -->  |   null   |   null   |   null   |  null   |
             |          |          |          |         |
             --------------------------------------------
               names[0]    names[1]  names[2]   names[3]

All four elements of the array (names[0], names[1], etc.) are references to Strings, but we haven't actually created the Strings and assigned them to the references yet! Therefore, currently, each index of the array stores "null".

To initialize these Strings, we would have to do the following for names[0], names[1], names[2] and names[3] (our four String references):


   names[0] = "Greg";

Once we've done this for names[0], the array looks like this:


               names[0]    names[1]  names[2]   names[3]
             --------------------------------------------
  names -->  |    *     |   null   |   null   |  null   |
             |    ||    |          |          |         |
             -----||-------------------------------------
                  ||
                  \/
             +----------+
             |  "Greg"  |
             +----------+

There is also a variable for all arrays called length that is publicly accessible. The line "System.out.println(names.length);" will print out 4 because that is the size that we made our array. Note that since we are calling on a public variable and not a method there are no parentheses after length.



import java.util.*;
import java.io.*;


class ArrayStuff {

  public static void main (String[] args) {
    Scanner keyboard = new Scanner (System.in);
    String[] names;
    
    
        
    System.out.print ("How many names? ");
    int numberOfNames = keyboard.nextInt();
    names = new String[numberOfNames];
    keyboard.nextLine(); // Throw away newline after number
    
    for (int nameIndex=0; nameIndex < numberOfNames; nameIndex++) {
      System.out.print ("Name: "); 
      names[nameIndex] = keyboard.nextLine();
    }
          
    System.out.println ("");
    System.out.println ("Names in reverse:");
          
    //Prints out the names in reverse order they were put in
    for (int nameIndex=(numberOfNames-1); nameIndex >=0; nameIndex--) {
      System.out.println (names[nameIndex]);
    }
    //Prints out the length of the array we created (numberOfNames
    System.out.println("Just printed " + names.length + " names.");
    
    
    
  
  }
  
}