jballerina.java.arrays
Module jballerina.java.arrays
ballerina/jballerina.java.arrays
Overview
This module provides APIs to create new Java array instances, get elements from arrays, set elements, etc.
For information on the operations, which you can perform with the jballerina.java.arrays module, see the below Functions.
Functions
fromHandle
Returns a Ballerina array for a handle that holds a Java array.
int[] 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
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 = 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
Returns the length of the given Java array.
handle array = getArray(); int 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
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 = 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)
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(); 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
Returns a handle value representation for a Ballerina array.
handle 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