User`s manual
5 Calling Java from MATLAB
5-32
Array Indexing
Java array indexing is different than MATLAB array indexing. Java array
indices are zero-based, MATLAB array indices are one-based. In Java
programming, you access the elements of array
y of length N using y[0]
through
y[N-1]. When working with this array in MATLAB, you access these
same elements using the MATLAB indexing style of
y(1) through y(N). Thus,
if you have a Java array of 10 elements, the seventh element is obtained using
y(7), and not y[6] as you would have used when writing a program in Java.
The Shape of the Java Array
A Java array can be different from a MATLAB array in its overall shape. A
two-dimensional MATLAB array maintains a rectangular shape, as each row
is of equal length and each column of equal height. The Java counterpart of
this, an array of arrays, does not necessarily hold to this rectangular form.
Each individual lower level array may have a different length.
Such an array structure is pictured below. This is an array of three underlying
arrays of different lengths. The term ragged is commonly used to describe this
arrangement of array elements as the array ends do not match up evenly.
When a Java method returns an array with this type of structure, it is stored
in a cell array by MATLAB.
Interpreting the Size of a Java Array
When the MATLAB size function is applied to a simple Java array, the
number of rows returned is the length of the Java array and the number of
columns is always 1.
Determining the size of a Java array of arrays is not so simple. The potentially
ragged shape of an array returned from Java makes it impossible to size the
array in the same way as for a rectangular matrix. In a ragged Java array,
there is no one value that represents the size of the lower level arrays.
jArray[0]
length = 5
jArray[1]
jArray[2]
length = 2
length = 3