Java Reference
In-Depth Information
if you want to create multidimensional Java arrays in nashorn, the array type will
be as you declare the array in Java. For example, Java.type("int[][]") will import a Java
int[][] array type; using the new operator on the imported type will create the int[][] array.
Tip
You can use a Nashorn array when a Java array is expected. Nashorn will perform the
necessary conversion. Suppose that you have a PrintArray class, as shown in Listing 7-6,
that contains a print() method that accepts a String array as an argument.
Listing 7-6. A PrintArray Class
// PrintArray.java
package com.jdojo.script;
public class PrintArray {
public void print(String[] list) {
System.out.println("Inside print(String[] list):" + list.
length);
for(String s : list) {
System.out.println(s);
}
}
}
The following script passes a Nashorn array to the PrintArray.print(String[])
method. Nashorn takes care of converting the native array to a String array, as shown in
the output:
// Create a JavaScript array and populate it with three strings
var names = new Array();
names[0] = "Rhino";
names[1] = "Nashorn";
names[2] = "JRuby";
// Create an object of the PrintArray class
var PrintArray = Java.type("com.jdojo.script.PrintArray");
var pa = new PrintArray();
 
Search WWH ::




Custom Search