Week03 Lesson 02 Arrays
Week03 Lesson 02 Arrays
Introduction
In this lesson you will be introduced to arrays, which enable you to deal with a number of
variables of the same type through a single variable name.
Learning outcome
On successful completion of this lesson you would be able to describe arrays in Java and how
to use them to improve efficiency in programming. Thus you should be able to,
2.1 Arrays
An array is a named set of variables of the same type. Each variable in the array is called an
array element. To reference a particular element in an array, the array name combined with an
integer value called index is used. The index for an array element is the offset of that particular
element from the beginning of the array. The first element will have an index of 0; the second
will have an index of 1, the third an index of 2, and so on. Following figure illustrate the
representation of an array.
Note that the index value does not need to be an integer literal. It can be any expression that
results in a value of type int equal to or greater than zero.
The following Figure 2.2 illustrates how an array of six integers have been stored in a RAM
memory.
The array variable is distinct from the array itself. Before you define an array you have to declare
it. You could declare the integer array variable primes with the statement:
int[] primes;
The following statement can also be used as an alternative statement, to declare the integer array
variable primes.
int primes[];
2
Arrays
IT 2142 –Data Structures & Algorithms Week 03
The variable primes is now a place holder for an integer array that you have yet to define. No
memory is allocated to hold the array itself at this point. The square brackets following the type
(here int) in the previous statement indicates that the variable is for referencing an array of int
values, and not for storing a single value of type int.
Once you have declared an array variable, you can define an array that it will reference as in the
statement:
This statement creates an array that will store 20 values of type int, and records a reference to
the array in the variable primes. The reference is simply where the array is in memory. You
could also declare the array variable and define the array of type int to hold 20 prime numbers
with a single statement:
For eg.
Int[] primes = new int[20];
If you know the length of the array and its elements’ value at compile time, then you can
simultaneously allocate and initialize the array with an initialization list in its declaration. For
example, the declaration
3
Arrays
IT 2142 –Data Structures & Algorithms Week 03
Int[] a = {10,22,32,44,55,66,72};
Allocates the array a[] to length 7with the value specified. This can be visualized as in following
figure.
0 1 2 3 4 5 7
a 10 22 32 44 55 66 72
An attempt to compile this will result in an “illegal start of expression” message from the compiler.
Initialization arrays can also be used in creation of anonymous arrays (i.e. arrays which do not
have array variable name is called as anonymous array). For example the expression:
This defines an anonymous array of 7 integer element with the values indicated. This anonymous
array expression could be passed to method, thus:
4
Arrays
IT 2142 –Data Structures & Algorithms Week 03
Note that the initialization list is being used to initialize a newly allocated anonymous array.
Assigning the list to the array reference merely copies the memory address of that anonymous
array object into the reference variable aa.
You can refer to the length of the array using length, a data member of the array object. For our
array primes, we can refer to its length as primes.length. We could use this to write the
calculation of the average as:
Now the code is independent of the number of array elements. If you change the number of
element in the array, the code will automatically deal with that. You should always use this
approach when you need to refer to the length of an array – never use explicit values.
5
Arrays
IT 2142 –Data Structures & Algorithms Week 03
We have only worked one‐dimensional arrays up to now, that is arrays that use a single index.
Why would you ever need the complication of using more indexes to access the element of an
array?
Suppose that you have fanatical interest in the weather, and you are intent on recording the
temperature each day at 10 separate geographical locations throughout the year 2007. Once you
have sorted out the logistics of actually collecting this information, How do you store them in a
computer?. Use an Excel file or Use a database... yah.. sure you can store them using any
application but if you need those data in your programme, for calculation purpose, how do you
store them in computer memory?
What would be the efficient way?
Here you can use an array of 10 elements corresponding to the number of locations, where each
of these elements in an array of 365 elements to store the temperature values. You would declare
this array with the statement:
This is called a two‐dimensional array, since it has two dimensions‐one with index values
running from 0 to 9, and the other with index values from 0 to 364. The first index will relate to
a geographical location, and the second index corresponds to the day of the year. That’s much
handier than a one dimensional array with 3650 elements isn’t it?
6
Arrays
IT 2142 –Data Structures & Algorithms Week 03
There are 10 arrays, each having 365 elements. In referring to an element, the first square
brackets enclose the index for a particular array, and the second pair of square brackets enclose
the index value for an element within that array. So to refer to the temperature for day 100 for
the sixth location, you would use temperature [5][99].Since each float variable occupies 4 bytes
the total space required to store the elements in this two‐dimensional is 10x365x4 bytes.
Consider the following table which shows the distance between cities in USA.
7
Arrays
IT 2142 –Data Structures & Algorithms Week 03
Now we will see how this set of data can be stored in a 2-D array. There are seven cities. Therefore,
in order to store distance between cities, we need 7x7 2-D array. So we can define a 2-D array
called distances as below.
This can also be viewed as there are seven arrays of seven elements.
Activity 2.1
Assume that there are 10 location and assume that you have generated 365 random numbers for
temperature values for each location. As is the above example values are stored in a 2-D array.
Write a program to calculate the average annual temperature for each location.
Hint : generate random values between ‐10°c and 35°c using Math.random() method
When you create an array of arrays, the arrays in the array do not need to be all the same length.
You could declare an array variable prime with the statement:
float [][] prime;
This declares the array object prime to be of type float[][]. We can then define the number of
elements in the first dimension with the statement:
8
Arrays
IT 2142 –Data Structures & Algorithms Week 03
The variable prime now references an array with 6 elements, each of which can hold a reference
to one‐dimensional array. You can define these arrays individually if you want as follows.
This defines two of the arrays. Obviously we cannot use an array until it has been defined, but
you could possibly use these two and define the others later.
Activity 2.2
Redefine the above prime array to have a triangular shape, with one element in the first row, two
elements in the second row, three in the third row, and so on.
The array, income, has three dimensions. It provides for holding income for each up to 8 sub
project per project, with 18 projects per country in each 5 countries.
You can envisage this as just a three‐dimensional array, but remember that income is an array
of 8 elements, each of which holds a two‐dimensional array, and each of these two‐dimension
arrays can be different. For example if you really want to go down, you can declare the array
income with the statement:
9
Arrays
IT 2142 –Data Structures & Algorithms Week 03
Each of the three elements in the first dimensions of income can hold a different two‐
dimensional array, so you could specify the first dimension of each explicitly with the
statements:
These three arrays have elements that each hold a one‐dimensional array, and you can also
specify the sizes of these independently. Note how the empty square brackets indicate there is
still a dimension undefined.
Summary
In this lesson you learnt about arrays as a simplified data structure which is used for store
data. Here you knew about how to define an array, and get the length of the array. Further
you learned about arrays of arrays and multi‐dimensional arrays. Next lesson you will learnt
about Linked Structures.
10
Arrays