// APS101, Winter 2009: Lecture 23 (Mar. 9)
//
// Review: last time we looked at nested loops and tracing code.
           make sure you know how to do this for the final exam.

// so far in the course we've talked about various control structures:
// sequences of statements;
// conditionals (if-statements);
// loops (for and while)

// we've also talked about data structures: 
// primitive data types (int, double, char);
// classes (String, JFrame, Integer, etc.)

// A very useful data structure for storing information is an array.
// Arrays can store many elements of the same type (0, 1, or even 1000000).
// You can create them on the fly - for example, in the Band class, we
// might want to have a different number of musicians (1, 2, 3, or even 20);
// so, instead of declaring 20 Musician variables, we just define 1 Musician[] variable
// that holds the appropriate number of musicians.
// Also, there are situations when we don't know in advance how many elements
// we need (e.g. the user may tell us), so we cannot declare that many varaibles
// in our code in advance!

int x = 7;
int y = 3;
int[] a; // declare a to be an integer array
a // notice that it's null - means that an array is an object
a = new int[3];
a
a[0]
a[1]
a[2] // use indices to access the variables stored in an array
a[0] = 3; // assign a new value to the variable at index 0 in the array
a[0]
a[1] = 4;
a[2] = 2;
a[1] = "TEST"; // what's the problem? this is one drawback of arrays. 
a[1]  
a[3]  // error!
a.length() // error!
a.length // a public variable
a.length = 7; // can't reassign it: it's "final"

// int[]: read it as "integer array" or "array of ints"
// a is an integer array with 3 elements

int[] b = new int[]{5, 12, 7};
b[0]
b[1]
b[2]

// alternate syntax:
int[] b2 = {2, 3, 5, 9, 234}  
b2.length
b[4]

// another drawback of arrays:
// you have to specify the length in advance
// (or specify the contents of the array... but you're also specifying
//  the length when you do that)

// elements of an array can be objects (but all of the same type).
// let's create an array of JFrames (compare this to an array of ints)
import javax.swing.JFrame;
JFrame[] j = new JFrame[4];
j
j[2] // once the JFrame array is created, all elements get their default values - for objects it's null!)
j[2] = new JFrame();
j[2]
j[0]  
j[1]
j[3]
j[2].setVisible(true);
j[2].setTitle("Element at position 2");

// let's create an array of 2 JFrames and initialize it at the same time (see how we did this with int arrays)
JFrame[] j2 = new JFrame[]{new JFrame(), new JFrame()};
// you could also do it more efficiently: JFrame[] j2 = {new JFrame(), new JFrame()};
j2[0].setVisible(true);
j2[1].setVisible(true);

// let's write a method that displays the contents of an array from index i to j inclusive
// (this is similar to String's substring method)
int[] a = {1, 2, 3};
ArrayDemo.displayArray(a, 0, 3) // error! remember index j is inclusive!
ArrayDemo.displayArray(a, 0, 2)
ArrayDemo.displayArray(a, 0, 0) // display first element
ArrayDemo.displayArray(a, 2, 2) // display last element
ArrayDemo.displayArray(a, 1, 1) // display middle element
ArrayDemo.displayArray(a, 0, a.length) // error!
ArrayDemo.displayArray(a, 0, a.length - 1) // display all the elements