Richard's J2SE Day 4, Lists, Logic and Loops

(P86) Arrays are a way to store a list of items that have the same primitive data type, the same class, or a common parent class.

To create a array in Java, do the following
1. Declare a variable to hold the array
2. Create a new array object and assign it to the array variable
3. Store information in the array

Creating Array Objects (P87)
Arrays are objects in Java, use new operator to create a new instance of an array.
String[] players = new String[10];

Array objects can hold primitive types
int[] temps = new int[99];

When creating an array object using new, all its slots are given a initial value (0 for numeric array, false for booleans, '\0' for character, and null for objects).

Create and initialize an array at the same time
Point[] markup = { new Point(1,5), new Point(3,3), new Point(2,3) };

Accessing Array Elements (P88)

testScore[40] = 920; // sets the 41st element of the testScore array

In Java, it is impossible to access or assign a value to an array slot outside the array's boundaries.

Changing Array Elements (P89)
An array of objects in Java is an array of references to the objects. (move values inside arrays = reassigning the reference).
Arrays of a primitive data type, e.g. int, float, do copy the values, as do elements of a String array, even though they are objects.
(P89) HalfDollars.java

Multidimensional Arrays (P91)
Java does not support multidimensional arrays, but you can achieve the same functionality of declaring an array of arrays.

E.g. A 52-element array in which each element contains a 7-element array:
int[][] dayValue = new int[52][7];

if Conditionals (P93)
if (blindDateIsAttractive == true)
restaurant = "Benihana's";
else
restaurant = "Taco Bell";

// if conditionals in Java requires the test to return a Boolean value, in C and C++, the test can return an integer.

switch Conditionals (P94)
switch (grade) {
case 'A':
System.out.println("Great job!");
break;
case 'B':
System.out.println("Good job!");
break;
default:
System.out.println("cheating!");
}

// tests and values can only be simple primitive types that can be cast to an int. Cannot use larger primitive types such as long or float, strings or other objects within a switch, nor can you test for any relationship other than equality.

Without a break statement in a case section, after a match is made, the statements for that match and all the statements farther down the switch are executed until a break or the end of the switch is found.
(P97) DayCounter.java

for Loops (P99)
for (initialization; test; increment) {
statement; }

(P101) HalfLoop.java

while and do Loops (P102)
while (i<10) {
x = x * i++;
}

(P103) CopyArrayWhile.java

long i=1;
do {
i*=2;
System.out.print(i + " ");
} while (i < 30000000000L);


Breaking Out of Loops (P105)

int count = 0;
while (count < array1.length) {
if (array1[count] == 1)
break;
array2[count] = (float) array2[count++];
}


// continue keyword starts the loop over at the next iteration
int count = 0;
int count2 = 0;
while (count++ <= array1.length) {
if (array1[count] == 1)
continue;
array2[count2++] = (float)array1[count];
}


Labeled Loops (P105)
out:
for (int i = 0, i<10; i++) {
while (x < 50) {
if (i * x++ > 400)
break out;
// inner loop here
}
// outer loop here
}


(P109) ex1DisplayDays.java