// Create an int array of 2 elements, populate it, and print the elementsvar intArray = java.lang.reflect.Array.newInstance(java.lang.Integer.TYPE, 2);intArray[0] = 100;intArray[1] = 200;for(var i = 0; i < intArray.length; i++) {        print(intArray[i]);}
100200

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 snippet of code shows how to create a String[] of two elements in Nashorn:

// Get the java.lang.String[] typevar StringArray = Java.type("java.lang.String[]");// Create a String[] array of 2 elementsvar strArray = new StringArray (2);strArray[0] = "Hello";strArray[1] = "Array";for(var ...

Get Beginning Java 8 APIs, Extensions and Libraries Swing, JavaFX, JavaScript, JDBC and Network Programming APIs now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.