com.mathworks.toolbox.javabuilder.MWJavaObjectRef Class
Namespace: com.mathworks.toolbox.javabuilder
Java class to create a MATLAB array that references aJava object
Description
Declaration
public class MWJavaObjectRef extends MWArray
MWJavaObjectRef, a special subclass of MWArray, can be used to create a MATLAB array that references a Java object
Implemented Interfaces:
MWComponentOption
, java.io.Serializable
,
java.lang.Cloneable
, java.lang.Comparable
Creation
Constructors
MWJavaObjectRef(java.lang.Object o)
Instantiates a new component object.
Properties
Public Properties
EMPTY_ARRAY
— Represent an empty array
public static final MWArray
A convenient, efficient, and consistent way to represent an empty array as follows:
public static final MWArray EMPTY_ARRAY
Methods
Public Methods
applyVisitor |
This
method is abstract and returns a type specified by the type parameter <T>.
It takes an instance of Parameters:
Returns:
|
classID |
Returns the MATLAB type of this array. Specified
by:
Returns: MWClassID of this array. Example: Returning MATLAB type of a given array: Hashtable<String,Integer> myHash = new Hashtable<String,Integer>(); 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); ClassID of A is opaque |
clone |
Example: Cloning (deep copying) an array Hashtable<tring,Integer> myHash = new Hashtable<String,Integer>(); 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); A = {b=5, a=3}, Clone of A = {b=5, a=3} A = {b=5, a=3, c=7}, Clone of A = {b=5, a=3, c=7} A = 12, Clone of A = {b=5, a=3, c=7} Overrides:
Returns: An MWArray instance representing a deep copy of the underlying MATLAB array. Throws:
|
columnIndex | This method returns an array containing the column index of each element in the underlying MATLAB array. In the
following example, the contents of the sparse MWArray (created by the
(2,1) 50 (1,2) 10 (2,2) 60 (1,5) 40 (2,5) 90 Example: Getting the Column Indices of a Sparse MWArray Get the column indices of the elements of the sparse array: System.out.print("Columnindicesare:"); int[]colidx=A.columnIndex(); for(inti=0;i<5;i++) System.out.print(colidx[i]+""); System.out.println(); Column indices are: 1 2 2 5 5 Specified by:
Returns: Array of indices. |
compareTo |
This method compares the MWArray object with the input object. It returns a negative integer, zero, or a positive integer if the MWArray object is less than, equal to, or greater than the specified object, respectively. Example: Comparing MWArrays with compareTo Create a shared copy of the MWArray object and then compare it to the original object. A return value of zero indicates that the two objects are equal: Object S = A.sharedCopy(); if (A.compareTo(S) == 0) System.out.println("Matrix S is equal to matrix A"); Matrix S is equal to matrix A Specified by:
Specified by:
Parameters:
|
dispose |
Frees the native MATLAB array contained by this array. Specified
by:
Specified
by:
|
equals |
Indicates whether some other array is equal to this one. Example: Indicating whether arrays are equal:
Hashtable<String,Integer> myHash = new Hashtable<String,Integer>(); myHash.put("a", new Integer(3)); myHash.put("b", new Integer(5)); Hashtable<String,Integer> myHash2 = new Hashtable<String,Integer>(); 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); Do A and B refer to the same object? true Do A and C? false Specified by:
Parameters:
|
get |
Example: Get information about a referenced object Hashtable<String,Integer> myHash = new Hashtable<String,Integer>(); 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); 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 Returns: Referenced object. |
getData |
Returns a 1-D array containing a copy of the data in the underlying MATLAB array. The elements of the returned array are converted according to default conversion rules. If the underlying MATLAB array is a complex numeric type, getData returns the real part. If the underlying array is a cell or struct array, toArray is recursively called on each cell. Example: Getting an MWArray Value with getData Get the data from MWArray object A, casting the type from Object to int: System.out.println("Data read from matrix A:"); int[] x = (int[]) A.getData(); for (int i = 0; i < x.length; i++) System.out.print(" " + x[i]); System.out.println(); Data read from matrix A: 1 7 13 2 8 14 3 9 15 4 10 16 5 11 17 6 12 18 Specified by:
Returns: A 1-D array of elements stored in column-wise order. The length of the returned array equals MWArray.numberOfElements() for a non-sparse array, and MWArray.numberOfNonZeros() for a sparse array. |
getDimensions |
Returns an array containing the size of each dimension of this array. This example uses a 3-by-6 MWNumericArray object A, as constructed by this Java code: 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]); Dimensions of A are 3 x 6 Specified by:
Returns: Array containing size of each dimension. |
hashCode |
Returns a hash code value for this array. Example: Return hash of array Hashtable<String,Integer> myHash = new Hashtable<String,Integer>(); 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); Hash of A is 0xffffff33 Specified by:
|
isEmpty |
This method returns true if the array object contains no elements, and false otherwise. This example uses a 3-by-6 MWNumericArray object A, as constructed by this Java code: 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()); A= 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Specified by:
Returns:
|
isSparse |
This method returns true if the MWArray object is sparse, and false otherwise. The contents of the sparse MWArray, created by the newSparse method, used in the following example are: (2,1) 50 (1,2) 10 (2,2) 60 (1,5) 40 (2,5) 90 Example: Testing an MWArray for Sparseness Test the MWArray object A created previously for sparseness: if (A.isSparse()) System.out.println("Matrix A is sparse"); Matrix A is sparse Specified by:
Returns:
|
maximumNonZeros |
Returns the
allocated capacity of a sparse array. If the underlying array is non-sparse, this
method returns the same value as
Specified by:
Returns: Currently allocated number of non-zero elements in a sparse array. Example: Display maximum number of non-zero elements in array Hashtable<String,Integer> myHash = new Hashtable<String,Integer>(); 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); Maximum number of nonzero elements in A is 1 |
numberOfDimensions |
Returns the number of dimensions of this array. This example uses a 3-by-6 MWNumericArray object A, as constructed by this Java code: 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"); Matrix A has 2 dimensions Specified by:
Returns: Number of dimensions. |
numberOfElements |
Returns the total number of elements in this array. Example: Return number of elements in array Hashtable<String,Integer> myHash = new Hashtable<String,Integer>(); 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); Number of elements in A is 1 Specified by:
Returns: Number of elements. |
numberOfNonZeros |
Returns
the number of non-zero elements in a sparse array. If the underlying array is
non-sparse, this method returns the same value as
Specified by:
Returns: Current number of non-zero elements in a sparse array. |
rowIndex |
This method returns an array containing the row index of each element in the underlying MATLAB array. In the following example, the contents of the sparse MWArray (created by the newSparse method) are: (2,1) 50 (1,2) 10 (2,2) 60 (1,5) 40 (2,5) 90 Specified by:
Returns: Array of indices. |
set |
Replaces the element at the specified 1-based index-array in this array with the specified element. Specified by:
Parameters:
|
set |
Replaces the element at the specified 1-based offset in this array with the specified element. Offers better performance than public void set(int[] index, Object element). Example: Setting an MWArray Value Modify the data in element (2, 4) of MWArray object A: 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()); Data read from A(2,4) is 555 Specified by:
Parameters:
|
set |
Example: Setting an array to specified values Hashtable<String,Integer> myHash = new Hashtable<String,Integer>(); 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); A = {b=5, a=3} A = 12 Parameters:
|
setData |
Specified by:
|
sharedCopy |
Example: Creating a shared copy of an array Hashtable<String,Integer> myHash = new Hashtable<String,Integer>(); 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); A = {b=5, a=3}, Shared copy of A = {b=5, a=3} A = {b=5, a=3, c=7}, Shared copy of A = {b=5, a=3, c=7} Specified by:
Returns: An MWArray instance representing a shared copy of the underlying MATLAB array. |
toArray |
Returns an array containing a copy of the data in the underlying MATLAB array. The returned array has the same dimensionality as the underlying MATLAB array. The elements of the returned array are converted according to default conversion rules. If the underlying MATLAB array is a complex numeric type, 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. Specified by:
Returns: An array with the same dimensionality of the MATLAB array. Example: Return array containing a copy of the data in underlying array plus additional information about array 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); 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 |
toString |
Example: Return string representation of array Hashtable<String,Integer> myHash = new Hashtable<String,Integer>(); 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); String representation of A is {b=5, a=3} Specified by:
|
unwrapJavaObjectRefs |
Example: 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]); refArr referenced the objects: 12 and 104 Returns: Array of objects to which the references refer. |
unwrapJavaObjectRefs |
Example: 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]); refArr referenced the objects: 12 and 104 Parameters:
Returns: Array of objects to which the references refer. |
Inherited Methods
Methods inherited from class
com.mathworks.toolbox.javabuilder.MWArray
.
disposeArray |
This
method destroys any native MATLAB arrays contained in the input object and frees
the memory occupied by them. This is a static method of the class and thus does
not need to be invoked in reference to an instance of the class. If the input
object implements the Example: Constructing an MWNumericArray Object MWArray[] MArr = new MWArray[10]; for (int i = 0; i < 10; i++) MArr[i] = new MWNumericArray(); MWArray.disposeArray(MArr); Parameters:
|
Methods inherited from class java.lang.Object
.
finalize |
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
getClass |
Returns the runtime class of this Object. |
notify |
|
notifyAll |
Wakes up all threads that are waiting on this object's monitor. |
wait |
Causes
the current thread to wait until another thread invokes the
|
Version History
Introduced in R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)