Functionality Provided By Java Array Types


What Is An Array?

An array is a contiguous memory allocation of same-sized or homogeneous data-type elements. The word contiguous means the array elements are located one after the other in memory. The term same-sized means that each array element occupies the same amount of memory space. The size of each array element is determined by the type of objects an array is declared to contain. So, for example, if an array is declared to contain integer primitive types, each element would be the size of an integer and occupy 4-bytes. If, however, an array is declared to contain double primitive types, the size of each element would be 8-bytes. The term homogeneous is often used in place of the term same-sized to refer to objects having the same data type and therefore the same size. Figure 8-1 illustrates these concepts.

image from book
Figure 8-1: Array Elements are Contiguous and Homogeneous

Figure 8-1 shows an array of 5 elements of no particular type. The elements are numbered consecutively beginning with 1 denoting the first element and 5 denoting the last, or 5th, element in the array. Each array element is referenced or accessed by its array index number. Index numbers are always one less than the element number you wish to access. For example, when you want to access the 1st element of an array you will use index number 0. To access the 2nd element of an array you will use index number 1, etc.

The number of elements an array contains is referred to as its length. The array shown in figure 8-1 contains 5 elements so it has a length of 5. The index numbers associated with this array will range from 0 to 4 (that is 0 to (length - 1)).

Specifying Array Types

Array elements can be primitive types, reference types, or arrays of these types. When you declare an array you must specify the type its elements will contain. Figure 8-2 illustrates this concept through the use of the array declaration and allocation syntax.

image from book
Figure 8-2: Specifying Array Component Type

Figure 8-2 shows the array declaration and allocation syntax for a single-dimensional array having a particular type and length. The declaration begins with the array element type. The elements of an array can be primitive types or reference types. Reference types can include any reference type (class or interface) specified in the Java API or reference types you or someone else create.

The element type is followed by a set of empty brackets. Single-dimensional arrays use one set of brackets. You will add a set of brackets for each additional dimension you want the array to have. (I cover single- and multidimensional arrays in greater detail below.) The element type plus the brackets yield an array type. This array type is followed by an identifier that declares the name of the array. To actually allocate memory for an array use the new operator followed by the type of elements the array can contain followed by the length of the array in brackets. The new operator returns a number representing the memory location of the newly created array object and the assignment operator assigns it to the array_reference_name.

Figure 8-2 combines the act of declaring an array and the act of creating an array object into one line of code. If required, you could declare an array in one statement and create the array in another. For example, the following line of code declares and allocates memory for a single-dimensional array of integers having length 5:

 int[] int_array = new int[5];

The following line of code would simply declare an array of floats:

 float[] float_array;

And this code would then allocate enough memory to hold 10 float values:

 float_array = new float[10];

The following line of code would declare a two-dimensional array of boolean-type elements and allocate some memory:

 boolean[][] boolean_array_2d = new boolean[10][10];

The following line of code would create a single-dimensional array of Strings:

 String[] string_array = new String[8];

You will soon learn the details about single- and multidimensional arrays. If the preceding concepts seem confusing now just hang in there. By the time you complete this chapter you will be using arrays like a pro!

Quick Review

Arrays are contiguously allocated memory elements of homogeneous data types. Contiguous means the elements are arranged in memory one after the other. Homogeneous means each element of the array is of the same data type. An array containing n elements is said to have a length equal to n. Array elements are accessed via their index value which ranges from 0 to length - 1. The index value of a particular array element is always one less than the element number you wish to access. (i.e., 1st element has index 0, 2nd element has index 1,..., the nth element has index n-1)




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net