Main Content

Assign Values to Java Array

To assign values to objects in a Java® object array, use the MATLAB® command syntax. For example, the following statement assigns a value to Java array A of type java.lang.Double.

A(row,column) = java.lang.Double(value)

In a Java program, you would assign the value to A[row-1][column-1]. For more information on the differences between Java and MATLAB arrays, see How MATLAB Represents Java Arrays.

To run the examples in this topic, create a 4-by-5 array dblArray. The values displayed for dblArray depend on the order in which you run the examples.

dblArray = javaArray('java.lang.Double',4,5);
for m = 1:4
    for n = 1:5
        dblArray(m,n) = java.lang.Double((m*10)+n);
    end
end
dblArray
dblArray =

  java.lang.Double[][]:

    [11]    [12]    [13]    [14]    [15]
    [21]    [22]    [23]    [24]    [25]
    [31]    [32]    [33]    [34]    [35]
    [41]    [42]    [43]    [44]    [45]

Single Subscript Indexing Assignment

You can use single-subscript indexing to assign values to an array. For example, create a 5-by-1 Java array and assign it to a row of dblArray.

onedimArray = javaArray('java.lang.Double',5);
for k = 1:5
    onedimArray(k) = java.lang.Double(100*k);
end

Replace row 3 with the values of onedimArray.

dblArray(3) = onedimArray
dblArray =

  java.lang.Double[][]:

    [ 11]    [ 12]    [ 13]    [ 14]    [ 15]
    [ 21]    [ 22]    [ 23]    [ 24]    [ 25]
    [100]    [200]    [300]    [400]    [500]
    [ 41]    [ 42]    [ 43]    [ 44]    [ 45]

Linear Array Assignment

To assign a value to every element of a multidimensional Java array, use the MATLAB colon operator (:). For example, initialize the contents of dblArray to zero.

dblArray(:) = java.lang.Double(0)
dblArray =

  java.lang.Double[][]:

    [0]    [0]    [0]    [0]    [0]
    [0]    [0]    [0]    [0]    [0]
    [0]    [0]    [0]    [0]    [0]
    [0]    [0]    [0]    [0]    [0]

Use the colon operator as you would when working with MATLAB arrays. For example, assign one value to each row in dblArray.

dblArray(1,:) = java.lang.Double(125);
dblArray(2,:) = java.lang.Double(250);
dblArray(3,:) = java.lang.Double(375);
dblArray(4,:) = java.lang.Double(500)
dblArray =

  java.lang.Double[][]:

    [125]    [125]    [125]    [125]    [125]
    [250]    [250]    [250]    [250]    [250]
    [375]    [375]    [375]    [375]    [375]
    [500]    [500]    [500]    [500]    [500]

Empty Matrix Assignment

You can assign the empty matrix ([]) to a Java array element. MATLAB stores the null value, rather than a 0-by-0 array.

dblArray(2,2) = []
dblArray =

  java.lang.Double[][]:

    [125]    [125]    [125]    [125]    [125]
    [250]       []    [250]    [250]    [250]
    [375]    [375]    [375]    [375]    [375]
    [500]    [500]    [500]    [500]    [500]

Subscripted Deletion

If you assign an empty matrix to an entire row or column of a MATLAB array, then MATLAB removes that row or column from the array. When you assign the empty matrix to a Java array, the array maintains its dimensions.

For example, create a MATLAB array.

for m = 1:4
    for n = 1:5
        matlabArr(m,n) = (m*10) + n;
    end
end
matlabArr
matlabArr = 
    11    12    13    14    15
    21    22    23    24    25
    31    32    33    34    35
    41    42    43    44    45

Assign the empty matrix to the fourth column. This statement changes its dimensions from 4-by-5 to 4-by-4.

matlabArr(:,4) = []
matlabArr = 
    11    12    13    15
    21    22    23    25
    31    32    33    35
    41    42    43    45

When you assign the empty matrix to the Java array dblArray, the array maintains its 4-by-5 dimensions.

dblArray(:,4) = []
dblArray =

  java.lang.Double[][]:

    [125]    [125]    [125]    []    [125]
    [250]       []    [250]    []    [250]
    [375]    [375]    [375]    []    [375]
    [500]    [500]    [500]    []    [500]