主要内容

Access Elements ofJavaArray

MATLABArray Indexing

To access elements of a Java®object array, use the MATLAB®array indexing syntax,A(row,column). In a Java program, the syntax isA[row-1][column-1].

Single Subscript Indexing

When you refer to the elements of a MATLAB matrix with a single subscript, MATLAB returns a single element of the matrix. In contrast, single subscript (linear) indexing into a multidimensional Java array returns a subarray.

For example, create a MATLAB array.

form = 1:4forn = 1:5 matlabArr(m,n) = (m*10)+n;endendmatlabArr
matlabArr = 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 41 42 43 44 45

Copy the contents into a Java array.

javaArr = javaArray('java.lang.Integer'、4、5);form = 1:4forn = 1:5 javaArr(m,n) = java.lang.Integer(matlabArr(m,n));endendjavaArr
javaArr = java.lang.Integer[][]: [11] [12] [13] [14] [15] [21] [22] [23] [24] [25] [31] [32] [33] [34] [35] [41] [42] [43] [44] [45]

Index value 3 returns a single element in the MATLAB array.

matlabArr(3)
ans = 31

Index value 3 returns the entire third row in a Java array.

javaArr(3)
ans = java.lang.Integer[]: [31] [32] [33] [34] [35]

Linear indexing into a Java array enables you to specify an entire array from a larger array structure. Then you can manipulate it as an object.

Colon Operator Indexing

To specify a range of elements in an array, use thecolonoperator (:). For example, create a 4-by-5 Java array.

dblArray = javaArray('java.lang.Double'、4、5);form = 1:4forn = 1:5 dblArray(m,n) = java.lang.Double((m*10)+n);endenddblArray
dblArray = java.lang.Double[][]: [11] [12] [13] [14] [15] [21] [22] [23] [24] [25] [31] [32] [33] [34] [35] [41] [42] [43] [44] [45]

Create a subarrayrow2Arrayfrom the elements in columns 2 through 4.

row2Array = dblArray(2,2:4)
row2Array = java.lang.Double[]: [22] [23] [24]

You also can use the colon with linear indexing to refer to all elements in the entire matrix. However, Java and MATLAB arrays are stored differently in memory meaning that the order of the elements in a linear array is different. Java array elements are stored in a row-by-column format, an order that matches the rows of the matrix. MATLAB array elements are stored column-wise, an order that matches the columns. For example, convert the 4-by-5 arraydblArrayinto a 20-by-1 linear array.

linearArray = dblArray(:)
linearArray = java.lang.Double[]: [11] [12] [13] [14] [15] [21] [22] [23] [24] [25] [31] [32] [33] [34] [35] [41] [42] [43] [44] [45]

Using END in a Subscript

To reference the top-level array in a multilevel Java array, use theendkeyword as the first subscript. For example, display data from the third to the last rows of Java arraydblArray.

last2rows = dblArray(3:end,:)
last2rows = java.lang.Double[][]: [31] [32] [33] [34] [35] [41] [42] [43] [44] [45]

Do not useendon lower-level arrays. Because of the potentially ragged nature of the arrays, MATLAB cannot determine the end value. For more information, seeShape of Java Arrays.

Converting Object Array Elements toMATLABTypes

When you access an element of ajava.lang.Objectarray, MATLAB converts the element to a MATLAB type, based on the table injava.lang.Object Return Types. MATLAB does not convert elements of any other type of Java array.

For example, if ajava.lang.Objectarray contains ajava.lang.Doubleelement, then MATLAB converts the element to a MATLABdouble. However, MATLAB does not convert ajava.lang.Doubleelement in ajava.lang.Doublearray. MATLAB returns it asjava.lang.Double.

Related Topics