Java Reference
In-Depth Information
You can access the array elements using the bracket notation and the index of the
array elements. You can delete an array element using the
delete
operator. You can
iterate array elements using the
for
loop,
for..in
loop, and
for..each..in
loop. The
for..in
and
for..each..in
loops iterate over elements as well as nonelement properties
of the array. The
Array.prototype
object contains a
forEach()
method that iterates over
only array elements and calls back the passed function for each element.
The
Array
object contains several useful methods to work with array elements. The
Array.isArray(object)
static method returns
true
if the specified
object
is an array;
otherwise, it returns
false
. Other methods such as
concat()
,
join()
,
slice()
,
splice()
,
sort()
, and so on are in the
Array.prototype
object.
An array-like object is a Nashorn object that has a
length
property and property
names that are valid array indexes. String objects and
arguments
object are examples of
array-like objects. Most of the methods in the
Array
object are generic and they can be
used on any array-like object.
Nashorn supports typed arrays. A typed array is a typed view of an
ArrayBuffer
object that contains raw binary data. Different types of typed arrays are available such
as
Int8Array
to deal with 8-bit signed integers,
Int32Array
to deal with 32-bit signed
integers, etc. Once you create a typed array, you can use it like an array to deal with the
specific types of values. The
DataView
object provides a low-level interface to deal with an
ArrayBuffer
object whose binary data may contain different types of numeric values.
All Nashorn objects are maps. Nashorn lets you treat Java
Map
s in a special way
by allowing you to access their values using the bracket notation with keys as indexes.
Nashorn lets you treat Java
List
s like Nashorn arrays. You can read and update elements
of Java
List
s using the bracket notation where element's indexes are treated as indexes
in the arrays. Nashorn creates a special property named
length
for Java
List
objects that
represents the number of elements in the
List
. If you need any other types of collections
in Nashorn, you can use the corresponding collections from Java.
Nashorn lets you use Java arrays in scripts. You need to use the
Java.type()
method
to import the Java array type in the script. For example.
Java.type("int[]")
returns a
Java type that represents a Java
int[]
. Once you get the Java array type, you need to use
the
new
operator to create the array. Nashorn also supports Nashorn-to-Java and Java-to-
Nashorn array conversions. The
Java.to()
method converts a Nashorn array to a Java
array and the
Java.from()
method converts a Java array to a Nashorn array. Nashorn
attempts to perform automatic conversion from Nashorn arrays to Java arrays,
List
s, and
Map
s, provided the choice of type conversion is unique. In other cases, you will need to
perform explicit conversion from Nashorn arrays to collections.
Search WWH ::
Custom Search