Java Reference
In-Depth Information
// The following statement will throw an IndexOutOfBoundsException because
// it is trying to add a new element using teh array syntax. You can only
// update an element, not add a new element, using the array syntax.
// nameList[3] = "An";
print("After updating names:");
for(var i = 0, len = nameList.length; i < len; i++) {
print(nameList[i]);
}
// Sort the list in natural order
nameList.sort(null);
// Use the Array.prototype.forEach() method to print the list
print("After sorting names:");
Array.prototype.forEach.call(nameList, function (value, index, data) {
print(value);
});
After adding names:
Lu
Do
Yu
After updating names:
Su
So
Bo
After sorting names:
Bo
So
Su
The example performs several things:
•
Creates an instance of the
java.util.ArrayList
•
Use the
add()
method of the Java
List
interface to add three
elements to the list
•
Prints the elements using the
size()
and
get()
method of the
List
interface
Updates the elements and prints them using the Nashorn syntax
•
for accessing array elements. It uses the Nashorn property
length
to get the size of the list
Search WWH ::
Custom Search