Functions - java.arrays

fromHandle

Returns a Ballerina array for a handle that holds a Java array.

get

Returns a handle, which refers to the element at the specified index in the given Java array.

getLength

Returns the length of the given Java array.

newInstance

Returns a new Java array instance with the specified element type and dimensions.

set

Replaces the indexed element at the specified index in the given Java array with the specified element.

toHandle

Returns a handle value representation for a Ballerina array.

fromHandle

(handle array, string jType, string bType)

returns any[] | error

Returns a Ballerina array for a handle that holds a Java array.

 int[] array = <int[]> check jarrays:fromHandle(arrayHandle, "int");

Parameters

  • array handle
  • The handle, which refers to the Java array

  • jType string
  • The string parameter provided to specify the Java array element type

  • bType string (default default)
  • The optional string parameter provided to specify the Ballerina array element type

  • Return Type

    (any[] | error)
  • Ballerina any[]|error array for the provided handle

get

(handle array, int index)

returns handle

Returns a handle, which refers to the element at the specified index in the given Java array. This function completes abruptly with a panic if the specified handle refers to a Java null or if the handle does not refer to a Java array.

 handle words = getSortedArray();
 handle firstWord = java.arrays:get(words, 0);

Parameters

  • array handle
  • The handle, which refers to the Java array

  • index int
  • The index of the element to be returned

  • Return Type

    (handle)
  • The handle, which refers to the element at the specified position in the Java array

getLength

(handle array)

returns int

Returns the length of the given Java array.

 handle array = getArray();
 int length = java.arrays:getLength(array);

Parameters

  • array handle
  • The handle, which refers to the Java array

  • Return Type

    (int)
  • The length of the given Java array

newInstance

(handle class, int[] dimensions)

returns handle

Returns a new Java array instance with the specified element type and dimensions. This function completes abruptly with a panic if the specified handle refers to a Java null or if zero dimensions have been provided.

 handle stringClass = check java:getClass("java.lang.String");
 handle StrArray = java.arrays:newInstance(stringClass, 4);

Parameters

  • class handle
  • The element type of the array

  • dimensions int[]
  • The dimensions of the array

  • Return Type

    (handle)
  • The new Java array instance

set

Replaces the indexed element at the specified index in the given Java array with the specified element. This function completes abruptly with a panic if the specified handle refers to a Java null or if the handle does not refer to a Java array.

 handle strArray = getStringArray();
 java.arrays:set(strArray, 0, java:fromString("Ballerina"));

Parameters

  • array handle
  • The handle, which refers to the Java array

  • index int
  • The index of the element to be replaced

  • element handle
  • The element to be stored at the specified index

toHandle

(any[] array, string jType)

returns handle | error

Returns a handle value representation for a Ballerina array.

 handle handleValue = check java.arrays:toHandle(array, "char");

Parameters

  • array any[]
  • Ballerina array, which is to be converted to a handle reference

  • jType string
  • Java class name or the primitive type of the array elements referenced by the handle

  • Return Type

    (handle | error)
  • The handle|error after the conversion