Main Content

Access Elements of Java Array

MATLAB Array Indexing

To access elements of a Java® object array, use the MATLAB® array indexing syntax, A(row,column). In a Java program, the syntax is A[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.

for m = 1:4
    for n = 1:5
        matlabArr(m,n) = (m*10)+n;
    end
end
matlabArr
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);
for m = 1:4
    for n = 1:5
        javaArr(m,n) = java.lang.Integer(matlabArr(m,n));
    end
end
javaArr
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 the colon operator (:). For example, create a 4-by-5 Java array.

dblArray = javaArray('java.lang.Double',4,5);
for m = 1:4
    for n = 1:5
        dblArray(m,n) = java.lang.Double((m*10)+n);
    end
end
dblArray
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 subarray row2Array from 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 array dblArray into 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 the end keyword as the first subscript. For example, display data from the third to the last rows of Java array dblArray.

last2rows = dblArray(3:end,:)
last2rows =

  java.lang.Double[][]:

    [31]    [32]    [33]    [34]    [35]
    [41]    [42]    [43]    [44]    [45]

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

Converting Object Array Elements to MATLAB Types

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

For example, if a java.lang.Object array contains a java.lang.Double element, then MATLAB converts the element to a MATLAB double. However, MATLAB does not convert a java.lang.Double element in a java.lang.Double array. MATLAB returns it as java.lang.Double.

Related Topics