public class MWJavaObjectRef extends MWArray
MWJavaObjectRef, a special subclass of MWArray, can be used to create a
MATLAB array that references a Java object.EMPTY_ARRAY| Constructor and Description |
|---|
MWJavaObjectRef(java.lang.Object o)
Example: Instantiate a new Builder component object:
|
| Modifier and Type | Method and Description |
|---|---|
<T> T |
applyVisitor(AbstractMWArrayVisitor<T> v) |
MWClassID |
classID()
Returns the MATLAB type of this array.
|
java.lang.Object |
clone()
Example: Cloning (deep copying) an array
|
int[] |
columnIndex()
This method returns an array containing the column index of
each element in the underlying MATLAB array.
|
int |
compareTo(java.lang.Object obj)
This method compares the MWArray object with the input object.
|
void |
dispose()
Frees the native MATLAB array contained by this array.
|
boolean |
equals(java.lang.Object obj)
Indicates whether some other array is equal to this one.
|
java.lang.Object |
get()
Example: Get information about a referenced object
|
java.lang.Object |
get(int index)
Returns the element at the specified 1-based offset in this array.
|
java.lang.Object |
get(int[] index)
Returns the element at the specified 1-based index-array in this array.
|
java.lang.Object |
getData()
Returns a 1-D array containing a copy of the data in the underlying MATLAB array.
|
int[] |
getDimensions()
Returns an array containing the size of each dimension of this array.
|
int |
hashCode()
Returns a hash code value for this array.
|
boolean |
isEmpty()
This method returns true if the array object contains no elements,
and false otherwise.
|
boolean |
isSparse()
This method returns true if the MWArray object is sparse,
and false otherwise.
|
int |
maximumNonZeros()
Returns the allocated capacity of a sparse array.
|
int |
numberOfDimensions()
Returns the number of dimensions of this array.
|
int |
numberOfElements()
Returns the total number of elements in this array.
|
int |
numberOfNonZeros()
Returns the number of non-zero elements in a sparse array.
|
int[] |
rowIndex()
This method returns an array containing the row index of each
element in the underlying MATLAB array.
|
void |
set(int[] index,
java.lang.Object element)
Replaces the element at the specified 1-based index-array in this array with the specified element.
|
void |
set(int index,
java.lang.Object element)
Replaces the element at the specified 1-based offset in this array with the specified element.
|
void |
set(java.lang.Object o)
Example: Setting an array to specified values
|
void |
setData(java.lang.Object data) |
java.lang.Object |
sharedCopy()
Example: Creating a shared copy of an array
|
java.lang.Object[] |
toArray()
Returns an array containing a copy of the data in the underlying MATLAB array.
|
java.lang.String |
toString()
Example: Return string representation of array
|
static java.lang.Object |
unwrapJavaObjectRefs(java.lang.Object obj)
Example:
|
static java.lang.Object[] |
unwrapJavaObjectRefs(java.lang.Object[] array)
Example:
|
disposeArraypublic MWJavaObjectRef(java.lang.Object o)
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(5));
MWJavaObjectRef A = new MWJavaObjectRef(myHash);
System.out.println("A = " + A.toString());
MWArray.disposeArray(A);
The result is:
A = {b=5, a=3}
o - The object to reference.public java.lang.Object clone()
throws java.lang.CloneNotSupportedException
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(5));
MWJavaObjectRef A = new MWJavaObjectRef(myHash);
Object C = A.clone();
System.out.println("A = " + A.toString() +
", Clone of A = " +
C.toString());
myHash.put("c", new Integer(7));
System.out.println("After change: A = " + A.toString() +
", Clone of A = " +
C.toString());
A.set(new Integer(12));
System.out.println("After set: A = " + A.toString() +
", Clone of A = " +
C.toString());
MWArray.disposeArray(A);
MWArray.disposeArray(C);
Initial results:
A = {b=5, a=3}, Clone of A = {b=5, a=3}
Results after change:
A = {b=5, a=3, c=7}, Clone of A = {b=5, a=3, c=7}
Results after set:
A = 12, Clone of A = {b=5, a=3, c=7}
public java.lang.Object sharedCopy()
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(5));
MWJavaObjectRef A = new MWJavaObjectRef(myHash);
Object S = A.sharedCopy();
System.out.println("A = " + A.toString() +
", Shared copy of A = " +
S.toString());
myHash.put("c", new Integer(7));
System.out.println("After change: A = " + A.toString() +
", Shared copy of A = " +
S.toString());
MWArray.disposeArray(A);
MWArray.disposeArray(S);
Initial results are as follows:
A = {b=5, a=3}, Shared copy of A = {b=5, a=3}
Results after change:
A = {b=5, a=3, c=7}, Shared copy of A = {b=5, a=3, c=7}
sharedCopy in class MWArrayMWArray instance representing a shared copy of the
underlying MATLAB array.public boolean equals(java.lang.Object obj)
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(5));
Hashtable myHash2 = new Hashtable();
myHash2.put("a", new Integer(3));
myHash2.put("b", new Integer(5));
MWJavaObjectRef A = new MWJavaObjectRef(myHash);
MWJavaObjectRef B = new MWJavaObjectRef(myHash);
MWJavaObjectRef C = new MWJavaObjectRef(myHash2);
System.out.println("Do A and B refer to the same object? " + A.equals(B));
System.out.println("Do A and C? " + A.equals(C));
MWArray.disposeArray(A);
MWArray.disposeArray(B);
MWArray.disposeArray(C);
Results are as follows:
Do A and B refer to the same object? true
Do A and C? false
public int hashCode()
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(8));
MWJavaObjectRef A = new MWJavaObjectRef(myHash);
int h = A.hashCode();
System.out.println("Hash of A is 0x" + Integer.toHexString(h));
MWArray.disposeArray(A);
Results are as follows:
Hash of A is 0xffffff33
public java.lang.String toString()
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(5));
MWJavaObjectRef A = new MWJavaObjectRef(myHash);
System.out.println("String representation of A is " + A.toString());
MWArray.disposeArray(A);
Results are as follows:
String representation of A is {b=5, a=3}
public int numberOfElements()
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(5));
MWJavaObjectRef A = new MWJavaObjectRef(myHash);
System.out.println("Number of elements in A is " + A.numberOfElements());
MWArray.disposeArray(A);
Results are as follows:
Number of elements in A is 1
numberOfElements in class MWArraypublic int numberOfNonZeros()
numberOfElements().numberOfNonZeros in class MWArraypublic int maximumNonZeros()
numberOfElements().maximumNonZeros in class MWArray
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(5));
MWJavaObjectRef A = new MWJavaObjectRef(myHash);
System.out.println("Maximum number of nonzero elements in A is " + A.maximumNonZeros());
MWArray.disposeArray(A);
Results are as follows:
Maximum number of nonzero elements in A is 1
public void dispose()
dispose in interface Disposabledispose in class MWArraypublic java.lang.Object get()
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(5));
MWJavaObjectRef A = new MWJavaObjectRef(myHash);
System.out.println("A.get() = " + A.get());
System.out.println("Class of referenced object is " + A.get().getClass().toString());
System.out.println("Class of A is " + A.getClass().toString());
MWArray.disposeArray(A);
Results are as follows:
A.get() = {b=5, a=3}
Class of referenced object is class java.util.Hashtable
Class of A is class com.mathworks.toolbox.javabuilder.MWJavaObjectRef
public void set(java.lang.Object o)
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(5));
MWJavaObjectRef A = new MWJavaObjectRef(myHash);
System.out.println("A = " + A.toString());
A.set(new Integer(12));
System.out.println("After set(), A = " + A.toString());
MWArray.disposeArray(A);
Initial results are as follows:
A = {b=5, a=3}
Results after set:
A = 12
public MWClassID classID()
classID in class MWArrayMWClassID of this array
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(5));
MWJavaObjectRef A = new MWJavaObjectRef(myHash);
System.out.println("ClassID of A is " + A.classID());
MWArray.disposeArray(A);
The result is:
ClassID of A is opaque
public java.lang.Object[] toArray()
toArray
returns the real part. If the underlying array is sparse, a full representation of
the array is returned. Care should be taken when calling toArray on a
sparse array with large row and column dimensions, as this action may exhaust system
memory. If the underlying array is a cell or struct array, toArray is
recursively called on each cell.toArray in class MWArray
Integer myInt = new Integer(2);
System.out.println("myInts = " + myInt.toString());
MWJavaObjectRef A = new MWJavaObjectRef(myInt);
Object[] B = A.toArray();
System.out.println("Array form is an instance of " + B.getClass().toString());
System.out.println("B = " + B);
System.out.println("Each element is an instance of " + B[0].getClass().toString());
System.out.println("B[0] = " + B[0]);
System.out.println("A is an instance of " + A.getClass().toString());
MWArray.disposeArray(A);
Results are as follows:
myInts = 2
Array form is an instance of class [Ljava.lang.Object;
B = [Ljava.lang.Object;@1d53f5b
Each element is an instance of class java.lang.Integer
B[0] = 2
A is an instance of class com.mathworks.toolbox.javabuilder.MWJavaObjectRef
public <T> T applyVisitor(AbstractMWArrayVisitor<T> v)
applyVisitor in class MWArraypublic static java.lang.Object unwrapJavaObjectRefs(java.lang.Object obj)
Hashtable myHash = new Hashtable();
myHash.put("a", new Integer(3));
myHash.put("b", new Integer(5));
MWJavaObjectRef A = new MWJavaObjectRef(new Integer(12));
System.out.println("A referenced the object: " + MWJavaObjectRef.unwrapJavaObjectRefs(A));
MWJavaObjectRef B = new MWJavaObjectRef(myHash);
Object bObj = (Object)B;
System.out.println("B referenced the object: " + MWJavaObjectRef.unwrapJavaObjectRefs(bObj));
Results are as follows:
A referenced the object: 12
B referenced the object: {b=5, a=3}
public static java.lang.Object[] unwrapJavaObjectRefs(java.lang.Object[] array)
MWJavaObjectRef A = new MWJavaObjectRef(new Integer(12));
MWJavaObjectRef B = new MWJavaObjectRef(new Integer(104));
Object[] refArr = new Object[2];
refArr[0] = A;
refArr[1] = B;
Object[] objArr = MWJavaObjectRef.unwrapJavaObjectRefs(refArr);
System.out.println("refArr referenced the objects: " + objArr[0] + " and " + objArr[1]);
Results are as follows:
refArr referenced the objects: 12 and 104
public int numberOfDimensions()
MWArray
int[][] Adata = {{ 1, 2, 3, 4, 5, 6},
{ 7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18}};
MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT32);
Example: Getting the Number of Dimensions of an MWArray
Display the number of dimensions for array object A:
System.out.println("Matrix A has " + A.numberOfDimensions() +
" dimensions");
When run, the example displays this output:
Matrix A has 2 dimensions
numberOfDimensions in class MWArraypublic int[] getDimensions()
MWArray
int[][] Adata = {{ 1, 2, 3, 4, 5, 6},
{ 7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18}};
MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT32);
Example: Getting Array Dimensions of an MWArray
int[] dimA = A.getDimensions();
System.out.println("Dimensions of A are " +
dimA[0] + " x " + dimA[1]);
When run, the example displays this output:
Dimensions of A are 3 x 6
getDimensions in class MWArraypublic boolean isEmpty()
MWArray
int[][] Adata = {{ 1, 2, 3, 4, 5, 6},
{ 7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18}};
MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT32);
Example: Testing for an Empty MWArray
Display a message if array object A is an empty array.
Otherwise, display the contents of A:
if (A.isEmpty())
System.out.println("Matrix A is empty");
else
System.out.println("A = " + A.toString());
When run, the example displays the contents of A:
A= 1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
public boolean isSparse()
MWArraynewSparse method, used in the following example is:
(2,1) 50 (1,2) 10 (2,2) 60 (1,5) 40 (2,5) 90Example: Testing an MWArray for Sparseness Test the MWArray object A created previously for sparseness:
if (A.isSparse())
System.out.println("Matrix A is sparse");
When run, the example displays this output:
Matrix A is sparse
public int compareTo(java.lang.Object obj)
MWArray
Object S = A.sharedCopy();
if (A.compareTo(S) == 0)
System.out.println("Matrix S is equal to matrix A");
When run, the example displays this output:
Matrix S is equal to matrix A
public java.lang.Object get(int index)
MWArraypublic Object get(int[] index).
int[] cdims = {1, 3};
MWArray C = new MWArray(cdims);
Integer val = new Integer(15);
int[] index2 = {1, 3};
C.set(index2, val);
Object x = C.get(index2);
if (x instanceof int[][])
{
int[][] y = (int[][])x;
System.out.println("B: Cell data C(1,3) is " + y[0][0]);
}
When run, the example displays this output:
B: Cell data C(1,3) is 15
public java.lang.Object get(int[] index)
MWArrayget in class MWArrayindex - Array of indices specifying the location of the requested element.
The length of the index array must be exactly the number of dimensions of this array.
Each element of the index array has the valid range: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.public void set(int index,
java.lang.Object element)
MWArraypublic void set(int[] index, Object element).
int[] index = {2, 4};
A.set(index, 555);
Object d_out = A.get(index);
System.out.println("Data read from A(2,4) is " +
d_out.toString());
When run, the example displays this output:
Data read from A(2,4) is 555
public void set(int[] index,
java.lang.Object element)
MWArrayset in class MWArrayindex - Array of indices specifying the location of the element to replace.
The length of the index array must be exactly the number of dimensions of this array.
Each element of the index array has the valid range: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.element - New element to replace at index.public java.lang.Object getData()
MWArraygetData returns
the real part. If the underlying array is a cell or struct array, toArray is
recursively called on each cell.
System.out.println("Data read from matrix A is:");
int[] x = (int[]) A.getData();
for (int i = 0; i < x.length; i++)
System.out.print(" " + x[i]);
System.out.println();
When run, the example displays this output:
Data read from matrix A is: 1 7 13 2 8 14 3 9 15 4 10 16 5 11 17 6 12 18
getData in class MWArrayMWArray.numberOfElements() for a non-sparse array, and MWArray.numberOfNonZeros()
for a sparse array.public int[] rowIndex()
MWArraynewSparse method, used in the following example is:
(2,1) 50 (1,2) 10 (2,2) 60 (1,5) 40 (2,5) 90Example: Getting the Row Indices of a Sparse MWArray Get the row indices of the elements of the sparse array:
System.out.print("Row indices are: ");
int[] rowidx = A.rowIndex();
for (int i = 0; i < 5; i++)
System.out.print(rowidx[i] + " ");
System.out.println();
When run, the example displays this output:
Row indices are: 2 1 2 1 2
public int[] columnIndex()
MWArraynewSparse method, used in the following example is:
(2,1) 50 (1,2) 10 (2,2) 60 (1,5) 40 (2,5) 90Example: Getting the Column Indices of a Sparse MWArray Get the column indices of the elements of the sparse array:
System.out.print("Column indices are: ");
int[] colidx = A.columnIndex();
for (int i = 0; i < 5; i++)
System.out.print(colidx[i] + " ");
System.out.println();
When run, the example displays this output:
Column indices are: 1 2 2 5 5
columnIndex in class MWArray© 1994-2008 The MathWorks, Inc. Terms of Use Patents Trademarks