Lesson 5 - Arrays

TABLE OF CONTENTS: Introduction - Arrays - ArrayList

Introduction -

We've talked in previous lessons about computers being useful for processing large amounts of data. One simple task that you might need to accomplish is sorting a set of data. We already know enough to accomplish this, but it will be rather inefficient. The code below will sort five integer values from lowest to highest...

int first = 2;
int second = 6;
int third = 4;
int fourth = 1;
int fifth = 3;


if (first > second)
	swap(first, second);

if (second > third)
	swap(second, third);
if (third > fourth)
	swap(third, fourth);
if (fourth > fifth)
	swap(fourth, fifth);

//At this point all you have guaranteed is that the highest value is in fifth.

if (first > second)
	swap(first, second);
if (second > third)
	swap(second, third);
if (third > fourth)
	swap(third, fourth);

if (first > second)
	swap(first, second);

if (second > third)
	swap(second, third);
if (first > second)
	swap(first, second);

After all of that you have sorted a list of five elements. But what if the list had ten elements? What if it had 1000? 1,000,000?

This method of coding would not be efficient - we would need a better way of organizing and accessing the data than giving every piece of data it's own identifier. We would need arrays.

Pre-requisites -

Before beginning this lesson you should have completed each of the previous lessons.  (This lesson will build upon previous concepts and many of them will be necessary to implement arrays.)

Goals -

  1. Students will use correct syntax when working with arrays in Java. 
  2. Students will be able to use simple arrays and the ArrayList class to organize data within a Java program.

Section 1: Arrays

These are your objectives for this section:

Given a output description and a section of code with a missing line, students will be able to fill in array statements that would accomplish the task without producing errors. 

Given sections of code that includes arrays, students will identify and correct the error in the code or identify the code as correct.  Possible errors to be tested would include – index out of bounds, use of length, syntax errors. 

An array is a data structure used to organize list of data of the same type. Any type (or class) can be used in an array. For the example above, using five different variable names for the five scores in the list made the program become cumbersome. A single array of integers would make this program much easier.

int[] scores = new int[5];

This declares a single variable called scores, however it allocates space for an array of five integers.

Each of the elements of the array can be accessed using the following notation:

variableName[index]

in this case

scores[0]

The number inside the square brackets is the index into the array. It tells which element of the array to use. Arrays always start numbering at zero. So an array with five elements would use indeces 0-4.

The cool thing about arrays is that we can use a variable in place of the index. Combine this with a nice for loop and you've got a quick and easy way to access all the elements of an array.

for(int index=0; index < 5; index++)
{
	scores[index] = randy.nextInt(100);
}

The code above would fill the array with random integers.

There are many different ways to sort a list of integers. One of the most simple is the swap sort (which is what we tried above in the introduction.) We'll try it again here using arrays.

public class ArrayDemo
{
	int NUMBER_OF_SCORES = 5; //notice how identifiers for constants are in all CAPS
	int[] scores = new int[NUMBER_OF_SCORES];


	public void fillArray()
	{
		for(int index=0; index < NUMBER_OF_SCORES; index++)
		{
			scores[index] = randy.nextInt(100);
		}
	}

	public void displayArray()
	{
		for(int index=0; index < NUMBER_OF_SCORES; index++)
		{
			System.out.print(" "+scores[index]);
		}
		System.out.println("  Done.");
	}

	public void swapSort()
	{
		for(int x=0;x<NUMBER_OF_SCORES;x++)
		{
			for(int y=0;y<NUMBER_OF_SCORES-1;y++)
			{
				if(scores[y] < scores[y+1])
					swap(y,y+1);
			}
		}
	}

	public void swap(int one, int two)
	{
		int temp = scores[one];
		scores[one] = scores[two];
		scores[two] = temp;
	}		

 

Section 2: The ArrayList class

These are your objectives for this section:

After reading this lesson, students will know the difference between simple arrays and ArrayList objects.

Another version of arrays is available using the ArrayList class which can be found in the Java API. The ArrayList class allows arrays to be dynamic in size while a program runs rather than having to be declared with a fixed size. The ArrayList class also provides many methods that make it easier to access the array.

Here are a few programming challenges for you to try:

(This assignment is worth 90 points. You must complete each of the following programs)

  1. Write a tester class that exercises an ArrayDemo objects.
  2. Add a method to the ArrayDemo class that finds the maximum value in the array and displays it.
  3. Add a method to the ArrayDemo class that finds the sum of all the elements in the array and returns it as an int.

Helpful References

BlueJ

Java Au Naturel

Java API

Online Tutorial - LandOfCode

Online Java Tutorial - Cafe Au Lait

Text - Java Concepts by Cay Horstmann (Chapter 6 - Decisions) *We have six copies of this book in the computer lab*