Java Reference
In-Depth Information
Multidimensional Arrays
Nashorn does have any special construct to support multidimensional arrays. You need
to use an array of arrays (a zagged array) to create a multidimensional arrays in which
elements of an array can be arrays. Typically, you will need nested for loops to populate
and access the elements of a multidimensional array. The following code demonstrates
how to create, populate, and print the elements of a 3x3 matrix. The code uses the
join() method of the Array object to concatenate elements of the array using a tab as a
separator:
var ROWS = 3;
var COLS = 3;
// Create a 3x3 array, so you pre-allocate the memory
var matrix = new Array(ROWS);
for(var i = 0; i < ROWS; i++) {
matrix[i] = new Array(COLS);
}
// Populate the array
for(var i = 0; i < ROWS; i++) {
for(var j = 0; j < COLS; j++) {
matrix[i][j] = i + "" + j;
}
}
// Print the array elements
for(var i = 0; i < ROWS; i++) {
var rowData = matrix[i].join("\t");
print(rowData);
}
00 01 02
10 11 12
20 21 22
Methods of the Array Object
The Array.prototype object defines several methods that are available as an instance
methods for all Array objects. In this section, I will discuss those methods. All array
methods are intentionally generic, so they can be applied to not just arrays but to any
array-like objects.
 
Search WWH ::




Custom Search