Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Arrays are first-class objects

java



+ Font mai mare | - Font mai mic



Arrays are first-class objects

Regardless of what type of array you're working with, the array identifier is actually a handle to a true object that's created on the heap. The heap object can be created either implicitly, as part of the array initialization syntax, or explicitly with a new expression. Part of the heap object (in fact, the only field or method you can access) is the read-only length member that tells you how many elements can be stored in that array object. The '[]' syntax is the only other access that you have to the array object.



The following example shows the various ways that an array can be initialized, and how the array handles can be assigned to different array objects. It also shows that arrays of objects and arrays of primitives are almost identical in their use. The only difference is that arrays of objects hold handles while arrays of primitives hold the primitive values directly. (See page if you have trouble executing this program.)

//: ArraySize.java

// Initialization & re-assignment of arrays

package c08;

class Weeble // A small mythical creature

public class ArraySize ;

// Compile error: variable a not initialized:

//!System.out.println('a.length=' + a.length);

System.out.println('b.length = ' + b.length);

// The handles inside the array are

// automatically initialized to null:

for(int i = 0; i < b.length; i++)

System.out.println('b[' + i + ']=' + b[i]);

System.out.println('c.length = ' + c.length);

System.out.println('d.length = ' + d.length);

a = d;

System.out.println('a.length = ' + a.length);

// Java 1.1 initialization syntax:

a = new Weeble[] ;

System.out.println('a.length = ' + a.length);

// Arrays of primitives:

int[] e; // Null handle

int[] f = new int[5];

int[] g = new int[4];

for(int i = 0; i < g.length; i++)

g[i] = i*i;

int[] h = ;

// Compile error: variable e not initialized:

//!System.out.println('e.length=' + e.length);

System.out.println('f.length = ' + f.length);

// The primitives inside the array are

// automatically initialized to zero:

for(int i = 0; i < f.length; i++)

System.out.println('f[' + i + ']=' + f[i]);

System.out.println('g.length = ' + g.length);

System.out.println('h.length = ' + h.length);

e = h;

System.out.println('e.length = ' + e.length);

// Java 1.1 initialization syntax:

e = new int[] ;

System.out.println('e.length = ' + e.length);

}

} ///:~

Here's the output from the program:

b.length = 5

b[0]=null

b[1]=null

b[2]=null

b[3]=null

b[4]=null

c.length = 4

d.length = 3

a.length = 3

a.length = 2

f.length = 5

f[0]=0

f[1]=0

f[2]=0

f[3]=0

f[4]=0

g.length = 4

h.length = 3

e.length = 3

e.length = 2

The array a is initially just a null handle, and the compiler prevents you from doing anything with this handle until you've properly initialized it. The array b is initialized to point to an array of Weeble handles, but no actual Weeble objects are ever placed in that array. However, you can still ask what the size of the array is, since b is pointing to a legitimate object. This brings up a slight drawback: you can't find out how many elements are actually in the array, since length tells you only how many elements can be placed in the array; that is, the size of the array object, not the number of elements it actually holds. However, when an array object is created its handles are automatically initialized to null so you can see whether a particular array slot has an object in it by checking to see whether it's null. Similarly, an array of primitives is automatically initialized to zero for numeric types, null for char, and false for boolean.

Array c shows the creation of the array object followed by the assignment of Weeble objects to all the slots in the array. Array d shows the "aggregate initialization" syntax that causes the array object to be created (implicitly with new on the heap, just like for array c) and initialized with Weeble objects, all in one statement.

The expression

a = d;

shows how you can take a handle that's attached to one array object and assign it to another array object, just as you can do with any other type of object handle. Now both a and d are pointing to the same array object on the heap.

Java 1.1 adds a new array initialization syntax, which could be thought of as a "dynamic aggregate initialization." The Java 1.0 aggregate initialization used by d must be used at the point of d's definition, but with the Java 1.1 syntax you can create and initialize an array object anywhere. For example, suppose hide( ) is a method that takes an array of Weeble objects. You could call it by saying:

hide(d);

but in Java 1.1 you can also dynamically create the array you want to pass as the argument:

hide(new Weeble[] );

This new syntax provides a more convenient way to write code in some situations.

The second part of the above example shows that primitive arrays work just like object arrays except that primitive arrays hold the primitive values directly.

Collections of primitives

Collection classes can hold only handles to objects. An array, however, can be created to hold primitives directly, as well as handles to objects. It is possible to use the "wrapper" classes such as Integer, Double, etc. to place primitive values inside a collection, but as you'll see later in this chapter in the WordCount.java example, the wrapper classes for primitives are only somewhat useful anyway. Whether you put primitives in arrays or wrap them in a class that's placed in a collection is a question of efficiency. It's much more efficient to create and access an array of primitives than a collection of wrapped primitives.

Of course, if you're using a primitive type and you need the flexibility of a collection that automatically expands when more space is needed, the array won't work and you're forced to use a collection of wrapped primitives. You might think that there should be a specialized type of Vector for each of the primitive data types, but Java doesn't provide this for you. Some sort of templatizing mechanism might someday provide a better way for Java to handle this problem.



This is one of the places where C++ is distinctly superior to Java, since C++ supports parameterized types with the template keyword.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 640
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved