Availability
Netscape 3 LiveConnect
Synopsis
javaarray.length // The length of the arrayjavaarray[index] // Read or write an array element
Properties
length
A read-only integer that specifies the number of elements in the Java
array represented by the JavaArray object.
Description
The JavaArray object is a JavaScript representation of a Java array
that allows JavaScript code to read and write the elements of the
array using familiar JavaScript array syntax. In addition, the
JavaArray object has a length field that specifies
the number of elements in the Java array.
When reading and writing values from array elements, data conversion
between JavaScript and Java representations is automatically handled
by the system. See Chapter 22 for full details.
Usage
Note that Java arrays differ from JavaScript arrays in a couple of
important aspects. First, Java arrays have a fixed length that is
specified when they are created. For this reason, the JavaArray
length field is read-only. The second difference
is that Java arrays are typed (i.e., their
elements must all be of the same type of data). Attempting to set an
array element to a value of the wrong type results in a JavaScript
error or exception.
Example
java.awt.Polygon is a JavaClass object. We can
create a JavaObject representing an instance of the class like this:
p = new java.awt.Polygon( );
The object p has properties
xpoints and ypoints, which are
JavaArray objects representing Java arrays of integers. We can
initialize the contents of these arrays with JavaScript code like the
following:
for(int i = 0; i < p.xpoints.length; i++)
p.xpoints[i] = Math.round(Math.random( )*100);
for(int i = 0; i < p.ypoints.length; i++)
p.ypoints[i] = Math.round(Math.random( )*100);
See Also
getClass( ), JavaClass, JavaObject, JavaPackage, the
java property of the Window object; Chapter 22
•
Table of Contents
•
Index
•
Reviews
•
Examples
•
Reader Reviews
•
Errata
JavaScript: The Definitive Guide, 4th Edition
By
David Flanagan
Publisher
: O'Reilly
Pub Date
: November 2001
ISBN
: 0-596-00048-0
Pages
: 936
Slots
: 1
This fourth edition of the definitive reference to
JavaScript, a scripting language that can be embedded
directly in web pages, covers the latest version of the
language, JavaScript 1.5, as supported by Netscape 6 and
Internet Explorer 6. The book also provides complete
coverage of the W3C DOM standard (Level 1 and Level 2),
while retaining material on the legacy Level 0 DOM for
backward compatibility.