CISC 1115
Introduction to Programming Using Java
Lecture #11
Arrays


Reading from the Text

Chapter 7

Motivation for Arrays

Consider the following program/method specifications: In all of the above cases:

Now, consider the following programs/methods specifications:

Some number-crunching programs:

How about some transcript-related programs? How about some payroll-related programs? In all these cases, we need to hold onto to ALL the numbers until we've finished our processing.

Consider the above programs in the context of

Not only is declaring 100 numbers horrible, but the subsequent logic is equally or more horrible.

What we need is some way of working with a bunch of numbers at once. This involves being able to:

Arrays

Basic definition An array is a sequence of items, all of the same type, each of which is referred to by its position in the sequence.
     0   1   2   3   4   5   6   7   8   9    -> index
   +---------------------------------------+
   | 2 | 6 | 4 | 9 | 1 | 3 | 8 | 7 | 0 | 6 |  -> element value
   +---------------------------------------+

Arrays in Java

Arrays in Java are objects in the sense that they must be created.

Tasks we will be performing with arrays

Revisiting Types, Values, and Variables

The following terminology and categories are Java-specific; many other object-oriented languages use similar or analogous terms.

Primitives

References

References, Objects, Aliases

Declaring an Array

Arrays are declared in a manner somewhat similar to other variables

int [] nums;
double [] averages;
String [] names; 

Creating an Array Object

Like Scanners, PrintStreams, and Files, an array object must be created and assigned to the declared variable.

The Number of Elements in an Array (length)

Working with Individual Elements in an Array: Subscripting

An individual element can be accessed by specifying its position in the array using the index or subscript operator [].

nums[0];					// literal or constant
averages[i];				// variable
names[names.length-1];		// arbitrary expressions 

Initializing

There are numerous ways of assigning values to the elements of an array:

Explicit assignment

public class Program_11_1 {
        public static void main(String [] args) {
                int [] arr = new int[5];
                arr[0] = 12;
                arr[1] = 15;
                arr[2] = 27;
                arr[3] = 95;
                arr[4] = 13;

                for (int i = 0; i < arr.length; i++)
                        System.out.println(arr[i]);
        }
}

Using an Initializer

Using the Index Variable of a for Loop

Reading in the Elements

More on Accessing and Modifying the Elements of an Array

lvalues and rvalues

We've seen that depending on the context, a variable represents either its value or its location; i.e., sometimes we are interested in accessing its value and sometimes we are interested in replacing the value with a new value

Subscripted Array Elements as lvalues and rvalues

The above discussion applies equally to the elements of an array; i.e. there are times when we are interested in the value (rvalue) of arr[i] and time were are interested in its location (lvalue). (They are the same contexts as for variables as presented above).

Traversing/Iterating Through an Array

The process of going through an array visiting each of its elements is known as traversal or iteration. It is an essential component to array processing … the whole point of the array is to provide a way to maintain a large number of elements whose processing is the same from one to another.

Arrays and for Loops

The Enhanced For Loop

Arrays and Methods

Arrays as Parameters

Methods that have array parameters are no different than other methods.

We want to turn useful operations on arrays into methods:

We'll present these in the Techniques lecture

Array Arguments vs Subscripted Array Element Arguments

Given
int [] arr = new int[100];
…
void f(int [] arr) {…}
void g(int x) {…}

Arrays as Return Values

Arrays can be returned as the value of a method, just like any other variable:
int [] copy(int [] arr) {
	int [] result = new int[arr.length];
	for (int i = 0; i < arr.length; i++)
		result[i] = arr[i];
	return result;
}

Methods That 'Modify' their Array Parameters

Changes made to the elements of an array in a method remain (persist) after the method returns to the caller
int [] fillArray(int arr[]) {
	for (int i = 0; i < arr.length; i++)
		arr[i] = 0;
}
Motes

Passing a reference is NOT 'call-by-reference'

The Wrong Way
void createAndFillArray(int [] arr, int size) {
	arr = new int[size];
	for (int i = 0; i < arr.length; i++)
		arr[i] = 0;
}
…
int [] a;
createAndFillArray(a);
Notes
The Right Way to Achieve This
int [] createAndFillArray(int size) {
	int arr[] = new int[size];
	for (int i = 0; i < arr.length; i++)
		arr[i] = 0;
	return arr;
}
…
int [] a = createAndFillArray();

Files used in this Lecture

Lab for this Lecture