Array




An Array sets aside several storage locations (memory locations) sequentially in memory (RAM). The storage locations must all be able to hold the same type of data. You can think of the storage locations as boxes that are side by side, where each box can hold a data value (int, double, boolean, String, etc.). You may want to think about mailboxes, where each mailbox has a unique number to identify it (index). The way you define the array determines what type of data can be stored in each storage location. Although an array object has data elements (the boxes and a length attribute), the array object does not have any methods of it's own. Each value of the array object is called an element or data element. We refer to the array object using a reference variable. The reference variable holds the starting location (memory address) of the array object.

Examples:

// The following definition sets aside 5 storage locations
// where each storage location holds an int.

int [] list = new int[5]; // sets aside 5 storage locations that hold an int



// All memory locations are initially set to zero.
// The reference variable list refers to the 5 storage locations and holds the
// memory address of where these 5 storage locations exist.
// Each storage location is indexed.
// The indexes for each storage location are 0, 1, 2, 3, and 4 in this example

// You can reference an individual storage location using
list[index]
// where index is the index of the storage location.
// The reference variable list holds the starting memory address of the 5 storage locations,
// and with the given index, the computer can calculate the actual location of the
// indexed position.


// Here are several instructions executed in sequence

list[0] = 100; // stores the value 100 in the 0 position of the array.

list[1] = 95; // stores the value 95 in the 1 position of the array

System.out.println(list[1]); // displays the value stored in the 1 position (95)

list[2] = list[0] + list[1]; // stores 195 in the 2 position of the array

list[1+2] = list[2]; // stores 195 in the 3 position of the array


You can also define the array and assign beginning values all in one step.
int [] list = {100, 95, 195, 195, 0};




You can find the number of boxes or data elements in the array object by accessing the length attribute.
list.length


// You can also declare the array reference variable
// and then create the array object later in your code.

int [] list;
// possibly more code (instructions)
list = new int[5]; // creates the array object with 5 boxes
list[0] = 100;
// etc.
// note that you must have your reference variable refer
// to an array object before accessing
// any storage locations.

// new int[5] is the instruction to create the array object in memory.
// before this instruction is executed, there is no array object in memory
// if you try to refer to a storage location before the array exists in memory
// you will have a runtime exception occur (an index out of bounds exception)



// You can use loops to traverse the array elements.
// The following example would print out all the elements of the array



for (int i=0; i<5; i++)
    System.out.println(list[i]);


You may also use list.length instead of 5.



More definitions:	
	
double [] myDoubleList = new double[10];

boolean [] list2 = new boolean[20];

String [] myStrings = new String[100];