Skill-Building Exercises


Summary

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 array types have special functionality because of their special inheritance hierarchy. Java array types directly subclass the java.lang.Object class and implement the Cloneable and Serializable interfaces. There is also one corresponding java.lang.Class object created in memory for each array-type in the program.

Single-dimensional arrays have one dimension — length. You can get an array’s length by accessing its length attribute. Arrays can have elements of primitive or reference types. An array type is created by specifying the type name of array elements followed by one set of brackets [ ]. Use the java.lang.Object and java.lang.Class methods to get information about an array.

Each element of an array is accessed via an index value contained in a set of brackets. Primitive-type element values can be directly assigned to array elements. When an array of primitive types is created each element is initialized to a default value that’s appropriate for the element type. Each element of an array of references is initialized to null. Each object that each reference element points to must be dynamically created at some point in the program.

Multidimensional arrays are arrays having more than one dimension. Multidimensional arrays in Java are arrays of arrays. An understanding of single-dimensional Java arrays leads to quick mastery of multidimensional arrays.

To declare a multidimensional array simply add a set of brackets to the array element type for each dimension required in the array. Multidimensional arrays can contain any number of dimensions, however, arrays of two and three dimensions are most common.

Multidimensional arrays can be created using array literal values. The use of array literals enables the initialization of each array element at the point of declaration.

Ragged arrays are multidimensional arrays having final element arrays of various lengths. Ragged arrays can be easily created with array literals. They can also be created by omitting the final dimension at the time of array creation and then dynamically creating each final array with a unique length.

Use the java.util.Arrays class to perform common manipulations such as searching and sorting on arrays of primitive and reference types.




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