Chapter 7.1.2 – Using Arrays | Introduction to Programming Using Java

Chapter 7.1.2 – Using Arrays | Introduction to Programming Using Java

 

7.1.2 Using Arrays

 

Suppose that A is a variable that refers to an array. Then the element at index k in A is referred to as A[k]. The first element is A[0], the second is A[1], and so forth. “A[k]” is really a variable, and it can be used just like any other variable. You can assign values to it, you can use it in expressions, and you can pass it as a parameter to a subroutine. All of this will be discussed in more detail below. For now, just keep in mind the syntax harray-variablei [ hinteger-expressioni ] for referring to an element of an array.

 

Although every array, as an object, belongs to some class, array classes never have to be defined. Once a type exists, the corresponding array class exists automatically. If the name of the type is BaseType, then the name of the associated array class is BaseType[ ]. That is to say, an object belonging to the class BaseType[ ] is an array of items, where each item is a variable of type BaseType.

 

 

Chapter 12.3.5 Keyboard Accelerators | Introduction to Programming Using Java

 

 

The brackets, “[]”, are meant to recall the syntax for referring to the individual items in the array. “BaseType[ ]” is read as “array of BaseType” or “BaseType array.” It might be worth mentioning here that if ClassA is a subclass of ClassB, then the class ClassA[ ] is automatically a subclass of ClassB[].

The base type of an array can be any legal Java type. From the primitive type int, the array type int[ ] is derived. Each element in an array of type int[ ] is a variable of type int, which holds a value of type int. From a class named Shape, the array type Shape[ ] is derived. Each item in an array of type Shape[ ] is a variable of type Shape, which holds a value of type Shape. This value can be either null or a reference to an object belonging to the class Shape. (This includes objects belonging to subclasses of Shape.)

Let’s try to get a little more concrete about all this, using arrays of integers as our first example. Since int[ ] is a class, it can be used to declare variables. For example, int[] list;

creates a variable named list of type int[ ]. This variable is capable of referring to an array of ints, but initially its value is null (if list is a member variable in a class) or undefined (if list is a local variable in a method). The new operator is used to create a new array object, which can then be assigned to list. The syntax for using new with arrays is different from the syntax you learned previously. As an example, list = new int[5];

creates an array of five integers. More generally, the constructor “new BaseType[N]” is used to create an array belonging to the class BaseType[ ]. The value N in brackets specifies the length of the array, that is, the number of elements that it contains. Note that the array “knows” how long it is. The length of the array is an instance variable in the array object.

In fact, the length of an array, list, can be referred to as list.length. (However, you are not allowed to change the value of list.length, so it’s really a “final” instance variable, that is, one whose value cannot be changed after it has been initialized.)

The situation produced by the statement “list = new int[5];” can be pictured like this:

 

Chapter 7.1.2 - Using Arrays | Introduction to Programming Using Java

 

Note that the newly created array of integers is automatically filled with zeros. In Java, a newly created array is always filled with a known, default value: zero for numbers, false for boolean, the character with Unicode number zero for char, and null for objects.

The elements in the array, list, are referred to as list[0], list[1], list[2], list[3], and list[4]. (Note again that the index for the last item is one less than list.length.) However, array references can be much more general than this. The brackets in an array reference can contain any expression whose value is an integer. For example if indx is a variable of type int, then list[indx] and list[2*indx+7] are syntactically correct references to elements of the array list. Thus, the following loop would print all the integers in the array, list, to standard output:

 

Chapter 7.1.2 - Using Arrays | Introduction to Programming Using Java

 

The first time through the loop, i is 0, and list[i] refers to list[0]. So, it is the value stored in the variable list[0] that is printed. The second time through the loop, i is 1, and the value stored in list[1] is printed. The loop ends after printing the value of list[4], when i becomes equal to 5 and the continuation condition “i < list.length” is no longer true. This is a typical example of using a loop to process an array. I’ll discuss more examples of array processing throughout this chapter.

Every use of a variable in a program specifies a memory location. Think for a moment about what the computer does when it encounters a reference to an array element, list[k], while it is executing a program. The computer must determine which memory location is being referred to. To the computer, list[k] means something like this: “Get the pointer that is stored in the variable, list. Follow this pointer to find an array object. Get the value of k. Go to the k-th position in the array, and that’s the memory location you want.” There are two things that can go wrong here.

 

Chapter 7.1.2 - Using Arrays | Introduction to Programming Using Java

 

Suppose that the value of list is null. If that is the case, then list doesn’t even refer to an array. The attempt to refer to an element of an array that doesn’t exist is an error that will cause an exception of type NullPointerException to be thrown. The second possible error occurs if list does refer to an array, but the value of k is outside the legal range of indices for that array. This will happen if k < 0 or if k >= list.length. This is called an “array index out of bounds” error.

When an error of this type occurs, an exception of type ArrayIndexOutOfBoundsException is thrown. When you use arrays in a program, you should be mindful that both types of errors are possible. However, array index out of bounds errors are by far the most common error when working with arrays.

 

 

 

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.1.2 – Using Arrays | Introduction to Programming Using Java”

Leave a Comment