In this lab, we will work with arrays.
An array is a data structure. It is an ordered collection of objects or primitive data elements. You access a specific element of an array by stating the collection name followed by the specific element's index number in that collection.
In Java, arrays are objects. To create an
array you create an object. The syntax for creating an array object
is slightly different from that used for other objects. To create an
array of 5 int
's called
tom
eu, you would type:
int[] tom
= new int[5];
On the left-hand side of the assignment
operator, int[]
is the data type
array of int and tom
is
the reference to (i.e., the name of) the array object. On the
right-hand side of the assignment operator, the operator
new
is being used to create an array object
containing a collection of ten (the number in square brackets) int
variables.
Each element in the array has a index number associated with it. The first element is at index number 0. Each element after that is numbered sequentially, up to the last element. This means that the last element in the array has an index number of the size of the array minus 1. You can almost think of each element in an array is a variable. Because you access the "variables" of an array via an index number, there are some pretty powerful tricks you can do with arrays.
If you wanted to set 5 int
variables equal to 0, how would you do it? Something like this, probably:
june = 0; tom = 0; may = 0; scarlet = 0; apple = 0;
This is a tedious way of doing things, and you run the risk of making a mistake. To do the same task using an array, you could do the following:
int[] stuff = new int[5]; for (int i = 0; i < 5; i ++) { stuff[i] = 0; }
This is a much more effecient way of doing things. Notice that each time the loop variable is incremented, a different element of the array is set equal to zero. Using a for-loop in this manner, you can easily perform a complex task on all of the (or specific) elements of an array. The following Stack N' Heap demonstrates how to create and initialize an array of integers storing the numbers 1 - 5. For simplicity, we will show the elements of the array as existing on the stack; but, because they are contained in an object, they are actually stored on the heap.
Code
int[] myArray = new int[5];
for (int i = 0; i < myArray.length; i ++) { myArray[i] = i + 1; } |
Stack
|
Heap
|
Temporary
|
As you see, loops and arrays are often used together. By using a loop variable as an index into an array, you can perform tasks on a large number of variables which just a few lines of code.
Download the jar file: ArrayOp.jar to your
csci/201 directory;
unjar the file (Help),
create a new NetBeans project called ArrayOp
,
and mount the ArrayOp
directory created when you unjarred the archive file (Help).
Compile and run the application class ArrayOp
.
Now take a look at the internal class MyClass1
of ArrayOp
.
private static class MyClass1 implements SpecArrayOp { public int TestFunc(int[] V) { int N = V.length ; return V[N/2] ; } }
As you see, MyClass1
consists of just oneu
method TestFunc()
and that
TestFunc()
has a single parameter,
V
of type int[]
.
TestFunc()
's purpose is to return an integer
value. Right now, it returns the middle element of the array that it
receives as input. (Can you see why?)
The ArrayOp
class works as follows. Its
main()
method calls the
TestArrayOperation()
method of
TestArrayOp
with a single variable, a class
which implements SpecArrayOp
(?)Interfaces are explained in more detail in the Interfaces lab..
TestArrayOperation()
creates a array, passes it
to the TestFunc()
method of its interface and
then prints the result.
You are to modify the main()
method of
ArrayOp
so that it tests six different classes.
These six classes have a TestFunc()
method which
implements the six array operations shown below.
Class Name | TestFunc Return Value |
---|---|
MyClass1 | Adds all elements of the array and returns the total |
MyClass2 | Returns the largest element of the array |
MyClass3 | Returns the smallest element of the array |
MyClass4 | Adds all the even elements of the array and returns the total |
MyClass5 | Returns an integer average of all elements in the array |
MyClass6 | Adds the smallest and largest elements of the array and returns their sum |
When you are done, show your lab instructor the output of your program.
Download the jar file: ArrayMod.jar to your
csci/201 directory;
unjar the file (Help),
create a new NetBeans project called ArrayMod
,
and mount the ArrayMod
directory created when you unjarred the archive file (Help).
Open, compile, and run the ArrayMod
class.
You should notice some fairly strong similarities between
the ArrayOp
and ArrayMod
classes.
ArrayMod
has six internal classes which are
supposed to modify the elements of an array.
Your job is to complete those six internal classes.
This table gives the details of your assigned tasks.
Class Name | Operation performed in TestFunc |
---|---|
MyClass1 | Adds one to each element of the array |
MyClass2 | Multiplies each element by three |
MyClass3 | Sets all array elements to zero |
MyClass4 | Sets the i'th array element to i |
MyClass5 | Reverses the array, that is, switches the first and last elements, etc. |
MyClass6 | Relaxes the array, that is, sets each element to the average of its two surrounding neighbors, except for the first and last elements which are left unchanged. |
If the last Java class stresses you, ask your lab instructor about relaxation.
Again, when you are done, show your lab instructor the output of your program.