Monday July 17, 2000


Recall that an array is a named collection of entities all of the same type.

The syntax for the declaration of an array is

data-type[] arrayname = new data-type[number of elements];
Once this statement is executed we have a new array, that is a new named collections of elements each of type data-type.

In order to access elements stored in an array we need to use the name of the array and the position of the element in the array.

Recall that Java begins counting at 0.

For example to create an array of 5 integers we could use the following code.

int[] numbers = int[5];
The elements in the array are indexed by integers.

Since the elements in an array are indexed by integers, arrays will work nicely with for loops. For example, if we want to initialize each element in the array to the value 1, we could use the following code.

for (int counter=0;counter<5;counter++)
   numbers[counter] = 1;
Arrays can be created during run-time. That is when we declare an array we do not have to immediately make a call to new. That is, we don't have to immediately allocate space for an array when we declare a new reference to an array. Recall the difference between an object and an object reference. We can declare a reference to an array object and it will initially be null. We can then later make a call to new. This means that during run-time we can create an array of a size specified by input from the user. However, once an array is created we cannot change its size.
Consider the following exercise.

In search of the digits of e

Begin with a group of 5 integers all initialized to 1.

1    1    1    1    1
Repeat the following steps 3 times.

1. Multiply each number in the group by 10.

2. Beginning at the right, compute the quotient and remainder when we divide the entry in that position by its position + 1. That is, for the first entry on the right, divide it by 6 to get the quotient and remainder. Add the quotient to the number to its left and replace the current number with the remainder.

3. Record the last quotient obtained.

For this group of numbers we would see the following as we complete these steps.

position:   1  position:  2  position:  3  position:  4  position:  5
old value: 10 old value: 10 old value: 10 old value: 10 old value: 10
           +4            +3            +2            +1
new value: 14 new value: 13 new value: 12 new value: 11
quotient:   7 quotient:   4 quotient:   3 quotient:   2 quotient:   1

new value:  0 new value:  1 new value:  0 new value:  1 new value:  4

old value:  0 old value: 10 old value: 0  old value: 10 old value: 40
           +3            +0           +3             +6
new value:  3 new value: 10 new value: 3  new value: 16
quotient:   1 quotient:   3 quotient:  0  quotient:   3 quotient:   6

new value:  1 new value:  1 new value: 3  new value:  1 new value:  4

old value: 10 old value: 10 old value: 30 old value: 10 old value: 40
           +6            +8            +3            +6
new value: 16 new value: 18 new value: 33 new value: 16
quotient:   8 quotient:   6 quotient:   8 quotient:   3 quotient:   6

new value:  0 new value:  0 new value:  1 new value:  1 new value:  4
Now collect the quotients found and concatenate them onto the String "2.";

Now print the result "2.718";

For this example we could use five different integer variables to store the numbers. But what if we want n (the number of integers we start with) to be arbitrary?

Using an array write the code necessary to produce the output string for an arbitrary integer n.
A Solution
Exercise 2

Using arrays rewrite the Sieve of Eratosthenes.