Main Content

Create Copy of Java Array

To make a copy of an existing Java® array, use subscript indexing. 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]

Copy the entire contents to newArray.

newArray = origArray(:,:)
newArray =

  java.lang.Double[][]:

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

Change elements of newArray.

newArray(3,:) = java.lang.Double(0)
newArray =

  java.lang.Double[][]:

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

The values in origArray do not change.

origArray
origArray =

  java.lang.Double[][]:

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

Related Topics