Main Content

HowMATLAB代表JavaArrays

The termJava®大批refers to a container object that holds a fixed number of values of a single type. The type of an array is written astype[]. An array of arrays—also known as a multidimensional array—uses two or more sets of brackets, such asString[][].

The term方面refers 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 arrayyof lengthNusingy[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.

的形状JavaArrays

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.

Jagged Java array.

The MATLABstringfunction pads a jagged Java string array, making it a rectangular MATLAB array.

解释规模JavaArrays

The MATLABsizefunction returns the length of the Java array. The number of columns is always 1.

Java阵列的潜在粗糙的形状使得不可能以与MATLAB阵列相同的方式大小。在Java数组中,没有单个值表示较低级别阵列的大小。

For example, consider this Java array.

Multilevel Java array.

尺寸(a)returns the dimensions of the highest array level of A. The highest level of the array has a size of 3-by-1.

尺寸(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 thesizecommand using the following syntax. However, this command only sizes the first dimension,dim=1,唯一的非空缺维度。

m = size(X,dim) size(A,1)
ans = 3

Interpret Number of Dimensions ofJavaArrays

The MATLABndimsfunction 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显示a Java vector as a column but processes it as if it were a row vector. For examples, see连接java arrays..

See Also

|