Functions - java.arrays

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.

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