[ Pobierz całość w formacie PDF ]

is quick to hop around. However, when using large data structures, linear access performs best and avoids
unnecessary swapping. In addition, you can simulate multidimensional arrays by packing them into a
one-dimensional array. This is done frequently with images. The two manners of packing the
one-dimensional array are row-major order, where the array is filled one row at a time; and column-major
order, where columns are placed into the array. Figure 2-5 shows the difference between the two.
Figure 2-5: Row-major versus column-major order.
Note In many of the image processing routines, such as setPixels() of the ImageFilter class, you'll
find two-dimensional image arrays flattened into row-major order, where Pixel (m, n)
translates into a one-dimensional position, n * scansize + m. This reads top-down,
left-to-right through the image data.
Initializing Arrays
When an array is first created, the runtime-environment will make sure that the array contents are
automatically initialized to some known (as opposed to undefined) value. As with uninitialized instance and
class variables, array contents are initialized to either the numerical equivalent of zero, the character
equivalent of \u0000, the boolean false, or null for object arrays, as shown in Table 2-1.
Table 2-1: Array Initial Values
DEFAULT VALUE ARRAY
0 byte
short
int
long
0.0 float
double
\u0000 char
false boolean
null Object
When you declare an array you can specify the initial values of the elements. This is done by providing a
comma-delimited list between braces [{ }] after an equal sign at the declaration point.
For instance, the following will create a three-element array of names:
String names[] = {"Leonardo", "da", "Vinci"};
11
Passing Array Arguments and Return Values
Notice that when you provide an array initializer, you do not have to specify the length. The array length is set
automatically based upon the number of elements in the comma-delimited list.
Note The Java language syntax permits a trailing comma after the last element in an array initializer block, as
in {"Leonardo", "da", "Vinci",}. This does not change the length of the array to four, but keeps it at
three. This flexibility is primarily for the benefit of code generators.
For multidimensional arrays, you would just use an extra set of parenthesis for each added dimension. For
instance, the following creates a 6 × 2 array of years and events. Because the array is declared as an array of
Object elements, it is necessary to use the Integer wrapper class to store each int primitive value inside. All
elements within an array must be of the array's declared type, or a subclass of that type, in this case, Object,
even though all of the elements are subclasses.
Object events[][] = {
{new Integer(1452), new Birth("Italy")},
{new Integer(1472), new Painting("baptismOfChrist.jpg")},
{new Integer(1483), new Theory("Helicopter")},
{new Integer(1495), new Theory("Parachute")},
{new Integer(1503), new Painting("monaLisa.jpg")},
{new Integer(1519), new Death("France")}
};
Note In the event the type of the array is an interface, all elements in the array must implement the interface.
Starting with the second dot-point Java release (Java 1.1), the concept of anonymous arrays was introduced.
While it was easy to initialize an array when it was declared, you couldn't reinitialize the array later with a
comma-delimited list unless you declared another variable to store the new array in. This is where
anonymous arrays step in. With an anonymous array, you can reinitialize an array to a new set of values, or
pass unnamed arrays into methods when you don't want to define a local variable to store said array.
Anonymous arrays are declared similarly to regular arrays. However, instead of specifying a length within the
square brackets, you place a comma-delimited list of values within braces after the brackets, as shown here:
new type[] {comma-delimited-list}
To demonstrate, the following line shows how to call a method and pass to it an anonymous array of String
objects:
method(new String[] {"Leonardo", "da", "Vinci"});
You'll find anonymous arrays used frequently by code generators. [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • kucharkazen.opx.pl