Chapter 7.2 – Programming With Arrays | Introduction to Programming Using Java

Chapter 7.2 – Programming With Arrays | Introduction to Programming Using Java

 

7.2 Programming With Arrays

Arrays are the most basic and the most important type of data structure, and techniques for processing arrays are among the most important programming techniques you can learn. Two fundamental array processing techniques—searching and sorting—will be covered in Section 7.4. This section introduces some of the basic ideas of array processing in general.

 

7.2.1 Arrays and for Loops

In many cases, processing an array means applying the same operation to each item in the array. This is commonly done with a for loop. A loop for processing all the elements of an array A has the form:

 

programming with arrays

 

Suppose, for example, that A is an array of type double[ ]. Suppose that the goal is to add up all the numbers in the array. An informal algorithm for doing this would be:

 

Chapter 7.2 - Programming With Arrays | Introduction to Programming Using Java

 

Putting the obvious repetition into a loop and giving a name to the sum, this becomes:

 

Chapter 7.2 - Programming With Arrays | Introduction to Programming Using Java

 

Note that the continuation condition, “i < A.length”, implies that the last value of i that is actually processed is A.length-1, which is the index of the final item in the array. It’s important to use “<” here, not “<=”, since “<=” would give an array index out of bounds error. There is no element at position A.length in A.

Eventually, you should just about be able to write loops similar to this one in your sleep. I will give a few more simple examples. Here is a loop that will count the number of items in the array A which are less than zero:

 

Chapter 7.2 - Programming With Arrays | Introduction to Programming Using Java

 

Replace the test “A[i] < 0.0”, if you want to count the number of items in an array that satisfy some other property. Here is a variation on the same theme. Suppose you want to count the number of times that an item in the array A is equal to the item that follows it. The item that follows A[i] in the array is A[i+1], so the test in this case is “if (A[i] == A[i+1])”. But there is a catch: This test cannot be applied when A[i] is the last item in the array, since then there is no such item as A[i+1].

The result of trying to apply the test in this case would be an ArrayIndexOutOfBoundsException. This just means that we have to stop one item short of the final item:

 

Chapter 7.2 - Programming With Arrays | Introduction to Programming Using Java

 

Another typical problem is to find the largest number in A. The strategy is to go through the array, keeping track of the largest number found so far. We’ll store the largest number found so far in a variable called max. As we look through the array, whenever we find a number larger than the current value of max, we change the value of max to that larger value. After the whole array has been processed, max is the largest item in the array overall.

The only question is, what should the original value of max be? One possibility is to start with max equal to A[0], and then to look through the rest of the array, starting from A[1], for larger items:

 

3 9 Chapter 7.2 - Programming With Arrays | Introduction to Programming Using Java

4 8 Chapter 7.2 - Programming With Arrays | Introduction to Programming Using Java

 

(There is one subtle problem here. It’s possible in Java for an array to have length zero. In that case, A[0] doesn’t exist, and the reference to A[0] in the first line gives an array index out of bounds error. However, zero-length arrays are normally something that you want to avoid in real problems. Anyway, what would it mean to ask for the largest item in an array that contains no items at all?)

As a final example of basic array operations, consider the problem of copying an array. To make a copy of our sample array A, it is not sufficient to say double[] B = A;

since this does not create a new array object. All it does is declare a new array variable and make it refer to the same object to which A refers. (So that, for example, a change to A[i] will automatically change B[i] as well.) To make a new array that is a copy of A, it is necessary to make a new array object and to copy each of the individual items from A into the new array:

 

5 8 Chapter 7.2 - Programming With Arrays | Introduction to Programming Using Java

 

Copying values from one array to another is such a common operation that Java has a predefined subroutine to do it. The subroutine, System.arraycopy(), is a static member subroutine in the standard System class. Its declaration has the form

 

Chapter 7.2 - Programming With Arrays | Introduction to Programming Using Java

 

sourceArray to destArray. The count tells how many elements to copy. Values are taken from sourceArray starting at position sourceStartIndex and are stored in destArray starting at position destStartIndex. For example, to make a copy of the array, A, using this subroutine, you would say:

 

Chapter 7.2 - Programming With Arrays | Introduction to Programming Using Java

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5.7 – Interfaces | Introduction to Programming Using Java

Chapter 5.8 – Nested Classes | Introduction to Programming Using Java

Chapter 6 – Introduction to GUI Programming | Introduction to Programming Using Java

Chapter 7 – Arrays | Introduction to Programming Using Java

1 thought on “Chapter 7.2 – Programming With Arrays | Introduction to Programming Using Java”

Leave a Comment