Main Content

Create Java Array References

Java® arrays in MATLAB® are references. Assigning an array variable to another variable results in a second reference to the array, not a copy of the array. For example, create and initialize origArray.

origArray = javaArray('java.lang.Double',3,4);
for m = 1:3
    for n = 1:4
        origArray(m,n) = java.lang.Double((m*10)+n);
    end
end
origArray
origArray =

  java.lang.Double[][]:

    [11]    [12]    [13]    [14]
    [21]    [22]    [23]    [24]
    [31]    [32]    [33]    [34]

Create a second reference to the array newArrayRef.

newArrayRef = origArray;

Change the array referred by newArrayRef. The changes also show up in origArray.

newArrayRef(3,:) = java.lang.Double(0);
origArray
origArray =

  java.lang.Double[][]:

    [11]    [12]    [13]    [14]
    [21]    [22]    [23]    [24]
    [ 0]    [ 0]    [ 0]    [ 0]

Related Topics