A script-enabled browser is required for this page to function properly.

Working with Arrays

The ORA_JAVA package includes built-ins that provide routines to create an array and to get and set the value of an array element. These arrays are fully interoperable with Java arrays.

The ORA_JAVA.JARRAY type is used to store references to created arrays. For example:


  
PROCEDURE get_object_array IS 
   arr ORA_JAVA.JARRAY; 
   obj1 ORA_JAVA.JOBJECT;
   ...
   

The ORA_JAVA.JARRAY type is a subtype of a ORA_JAVA.JOBJECT, so arrays can be persisted in the same manner as all other Java objects using the global reference functions.

Arrays can be created of any Java scalar type or of the java.lang.Object type. The ORA_JAVA package contains built-ins that allow you to create arrays of a specific type and of a designated length. Arrays can be returned from function calls in the generated PL/SQL packages.

Another convenient array built-in is the ARRAY_LENGTH function that returns the maximum length of a specified array. This built-in is commonly used when an array object is returned from a Java method call and the length of the array is unknown.

Example

In the following example, the ORA_JAVA built-ins are used to create an array with three elements, store integers in the first two element positions, and store the sum of the two elements in the third position. The length of the array is then sent to the message line.



PROCEDURE set_int_array IS
   arr ORA_JAVA.JARRAY;
   arg1 PLS_INTEGER;
   arg2 PLS_INTEGER;
   arg3 PLS_INTEGER;
BEGIN
   arr := ORA_JAVA.NEW_INT_ARRAY(3);
   ORA_JAVA.SET_INT_ARRAY_ELEMENT(arr, 0, 2);
   ORA_JAVA.SET_INT_ARRAY_ELEMENT(arr, 1, 4);
   arg1 := ORA_JAVA.GET_INT_ARRAY_ELEMENT(arr, 0);
   arg2 := ORA_JAVA.GET_INT_ARRAY_ELEMENT(arr, 1);
   arg3 := arg1 + arg2;
   ORA_JAVA.SET_INT_ARRAY_ELEMENT(arr, 2, arg3);   
   message('  Array length is:  ' || ORA_JAVA.GET_ARRAY_LENGTH(arr));
END;

NEW_java_type_ARRAY Built-in

GET_java_type_ARRAY_ELEMENT Built-in

SET_java_type_ARRAY_ELEMENT Built-in

GET_ARRAY_LENGTH Built-in