Arrays and You

Purpose

The purpose of an array is to store data.  An array can store lots of data, but each piece of data must be of the same type. 
 

About Arrays

Arrays are variables just like Fonts and Colors are variables.  Therefore, arrays need to be declared and initalized just like a Font variable or a Color variable.  However, the syntax to declare and initalize them is different than a Font or a Color.

The steps are

  1. Declare the array.  Often this is done at the top of the program just after the "class *" line.
  2. Allocate space for the array.  This is often done with a "new" command in init().
  3. Use the array.  This can be done anywhere arrays are useful.

How to Declare Arrays

Arrays must be declared before they can be used.  They can be declared anywhere you can declare any other variable.  Normally this is done after the "public class foo" line and before the first function.  The general way to declare an array is to name the type of the array, then the name of the array, and finally the "[]" brackets.  For example Failure to declare an array will result in a compile time error message.

How to Initalize an Array

Memory must be allocated for an array before you can use the array.  Basically, the computer must be told how large the array is.  The computer will treat a 10 element array differently than a 10,000,000 element array.  If you try and make an array too big, the program will crash.  In practice, this almost never happens.  But a 10,000,000 element array might just do it.

Normally, array memory is allocated in the init() function.  Failure to allocate memory will cause your program to crash as it begins to run.  This can be a difficult bug to detect, so be careful.To allocate memory for the array, you assign to the array the value of "new" when given the arguments of the type and the number.   The type must be the same as when the array was declared. For example,

Remember that if you declare an array to be N big, then the elements are numbered 0 ... (N-1).  For example, age has elements 0 .. 9, score has elements 0...99, and lastnames has elements 0...9999.  If you try and access an element that does not exist, the program crashes at that point.  Such an error is called a "array indexing error" or an "outof bounds exception".  If your program seems to sudently stop working, suspect this error.

You also might want to give each element of the array in initial value.  Normally this is done in the init() function.  If you want each element to be zero or empty, then you need do nothing more.  But if you want a different starting value, then you must assign to each element of the array this different value.  For example

    for(i = 0; i < lastnames.length; i++) {
         lastnames[i] = "Unknnown";
    }

Array Examples

 

 

Find the largest element

// largest should be the same type as each array element
int largest = age[0];
for(index = 0; index < age.length; index++) {
     if (age[index] > largest) {
          largest = age[index];
     }
}

Put an element into the beginning of an array

// set element to be the thing to add
for(index = lastnames.length; index > 0; index--) {
    lastnames[index] = lastnames[index-1];
}
lastnames[0] = element;

Put an element into the end of an array

// set element to be the thing to add
lastnames[lastname.length-1] = element;

Printing an Array

// The "15" spaces the array 15 pixels apart per element
for(index = 0; index < age.length; index++) {
      g.drawString("" + age[index], 10, index * 15);
}

Printing an Array in Reverse

// The "15" spaces the array 15 pixels apart per element
for(index = score.length; index < 0; index++) {
      g.drawString("" + score[index], 10, (score.length - index) * 15);
}