CSCI A201/A597

Lecture Notes 24

Spring 2000


Arrays revisited. Hints on homework 9 and 10. Overview of lab twelve.

Arrays represent a data structure. They allow us to gain access to the computer memory to store lots of data in an uniform way, that is, by using a name and an index.

Arrays can be visualized as many memory locations that can store data of a certain type. The locations can be accessed individually using the name of the array and their index in the collection of locations that comprise the array.

When working with an array we distinguish three important stages:

1. Declaration

This defines the name of the array and the the type of data it can hold.

2. Allocation

This part actually asks the computer to allocate space for the locations by specifying the number of locations that the array will comprise.

3. Initialization

This part actually stores the data that we originally need in the locations of the array, replacing the default values that the array contains after the allocation stage).

Let's look at an example.

Let's declare, allocate and initialize an array of int's.

int[] m;
m = new int[20];
for (int i = 0; i < m.length; i++) {
  m[i] = 3;
}
This declares a variable m of type int[].

It also allocates 20 locations for the 20 ints that could be refered to as m[0], m[1], ..., m[19].

It then places a value of 3 in each of the 10 locations of the array.

Let's do the same for an array of Circle's.

For this example let's say we have a function fun defined in a class Library that returns random ints within a specified range.

Circle[] c;
c = new Circle[30];
for (int i = 0; i < c.length; i++) { 
  int x = Library.fun(20, 180); 
  int y = Library.fun(20, 180); 
  int radius = Library.fun(10, 20); 
  c[i] = new Circle(x, y, radius); 
}
This first defines a name c that could be used to refer to a collection of Circles using an index.

Then 30 locations are allocated to store information about these Circles. After allocation the cells (locations) contain a default value null.

To place actual Circles in the array we need to create them. So for each position in the array we generate a random x and a random y (between 20 and 180) and a random radius and use these three values to generate a new Circle that we place in the array at the current position of the index (i).

All the arrays that we looked at are unidimensional.

To access data we only need the name and the value of one index.

Multi-dimensional arrays are possible.

I don't think we will be using arrays with more than two indices (or dimensions) in this class so looking at two-dimensional arrays will be informative enough, I believe.

Here's a version of Crossed.java in which the pattern (the letter Z) is first created in memory, in a two by two arrays of char's and only then printed.

public class AC {
    public static void main(String[] args) {
	int size = Integer.parseInt(args[0]); 
	char[][] p; 
	p = new char[size][size]; 
	for (int i = 0; i < size; i++) 
	    for (int j = 0; j < size; j++) 
		p[i][j] = ' '; 
	for (int k = 0; k < size; k++) 
	    p[size-1][k] = '*'; 
	for (int k = 0; k < size; k++) 
	    p[0][k] = '*'; 
	for (int k = 0; k < size; k++) 
	    p[k][size-1-k] = '*'; 
	for (int i = 0; i < size; i++) {
	    for (int j = 0; j < size; j++) 
		System.out.print(" " + p[i][j]); 
	    System.out.println(); 
	}
    } 
}
Notice that we can fill the memory structure in the order we want (by first creating the line at the bottom, then the diagonal and only then the top line). This is possible because the memory structure provides direct access to its elements, unlike the characters on the screen, that have to be produced and printed in a certain way (sequentially, one by one).

Now let's look again at two problems involving arrays of numbers.

1. Finding the max element in an unidimensional array of numbers.

frilled.cs.indiana.edu%javac Test.java
frilled.cs.indiana.edu%java Test
Here's the array: 
0 1 2 3 4 5 6 7 8 9 
Now we are going to find the max element.
Max element is: 9
frilled.cs.indiana.edu%cat Test.java
public class Test {
    public static void main(String[] args) {
	int[] n = new int[10]; 
	for (int i = 0; i < n.length; i++) {
	    n[i] = i; 
	} 
	System.out.println("Here's the array: "); 
	for (int i = 0; i < n.length; i++) {
	    System.out.print(n[i] + " "); 
	} 
	System.out.println(); 
	System.out.println("Now we are going to find the max element."); 
	int max = n[0]; 
	for (int i = 0; i < n.length; i++) {
	    if (n[i] > max) {
		max = n[i]; // updated
	    } 
	} 
	System.out.println("Max element is: " + max); 
    } 
} 
frilled.cs.indiana.edu%
Extension: finding the max element in an two-dimensional array of numbers.

Note: Rawles 100 is a two-dimensional array of students.

We could say:

Student[] rawles100;
rawles100 = new Student[15][17];
This defines the name and allocates the chairs, 15 rows of 17 chairs each.

So one day the lecture topic is so interesting and the weather outside is so nice that only two students come to class, Jason (sitting somwehere in the middle of the class) and Sangwook (who decides to sit in the front row anyway):

rawles100[0][8] = new Student("Sangwook Park"); 
rawles100[8][9] = new Student("Jason Levy"); 
Later the lecture becomes so interesting, the weather outside so bad, that many more students are attracted to rawles100 but we don't show that part in here.

2. Sorting a unidimensional array of numbers.

frilled.cs.indiana.edu%javac Test.java
frilled.cs.indiana.edu%java Test
Here's the array: 
0 1 2 3 4 5 6 7 8 9 
Now we are going to sort the array in descending order.
Here's the array sorted: 
9 8 7 6 5 4 3 2 1 0 
frilled.cs.indiana.edu%cat Test.java
public class Test {
    public static void main(String[] args) {
	int[] n = new int[10]; 
	for (int i = 0; i < n.length; i++) {
	    n[i] = i; 
	} 
	System.out.println("Here's the array: "); 
	for (int i = 0; i < n.length; i++) {
	    System.out.print(n[i] + " "); 
	} 
	System.out.println(); 
	System.out.println("Now we are going to sort the" +
                           " array in descending order."); 
	for (int i = 0; i < n.length; i++) {
	    for (int j = i; j < n.length; j++) {
		if (n[i] < n[j]) {
		    int val = n[i]; 
		    n[i] = n[j];
		    n[j] = val; 
		} 
	    } 
	} 
	System.out.println("Here's the array sorted: "); 
	for (int i = 0; i < n.length; i++) {
	    System.out.print(n[i] + " "); 
	} 
	System.out.println(); 
    } 
} 
frilled.cs.indiana.edu%
The actual sorting part is in blue.

We will draw a picture in class to illustrate this situation.

The last part of the lecture will be looking at and presenting lab notes 12 (twelve):


Last updated: Apr 06, 2000 by Adrian German