HowMATLAB代表JavaArrays
The termJava®arrayrefers to a container object that holds a fixed number of values of a single type. The type of an array is written as
. An array of arrays—also known as a multidimensional array—uses two or more sets of brackets, such astype
[]String[][]
.
The termdimensionrefers to the number of subscripts required to address the elements of an array. Dimension is not a measure of length, width, and height. For example, a 5-by-1 array is one-dimensional, because you use one subscript to access an individual element. To work with a two-dimensional array, create an array of arrays. To add further dimensions, add more levels to the array, making it an array of arrays of arrays, and so on.
MATLAB®treats multilevel Java arrays like matrices and multidimensional arrays. Use the same MATLAB syntax to access elements of a Java array.
Array Indexing
Java array indices are zero-based while MATLAB array indices are one-based. In Java programming, you access the elements of arrayy
of lengthN
usingy[0]
throughy[N-1]
. When working with this array in MATLAB, you access these elements usingy(1)
throughy(N)
.
For an example, seeAccess Elements of Java Array.
Shape ofJavaArrays
A two-dimensional MATLAB array is a rectangle, as each row is of equal length and each column of equal height. A Java array is an array of arrays and does not necessarily hold to this rectangular form. Each individual lower-level array might have a different length.
This image shows an array of three underlying arrays of different lengths. The termjagged(或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 a jagged array of primitive Java types, MATLAB stores it in a cell array.
The MATLABstring
function pads a jagged Java string array, making it a rectangular MATLAB array.
Interpret Size ofJavaArrays
The MATLABsize
function returns the length of the Java array. The number of columns is always 1.
The potentially ragged shape of a Java array makes it impossible to size the array in the same way as for a MATLAB array. In a Java array, no single value represents the size of the lower-level arrays.
For example, consider this Java array.
size(A)
returns the dimensions of the highest array level of A. The highest level of the array has a size of 3-by-1.
size(A)
ans = 3 1
To find the size of a lower-level array, for example the five-element array in row 3, refer to the row explicitly.
size(A(3))
ans = 5 1
You can specify a dimension in thesize
command using the following syntax. However, this command only sizes the first dimension,dim=1
,唯一nonunary dimension.
m = size(X,dim) size(A,1)
ans = 3
Interpret Number of Dimensions ofJavaArrays
The MATLABndims
function always returns a value of 2 for the number of dimensions in a Java array. This value is the number of dimensions in the top-level array.
DisplayJavaVector
MATLAB显示Javavector as a column but processes it as if it were a row vector. For examples, seeConcatenate Java Arrays.