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. A copy of this array variable is created by the statement newArrayRef = origArray
. Changes made to the array referred to by newArrayRef
also show up in the original array.
origArray = javaArray('java.lang.Double', 3, 4); for i = 1:3 for j = 1:4 origArray(i,j) = java.lang.Double((i * 10) + j); 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 | ![]() |