• Section 1 Creating and Using Arrays

  • FileName: ch08.01.ho.pdf [read-online]
    • Abstract: In Java, the items in an array are always numbered from zero up to ... If an array contains 100 integers, indexes will range from zero to 99. The items in an ...

Download the ebook

I2IPUJ - Chapter 8
Section 1
Creating and Using Arrays
Rob Dempster
[email protected]
(School of Computer Science)
University of KwaZulu-Natal
Pietermaritzburg Campus
I2IPUJ - Chapter 8Arrays – p.1/17
This is not a paper. It is the lecture presentation slides I used to provide a framework for the
lectures I presented dealing with the material covered in Section 1 of Chapter 8 of David J.
Eck’s book entitled Introduction to Programming Using Java [1]. The slides were prepared
using SuSE Linux, Emacs, LTEXand Prosper.
c 2005, Robert Dempster. These are free slides. There are no restrictions on using or
redistributing or posting on the web a complete, unmodified copy of this material. There are
some restrictions on modified copies. To be precise: Permission is granted to copy, distribute
and/or modify this document under the terms of the GNU Free Documentation License,
Version 1.1 or any later version published by the Free Software Foundation; with no invariant
sections, front cover text, or back cover text.
The most recent version of these slides are always available, at no charge, for downloading
and for on-line use at the Web address http://saturn.cs.ukzn.ac.za/ robd/javaslides/. There
you will find the LTEXsource code together with the slides in formats suitable for slide
presentations (.sp.) and hand-outs (.ho.).
I2IPUJ - Chapter 8Arrays – p.2/17
Introduction to Data Structures
Computers get a lot of their power from working with data structures.
A data structure is an organized collection of related data.
Objects are data structures.
Objects contain data in the form of the values associated with the fields
defined for the class used to instantiate the data structure.
Objects also contain methods that control the manner in which the data is
Niklaus Wirth coined a now famous phrase when he entitled a book:
“Algorithms + Data Structures = Programs”.[2]
It is not uncommon for programmers to build complicated data structures by
hand, by linking objects together.
When programmers do this, they would consider designing a class that
represents the data structure.
I2IPUJ - Chapter 8Arrays – p.3/17
Introduction to Arrays
There is one type of data structure that is so important, and so basic,
that it is built into every programming language: it is the array.
An array is a data structure consisting of a numbered list of items,
where all the items are of the same type.
In Java, the items in an array are always numbered from zero up to
some maximum value, which is set when the array is created.
If an array contains 100 integers, indexes will range from zero to 99.
The items in an array can belong to one of Java’s primitive types.
They can also be references to objects, so that you could, for
example, make an array containing strings or “hello world” objects.
I2IPUJ - Chapter 8Arrays – p.4/17
Creating and Using Arrays
When a number of data items are chunked together into a unit, the
result is a data structure.
Data structures often simply consist of a sequence of data items.
Data structures of this simple variety can be either arrays or records.
The term record is not used in Java. A record is essentially the same
as a Java object that has instance variables only, but no instance
The data items in a record – in Java, an object’s instance variables –
are called the fields of the record.
Each item is referred to using a field name.
I2IPUJ - Chapter 8Arrays – p.5/17
Record Example Creating and Using Arrays
For example, if the class Person is defined as:
class Person {
String name;
int id_number;
Date birthday;
int age;
Then an object of class Person could be considered to be a record
with four fields.
The field names are name, id_number, birthday, and age.
Records are also associated with file storage and data bases and the
systems which process records stored using these technologies.
I2IPUJ - Chapter 8Arrays – p.6/17
Definition of an Array
Like a record, an array is a sequence of items.
However, where items in a record are referred to by name, the items
in an array are numbered, and individual items are referred to by their
position number.
The definition of an array is: a numbered sequence of items, which
are all of the same type.
The number of items in an array is called the length of the array.
The position number of an item in an array is called the index of that
The type of the individual items in an array is called the base type of
the array.
I2IPUJ - Chapter 8Arrays – p.7/17
Some Array properties
The base type of an array can be any Java type, that is, one of the
primitive types, or a class name, or an interface name.
If the base type of an array is int, it is referred to as an "array of
As always, there is some potential for confusion between the two uses
of a variable: as a name for a memory location and as a name for the
value stored in that memory location.
Each position in an array acts as a variable.
If the length of the array is N, then the index of the last element in the
array is N-1.
NB!!! – Once an array has been created, its length cannot be
I2IPUJ - Chapter 8Arrays – p.8/17
Arrays are Objects!
Java arrays are objects and this has several consequences.
Arrays are created using a form of the new operator.
No variable can ever hold an array – a variable can only refer to an
Any variable that can refer to an array can also hold the value null,
meaning that it doesn’t at the moment refer to anything.
Like any object, an array belongs to a class, which like all classes is a
subclass of the class Object.
The elements of the array are, essentially, instance variables in the
array object, except that they are referred to by number rather than by
I2IPUJ - Chapter 8Arrays – p.9/17
Arrays are also special!
Even though arrays are objects, there are differences between arrays
and other kinds of objects.
There are also a number of special language features in Java for
creating and using arrays.
Suppose that A is a variable that refers to an array. Then:
The item at index k in A is referred to as A[k].
The first item is A[0], the second is A[1], and so forth.
"A[k]" is really a variable, and it can be used just like any other
Although every array, as an object, is a member of some class, array
classes never have to be defined.
Once a type exists, the corresponding array class exists automatically.
I2IPUJ - Chapter 8Arrays – p.10/17
There will be a corresponding array class
Once a type exists, the corresponding array class exists
If the name of the base-type of an array is BaseType, then the
name of the associated array class is BaseType[].
The brackets, "[]", are meant to recall the syntax for referring
to the individual items in the array.
"BaseType[]" is read as "array of BaseType" or
"BaseType array."
Recall that if ClassA is a subclass of ClassB, then
ClassA[] is automatically a subclass of ClassB[].
I2IPUJ - Chapter 8Arrays – p.11/17
Declaring arrays
As int[] is a class, the declaration:
int[] list;
creates a variable named list of type int[].
This variable is capable of referring to an array of ints, but initially its value is
null (if it is a member variable in a class) or undefined (if it is a local variable
in a method).
The new operator is used to create a new array object, which can then be
assigned to list. The syntax is:
list = new int[5];
The constructor "new BaseType[N]" is used to create an array belonging
to the class BaseType[].
The value N in brackets specifies the length of the array, that is, the number of
elements that it contains.
Note that the array "knows" how long it is.
I2IPUJ - Chapter 8Arrays – p.12/17
Declaring arrays - what do we get?
The situation produced by the statement "list = new int[5];" can be
pictured like this:
Note that the newly created array of integers is automatically filled with zeros.
In Java, a newly created array is always filled with a known, default value: zero
for numbers, false for boolean, the character with Unicode number zero for
char, and null for objects.
I2IPUJ - Chapter 8Arrays – p.13/17
Some Examples and an Exercise
Check out the examples towards the end of this section in the on line form of
Eck’s book.
Check out the ArrayEx01.java file for some examples.
Design, code and debug a program that:
Prompts the user and then captures the marks (percentages) for a class
Prints the lowest mark.
Prints the highest mark.
Prints the average mark.
Prints the median mark.
Prints the mode mark.
Prints a bar chart representing the mark distribution using 10 as an interval
I2IPUJ - Chapter 8Arrays – p.14/17
Multi-dimensional Arrays
Any type can be used as the base type for an array.
Since an array type is a first-class Java type, you can have an array of arrays.
So if ints has type int[] then there must automatically be another type,
int[][], which represents an "array of arrays of ints" and is a
two-dimensional array.
Similarly there must also be automatically be another type, int[][][]
which is a three-dimensional array.
The declaration statement "int[][] A;" declares a variable named A of
type int[][]. This variable can hold a reference to an object of type
The assignment statement "A = new int[3][4];" creates a new
two-dimensional array object and sets A to point to the newly created object.
The notation int[3][4] indicates that there are 3 arrays-of-ints in the
array A, and that there are 4 ints in each array-of-ints.
I2IPUJ - Chapter 8Arrays – p.15/17
Multi-dimensional Arrays - a diagram
A two-dimensional array
I2IPUJ - Chapter 8Arrays – p.16/17
[1] Introduction to Programming Using Java, Version 4,
Eck, David J., URL:
[2] Algorithms + Data Structures = Programs, Niklaus
Wirth, Prentice Hall, 1976
I2IPUJ - Chapter 8Arrays – p.17/17

Use: 0.1639