These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

JAVA01/Arrays

Classnotes | JAVA01 | RecentChanges | Preferences

Arrays that are variables that are grouped together under a commond name. Like variables, arrays are created by stating the type of the variable being organized into the array and the name of the array. The difference lies in the addition of square brackets "[]".

You can create arrays for any type of information that can be stored as a variable. For example, here is an array of integers:

 int[] numbers;

To reference elements of an array, you use an index number. The index number is like a reference lookup in a library; it points to a specific element inside of an array.

For example, if we had an array of integers called 'numbers' that had three elements in it; 3, 5, and 10 in that order, we would reference them thusly:

 numbers[0] would be 3
 numbers[1] would be 5
 numbers[2] would be 10

We have actually been using arrays all along. If you refer back to the general structure of a Java program, you will see that the main method actually takes an array of strings as a parameter (which is how we were able to accept options from the command line):

 class Hello {
      public static void main(String[] arguments) {
          // My first Java program goes here
      }
 }

In order to actually store items into an array, you need to define how long the array will be. You do this with the "new" operator. If I wanted to create an array of integers that was 250 elements long, I would do this:

 int[] numberArray = new int[250];

which would create an integer array called "numberArray" and assign 250 elements to it (0-249).

If I needed a String array with 50 elements, I would create it thusly:

 String[] names = new String[50];

You could also create a new array by assigning default values to it. For example:

 String[] reindeerNames = { "Dasher", "Dancer", "Prancer", "Vixen",
     "Comet", "Cupid", "Donder", "Blitzen" };

would create a string array 8 elements long and assign the above values to it. This would mean that

 reindeerNames[0] = "Dasher"
 reindeerNames[1] = "Dancer"
 reindeerNames[2] = "Prancer"
 .... etc.

Multidimensional Arrays

You can also create multidimensional arrays by adding additional brackets. For example, if I needed a 2-dimensional array that corresponded to a grid of lights (40x20) with each light either on or off, I could create an array for it like this
 boolean[][] lightGrid = new boolean[40][20];

I could then access the elements in this array in the expected way:

 lightGrid[1][13] = true;
 lightGird[22][18] = false;
 ... etc.

You can have multidimensional arrays in whatever dimension you want. It all depends upon your program need.

Finding the length of an Array

Sometimes (as in the main() declaration) you will not know the length of an Array. In order to find a length for an array, you use the length method:
 int[] array = new int[25]
 System.out.println("Array length is " + array.length);

which would display

 Array length is 25


Classnotes | JAVA01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited May 29, 2003 12:52 am (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.