nashorn/docs/JavaScriptingProgrammersGuide.html
changeset 16522 d643e3ee819c
parent 16212 363737d0d757
child 16784 ba0dd689dd98
equal deleted inserted replaced
16346:5e8c55025644 16522:d643e3ee819c
   531 </code>
   531 </code>
   532 </pre>
   532 </pre>
   533 <hr>
   533 <hr>
   534 <a name="jsarrays" id="jsarrays"></a>
   534 <a name="jsarrays" id="jsarrays"></a>
   535 <h3>Creating, Converting and Using Java Arrays</h3>
   535 <h3>Creating, Converting and Using Java Arrays</h3>
   536 <p>While creating a Java object is the same as in Java, to create
   536 <p>
   537 Java arrays in JavaScript we can use Java reflection
   537 Array element access or length access is
   538 explicitly. But once created the element access or length access is
       
   539 the same as in Java. Also, a script array can be used when a Java
   538 the same as in Java. Also, a script array can be used when a Java
   540 method expects a Java array (auto conversion). So in most cases we
   539 method expects a Java array (auto conversion). So in most cases we
   541 don't have to create Java arrays explicitly.</p>
   540 don't have to create Java arrays explicitly.</p>
   542 <pre><code>
   541 <pre><code>
   543 // <a href="source/javaarray.js">javaarray.js</a>
   542 // <a href="source/javaarray.js">javaarray.js</a>
   544 
   543 
   545 // create Java String array of 5 elements
   544 // create Java String array of 5 elements
   546 var a = java.lang.reflect.Array.newInstance(java.lang.String.class, 5);
   545 var StringArray = Java.type("java.lang.String[]");
       
   546 var a = new StringArray(5);
   547 
   547 
   548 // Accessing elements and length access is by usual Java syntax
   548 // Accessing elements and length access is by usual Java syntax
   549 a[0] = "scripting is great!";
   549 a[0] = "scripting is great!";
   550 print(a.length);
   550 print(a.length);
   551 print(a[0]);
   551 print(a[0]);