External Interfaces/API |
Creating a New Array Reference
Because Java arrays in MATLAB are references, assigning an array variable to another variable results in a second reference to the array.
Consider the following example where two separate array variables reference a common array. The original array, origArray
, is created and initialized. The statement newArrayRef = origArray
creates a copy of this array variable. Changes made to the array referred to by newArrayRef
also show up in the original array.
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] % ----- Make a copy of the array reference ----- newArrayRef = origArray; newArrayRef(3,:) = java.lang.Double(0); origArray origArray = java.lang.Double[][]: [11] [12] [13] [14] [21] [22] [23] [24] [ 0] [ 0] [ 0] [ 0]
Concatenating Java Arrays | Creating a Copy of a Java Array |