Session 5 - More Data Types and Operators |
Arrays are objects. The new operator is used to create objects, including arrays.
base_type array_name[] = new base_type[size];
The size of an array is available through its static instance variable: array_name.length.

min and max: -978 100123
You can initialize an array without using the new operator.

A runtime error occurs if you try to access an non-existing index.

Sorting an array with the Bubble Sort algorithm.

Two-dimensional array

An irregular or ragged array

Initializing multiple dimensions of a multi-dimensional array.

Because an array is an object, assignment of values can occur by reference.

Using the length member, which represents the number of elements the array can hold. For loops often use the length of an array as the conditional test. This ensures each element is accessed exactly one time.

Because an array is zero-based, the last element (whether populated or not) is at the index that corresponds to array.length - 1.
With J2SE 5, it is no longer necessary to use array length to process each index exactly one time.

Getting the average value, searching, finding minimum or maximum value: all these require processing all the elements in an array.

Strings are objects.
Let's review this example:

Three ways to construct a String:

The String class provides APIs for operations.

The equals method checks for the same sequences of chars. If so, the two
strings are equivalent.
If you use == to compare two strings, you are checking for identity, two
references to the same string object.
An array can store String objects.

Using the substring property of a String.


The ternary operator is a terse way of handling if/else logic. You ask a
question that can be answered true or false. Depending on the answer, something
happens or not.
Here, we ask if i equals zero. If so, we execute the if statement. If not, we
skip the if statement execution and avoid an error.

END OF LECTURE
=================================================================================================
Let's look at this example:


Sample output is:
Enter the average number of days
on vacation in Hawaii 5.5
Enter the average number of coconut drinks people on vacation in Hawaii consume
each day 3.33
The average number of days on vacation in Hawaii : 5.5
The average number of coconut drinks people consume each day: 3.33
18.315