JavaArray
.JavaArray
with an arbitrary data type using the newInstance
method of the Array
class:
public static Object newInstance(Class componentType,
int length)
throws NegativeArraySizeException
JavaArray
object is an instance of a Java array that is created in or passed to JavaScript. JavaArray
is a wrapper for the instance; all references to the array instance are made through the JavaArray
.
In JavaScript 1.4 and later, the componentType
parameter is either a JavaClass
object representing the type of the array or class object, such as one returned by java.lang.Class.forName
. In JavaScript 1.3 and earlier, componentType
must be a class object.
Use zero-based indexes to access the elements in a JavaArray
object, just as you do to access elements in an array in Java. For example:
var javaString = new java.lang.String("Hello world!");Any Java data brought into JavaScript is converted to JavaScript data types. When the
var byteArray = javaString.getBytes();
byteArray[0] // returns 72
byteArray[1] // returns 101
JavaArray
is passed back to Java, the array is unwrapped and can be used by Java code. See the Core JavaScript Guide for more information about data type conversions.
In JavaScript 1.4 and later, the methods of java.lang.Object
are inherited by JavaArray
.
java.lang.Object
are not inherited by JavaArray
. In addition, the toString
method is inherited from the Object
object and returns the following value:
[object JavaArray]You must specify a class object, such as one returned by
java.lang.Object.forName
, for the componentType
parameter of newInstance
when you use this method to create an array. You cannot use a JavaClass
object for the componentType
parameter.
Property |
Description
The number of elements in the Java array represented by |
---|
JavaArray
also inherits methods from the Java array superclass, java.lang.Object
.
JavaArray
in JavaScript.
In this example, the JavaArray
byteArray
is created by the java.lang.String.getBytes
method, which returns an array.
var javaString = new java.lang.String("Hello world!");Example 2. Instantiating a
var byteArray = javaString.getBytes();
JavaArray
in JavaScript with the newInstance
method.
In JavaScript 1.4, you can use a JavaClass
object as the argument for the newInstance
method which creates the array, as shown in the following code:
var dogs = java.lang.reflect.Array.newInstance(java.lang.String, 5)In JavaScript 1.1, use a class object returned by
java.lang.Class.forName
as the argument for the newInstance
method, as shown in the following code:
var dataType = java.lang.Class.forName("java.lang.String")
var dogs = java.lang.reflect.Array.newInstance(dataType, 5)
JavaArray
object.Array.length
, JavaArray.length
is a read-only property. You cannot change the value of the JavaArray.length
property because Java arrays have a fixed number of elements.
Array.length
java.lang.Object.toString
, which returns the value of the following expression:
JavaArray.getClass().getName() + '@' +
java.lang.Integer.toHexString(JavaArray.hashCode())
toString
method is inherited from the Object
object and returns the following value:
[object JavaArray]
Last Updated: 10/29/98 20:17:15