Manipulating Java arrays

To represent an array in a COBOL program, code a group item that contains a single elementary item that is of the data type that corresponds to the Java™ type of the array. Specify an OCCURS or OCCURS DEPENDING ON clause that is appropriate for the array.

About this task

For example, the following code specifies a structure to receive 500 or fewer integer values from a jlongArray object:


01  longArray.
    02 X pic S9(10) comp-5 occurs 1 to 500 times depending on N.

To operate on objects of the special Java-array classes, call the services that the JNI provides. You can use services to access and set individual elements of an array and for the following purposes, using the services cited:

Table 1. JNI array services
Service Input arguments Return value Purpose
GetArrayLength
  • The JNI environment pointer
  • The array object reference
The array length as a binary fullword integer To get the number of elements in a Java array object
NewBooleanArray, NewByteArray, NewCharArray, NewShortArray, NewIntArray, NewLongArray
  • The JNI environment pointer
  • The number of elements in the array, as a binary fullword integer
The array object reference, or NULL if the array cannot be constructed To create a new Java array object
GetBooleanArrayElements, GetByteArrayElements, GetCharArrayElements, GetShortArrayElements, GetIntArrayElements, GetLongArrayElements
  • The JNI environment pointer
  • The array object reference
  • A pointer to a boolean item. If the pointer is not null, the boolean item is set to true if a copy of the array elements was made. If a copy was made, the corresponding ReleasexxxArrayElements service must be called if changes are to be written back to the array object.
A pointer to the storage buffer To extract the array elements from a Java array into a storage buffer. The services return a pointer to the storage buffer, which you can use as the address of a COBOL group data item defined in the LINKAGE SECTION.
ReleaseBooleanArrayElements, ReleaseByteArrayElements, ReleaseCharArrayElements, ReleaseShortArrayElements, ReleaseIntArrayElements, ReleaseLongArrayElements
  • The JNI environment pointer
  • The array object reference
  • A pointer to the storage buffer
  • The release mode, as a binary fullword integer. See Java JNI documentation for details. (Recommendation: Specify 0 to copy back the array content and free the storage buffer.)
None; the storage for the array is released. To release the storage buffer that contains elements that have been extracted from a Java array, and conditionally map the updated array values back into the array object
NewObjectArray
  • The JNI environment pointer
  • The number of elements in the array, as a binary fullword integer
  • An object reference for the array element class
  • An object reference for the initial element value. All array elements are set to this value.
The array object reference, or NULL if the array cannot be constructed1 To create a new Java object array
GetObjectArrayElement
  • The JNI environment pointer
  • The array object reference
  • An array element index, as a binary fullword integer using origin zero
An object reference2 To return the element at a given index within an object array
SetObjectArrayElement
  • The JNI environment pointer
  • The array object reference
  • The array element index, as a binary fullword integer using origin zero
  • The object reference for the new value
None3 To set an element within an object array
  1. NewObjectArray throws an exception if the system runs out of memory.
  2. GetObjectArrayElement throws an exception if the index is not valid.
  3. SetObjectArrayElement throws an exception if the index is not valid or if the new value is not a subclass of the element class of the array.