Laboratory 10

Arrays

 

Objective

A programmer often needs to represent a group of values or objects together in a list. The basic Java list mechanism is the array. When you use a list, you often need to be able to find values in the list. Therefore, we review several search algorithms.

 

Key Concepts

 

Getting Started

Using the procedures in the previous laboratories, copy the files Sum.java, ArrayFun.java, Sequential.java, Searcher.java, and Binary.java for your manipulation.

 

Basics

An array in Java is a list of elements. The list is an object. The individual elements can be accessed through subscripting using the bracket operator []. If a list is composed of n values, then the first element has a subscript value of 0, the second element has a subscript value of 1, and so on with the last element having a subscript value of n-1.

To assist programmers, an array has a public data field named length that specifies the number of elements in the list.

Another form of array assistance is the automatic detection by Java of illegal subscripting – subscripts that are too big or too little. If a subscript is determined to be invalid, an exception of type IndexOutOfBoundsException is generated. As it is with other unhandled exceptions, if IndexOutOfBoundsException occurs and is not handled explicitly by the program, then the program terminates.

The IndexOutOfBoundsException prevents your program from inappropriately accessing memory outside its allocated space. (Attempting illegal memory accesses and assignments is a common exploitation effort of virus writers.)

 

When writing programs in the future pay attention to the fact that when using a for loop in Java to iterate through an array, the body of loop should typically only iterate when loop indices are in the range 0 to n-1, where n is the size of the array. This design accommodates the fact that Java numbers array indices from 0 to n-1. Hence, the typical for loop initializes its loop control variable to 0 and repeats the loop as long as the index is less than the size of the list.

 

Array manipulations

Open program file ArrayFun.java. This program first determines from its user the size n of a list that is to be extracted. The program then extracts n values from the standard input stream and uses them to set the values of the elements of array number. The program then determines and displays the average of the values in the array.

 

Searching

Often it is necessary to search a list of data to determine if a value is present. We will now examine two methods of searching for a particular value: sequential and binary. Record the number of element comparisons needed to complete each search.

 

Sequential Search

Binary Search

The binary search described in this section is a more efficient search than the modified sequential search. For example, when you search for a name in a phone book, you probably don’t start at the beginning and search straight through. You further exploit the fact that the names are in sorted order to speed up your search.

The binary search can be implemented as an iterative technique in which each iteration eliminates half of the remaining values from further consideration. The search starts with the middle list element and decides which half of the data to examine further. In the next iteration, the middle element from the half of the data values that can possibly contain the key value is determined, and from it, the range of possible positions for the key value is restricted further. The process is repeated until the search finds the key value or until the search determines that there are no more potential positions to consider.

 

As list size increases, the efficiency improvement of binary search over sequential search is very dramatic. For example binary search requires at most 20 comparisons for a list of size 1000. Sequential search might make 1000 comparisons!

 

Finishing Up