Preview Java Syntax: Arrays and Loops

Also see the Java Tutorial on arrays and for loops and this while link

Arrays

ARRAY beatles:John PaulGeorge Ringo theMetamorphisisGuy
INDICES0 1 2 3 4

ARRAY array1:42 1574 6 32 150724 66
INDICES0 1 2 3 4 5 6 7

You can declare arrays of any type

optional placement of []

in Java, the above shows:
array1[1] = 15;
beatles[3] = "Ringo";

What is array1[4]?
What is beatles[3]?
What is array1[6]?
If x = 1, what is beatles[x], beatles[2*x], beatles[x + 1],
beatles[x++] (yep, tricky - does not add until after uses), beatles[++x]?
What is array1[8]?

Here is a graphical example of values being set in an array. Source

You can even build arrays of arrays:

Lab 7 deals with 2-D arrays

A 2-D example and its code

Array boundaries are checked at run-time to avoid overflowing a stack and corrupting memory.

Note:
however An array is an object (sorta). Array types are not classes, but array instances are objects. This means that arrays inherit the methods of java.lang.Object.
It is useful to consider arrays to be a separate kind of reference type from objects. (E.G., a "set" holding things)
However, arrays may be assigned to variables of type Object and the methods of the Object class may be invoked for arrays. (The method clone() can prove useful on occassion.)

Arrays have an attribute length. This can be usful in loops and if statements.
Suppose we have an array called table:

if (table.length < 12) ...
while (table.length < 12) ...

Suppose we have an array called grades:
Notice here we are passing an array to a method defaultFillGrade

This (above) code will give 2 (maybe 3) errors. What errors and why? hint

Let's take our grades array and write a loop to find the highest. Here is a start:

What is the i?
What else is needed if I want to return which student had the highest?

Consider what happens to code if the test has a curve...(hint - use actual member of an array to initialize)

For two dimensions we can get lengths of both dimensions:

Further discussion with graphic of how stored in memory: Probably more than you want to know.

To create an array, you can use one of two methods:
To make an empty array: (i.e., this only declares that I have an array of ints, it does not put anything in for their values...except that since they are ints, java might put the default of 0 in each place. Be aware, the default for any object other than primitive data types is null)

Or you can fill an array with its initial values (once made, you can not use this syntax to change the values): This is equivalent to: And, yes, this works for Objects as well. (I.e., you can list the Objects in {} to initialize the array of some type of object - as long as the Objects have been instantiated.)

You cannot create static arrays at compile time:

Nor can you attempt to fill an array without declaring the size of the array with the new operator: won't work.
will (what's the difference?)

You can also declare and initialize non-rectangular arrays with nested initializations

Passing arrays as parameters

A small method which accepts an array as a parameter: Somewhere an array was instatiated:
int[] table = new int[24];

And a sample invocation of the method looks like:

total = sum(table);

Block Statements
{}


some examples


Flow of Control

Branching

if/else (must be Boolean: not 0 or 1)

switch

expressions must be constants: int, byte, short or char (convertable via casting to int (automatic promotion) )

Consider for some graphics method specify Class Variables:

Alternatively, sometimes on sees "constant" variables set:

They are constant since final says no one can change them

If no breaks, drop-through (once match, executes all below)

Looping (while, do and for same as C, C++)

While loops

Do loops Example: For loops

Example a code fragment (would be a part of a method) that draws several lines and cycles the lines colors between red, blue and green:


General