Java Reference
In-Depth Information
Chapter 7
Collections
In this chapter, you will learn:
•
What arrays in Nashorn are
•
How to create arrays in Nashorn using array literals and the
Array
object
Array
object
•
How to use different methods of the
•
How to work with array-like objects
•
How to create and use typed arrays in Nashorn
•
How to use Java collections in Nashorn
•
How to create Java arrays in Nashorn
•
How to convert Java arrays to Nashorn arrays and vice versa
What Is an Array in Nashorn?
An array in Nashorn is a specialized object, called an
Array
object, which is used to
represent an ordered collection of values. An
Array
object treats certain property names
in a special way. If a property name can be converted to an integer that is between 0 and
2
32
-2 (inclusive), such a property name is known as an array
index
and the property is
known as an
element
. In other words, an element in an array is a special property whose
name is an array index. Apart from adding elements to an array, you can add any other
properties as you have been doing to Nashorn objects. Notice that, in an
Array
object,
every array index is a property, but every property is not an array index. For example, you
can add two properties named “0” and “name” to an array where “0” is an array index
because it can be converted to an integer, whereas “name” is simply a property.
Every
Array
object has a property named
length
whose value is greater than the
index of all elements. When elements are added,
length
is automatically adjusted. If
length
is changed, the elements whose indexes are greater than or equal to the new
length
are deleted. Note that, unlike Java arrays, Nashorn arrays are variable-length
arrays. That is, Nashorn arrays are not fix-length arrays; they can expand and shrink when
elements are added and removed, or the
length
property is changed.
Search WWH ::
Custom Search