Also see the Java Tutorial on arrays and for loops and this while link
Arrays
| ARRAY beatles: | John | Paul | George | Ringo | theMetamorphisisGuy |
| INDICES | 0 | 1 | 2 | 3 | 4 |
| ARRAY array1: | 42 | 15 | 74 | 6 | 32 | 150 | 724 | 66 |
| INDICES | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
You can declare arrays of any type
optional placement of []
in Java, the above shows:
What is array1[4]?
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.
Arrays have an attribute length. This can be usful in loops and if
statements.
if (table.length < 12) ... Suppose we have an array called grades:
Let's take our grades array and write a loop to find the highest. Here is a start: What is the i?
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:
You cannot create static arrays at compile time:
You can also declare and initialize non-rectangular arrays with
nested initializations
And a sample invocation of the method looks like:
total = sum(table);
Block Statements
Branching
if/else (must be Boolean: not 0 or 1)
Consider for some graphics method specify Class Variables:
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
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:
array1[1] = 15;
beatles[3] = "Ringo";
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]?
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.)
Suppose we have an array called table:
while (table.length < 12) ...
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
What else is needed if I want to return which student had the highest?
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.)
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?)
Passing arrays as parameters
A small method which accepts an array as a parameter:
Somewhere an array was instatiated:
int[] table = new int[24];
{}
some examples
Flow of Control
switch
Alternatively, sometimes on sees "constant" variables set:
Do loops
Example:
For loops
General