Java Reference
In-Depth Information
Nashorn supports a new syntax to create Java arrays. First, create the appropriate
Java array type using the Java.type() method, and then use the familiar new operator to
create the array. The following code shows how to create a String[] of two elements in
Nashorn:
// Get the java.lang.String[] type
var StringArray = Java.type("java.lang.String[]");
// Create a String[] array of 2 elements
var strArray = new StringArray(2);
// Populate the array
strArray[0] = "Hello";
strArray[1] = "Array";
for(var i = 0; i < strArray.length; i++) {
print(strArray[i]);
}
Hello
Array
Nashorn supports creating the arrays of primitive types the same way. The following
code creates an int[] of two elements in Nashorn:
// Get the int[] type
var IntArray = Java.type("int[]");
// Create a int[] array of 2 elements
var intArray = new IntArray(2);
intArray[0] = 100;
intArray[1] = 200;
for(var i = 0; i < intArray.length; i++) {
print(intArray[i]);
}
100
200
 
Search WWH ::




Custom Search