Java Tutorial #15.1 / 15.2 — Introduction to and using arrays


Topics Discussed: Arrays, subscripts, iterating through arrays

Source Code Below

15.2

Topics discussed: Same as above

Source Code Available Here
Homework is here


Arrays
Arrays are the most basic multi-variable container made available in java. The syntax is pretty simple, but the implementation can be confusing if you don’t have a programming background. The basic specifications of an array is that it’s a basic, 0-indexed, template’d, data structure that can be created with either explicit values or in an empty state.


int anArray[] = new int[7]; // Creates an array that can hold 7 ints.
for(int i=0; i< anArray.length(); i++){
    anArray[i] = i; // sets the array equal to it's index based off the iteration in a loop.
}

Arrays can be iterated through easily with loops, especially by for loops.


The Subscript
The subscript is the way that we access a certain part of an array. anArray[0] would refer to the ‘first’ (remember, 0-indexed) location in the array. Therefore the subscript in this case would be 0. Changing the subscript from within a loop is fairly common practice in Object oriented programming.