External Interfaces/API | ![]() ![]() |
Creating a Copy of a Java Array
You can create an entirely new array from an existing Java array by indexing into the array to describe a block of elements, (or subarray), and assigning this subarray to a variable. The assignment copies the values in the original array to the corresponding cells of the new array.
As with the example in section Creating a New Array Reference, an original array is created and initialized. But, this time, a copy is made of the array contents rather than copying the array reference. Changes made using the reference to the new array do not affect the original.
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 contents ----- newArray = origArray(:,:); newArray(3,:) = java.lang.Double(0); origArray origArray = java.lang.Double[][]: [11] [12] [13] [14] [21] [22] [23] [24] [31] [32] [33] [34]
![]() | Creating a New Array Reference | Passing Data to a Java Method | ![]() |