Functions

fromHandle
I

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

get
I

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

getLength
I

Returns the length of the given Java array.

newInstance
I

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

set
I

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

toHandle
I

Returns a handle value representation for a Ballerina array.

fromHandle

function fromHandle(handle array, string jType, string bType) returns any[ ] | error
Isolated Function

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

1int[] array = <int[]> check arrays: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 array any[]|error for the provided handle

get

function get(handle array, int index) returns handle
Isolated Function

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.

1handle words = getSortedArray();
2handle firstWord = 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

function getLength(handle array) returns int
Isolated Function

Returns the length of the given Java array.

1handle array = getArray();
2int length = 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

function newInstance(handle classType, int... dimensions) returns handle
Isolated Function

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.

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

Parameters

  • classType handle
  • The element type of the array

  • dimensions int...
  • The dimensions of the array

Return Type

(handle)

The new Java array instance

set

function set(handle array, int index, handle element)
Isolated Function

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.

1handle strArray = getStringArray();
2arrays: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

function toHandle(any[ ] array, string jType) returns handle | error
Isolated Function

Returns a handle value representation for a Ballerina array.

1handle handleValue = check 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