External Interfaces/API | ![]() ![]() |
Concatenating Java Arrays
The degree of concatenation you can perform on Java arrays is limited due to the significant structural differences between MATLAB and Java arrays. The counterpart of a multidimensional MATLAB array in Java is a considerably more complex structure built upon arrays of the one-dimensional Java array. The individual lower level arrays in this structure can be of varying lengths, giving the overall array structure an uneven, or ragged, look. This quality in the Java array results in a greater chance of incompatibility between arrays and thus limits the degree to which they can be joined together by the MATLAB concatenation operation.
If the Java arrays being concatenated are of the same class (or the class of one array is assignable to that of the other(s)), then they are considered to be assignment compatible. In this case, the class of the resultant array is the class that is compatible with all of the constituent arrays. And the length of the resultant array is the sum of the lengths of each constituent array.
Consider the two arrays of java.awt.Frame
shown below. The frmArray1
array is a 3-by-5 array of Frame objects. The frmArray2
array is a 7-by-12 array of the same. The cat
function is used to concatenate the two arrays. The result is a 10-element array of the java.awt.Frame class.
Note
When you use the cat function, the first argument (specifying the axis along which to perform the concatenation), must be 1. Java objects can only be concatenated along the first axis.
|
frmArray1 = javaArray('java.awt.Frame', 3, 5); frmArray2 = javaArray('java.awt.Frame', 7, 12); twoFrames = cat(1, frmArray1, frmArray2); class(twoFrames) ans = java.awt.Frame[][] length(twoFrames) ans = 10
If the constituent arrays are not assignment compatible, then the resultant array is of the java.lang.Object
class. The length of the array is equal to the number of arrays being combined in the concatenation. For example,
ptArray = javaArray('java.awt.Point', 8, 5); dblArray = javaArray('java.lang.Double', 8, 5); class([ptArray dblArray]) ans = java.lang.Object[] length([ptArray dblArray]) ans = 2
![]() | Assigning to a Java Array | Creating a New Array Reference | ![]() |