Skip to Content
API reference

Array<E>

A TypeScript array as Java and Kotlin see it: the array object itself, so every alias sees push, pop and splice. Java null is undefined and Null.INSTANCE is null.

CollectionsType: typescriptnative.ArrayArtifact: com.margelo:typescript-native-stdlibJVM only

Type mapping

TypeScriptJVMNotes
T[]typescriptnative.Array<T>The same array on both sides; int[] and number[] read and write without boxing through getInt and getNumber.
FixedArray<T>T[] (a Java array)A direct native JVM array.

Array interface

public interface Array<E> extends Iterable<E>

A TypeScript array as Java and Kotlin see it, such as string[] as Array<String>, int[] as Array<Integer> and int[][] as Array<Array<Integer>>.

The runtime's Array object implements this interface, so a typescriptnative.Array is the TypeScript array itself: == is its identity, and mutation is shared both ways. It does not implement java.util.List and does not override equals or hashCode. Build one with of, ofInts or ofNumbers. The runtime accepts only its own arrays: a class of the app that implements this interface throws IllegalArgumentException when it reaches TypeScript code.

Absent values follow one fixed rule, whatever the element type and whoever made the array:

  • Java's null is undefined, in both directions.
  • Null.INSTANCE is TypeScript's null, in both directions.
  • A hole, an index below the length without an element, reads as null, as undefined, from get and the iterator. has is the only way to tell it from a stored undefined. No Java call writes a hole.

So (string | null | undefined)[] is Array<Object>, and Array.of("a", null, Null.INSTANCE) holds a string, undefined and null. A TypeScript parameter checks the elements when it receives an array: undefined in a string[] parameter, or null in a (string | undefined)[] one, throws IllegalArgumentException naming the index.

An int[] or number[] that TypeScript creates stores its elements unboxed. getInt, setInt, pushInt and toIntArray, and their Number forms, read and write them without boxing. A write from Java cannot change how the array stores its elements: on such an array null, Null.INSTANCE and anything but a number of the store's kind throw.

The name is TypeScript's. A Java file that also uses java.lang.reflect.Array names one of them in full, and Kotlin code that also uses kotlin.Array imports this one under another name, such as import typescriptnative.Array as JSArray.

Not thread-safe, as for every TypeScript value: the app coordinates concurrent access.

E
the element type

Static methods

static <E> Array<E> of(E... values)

A new array with a copy of the values. null is undefined and Null.INSTANCE is null.

E
the element type
values
the elements
Returns
the new array, which shares no storage with values
Throws IllegalArgumentException
when a value is a typescriptnative.Array that the runtime did not make
static Array<Integer> ofInts(int... values)

A new int[] array with a copy of the values, stored unboxed.

values
the elements
Returns
the new array
static Array<Double> ofNumbers(double... values)

A new number[] array with a copy of the values, stored unboxed.

values
the elements
Returns
the new array
static <E> int pushAll(Array<E> array, E... values)

Appends the values in order, as array.push(...values) does.

E
the element type
array
the array
values
the new last elements
Returns
the new length
static <E> Array<E> splice(Array<E> array, int start, int deleteCount, E... items)

ECMAScript array.splice(start, deleteCount, ...items). start and deleteCount are clamped as the specification clamps them: a negative start counts from the end, and a negative count removes nothing.

E
the element type
array
the array
start
the first index to remove
deleteCount
the number of elements to remove
items
the elements to insert at start
Returns
the removed elements, as a new array

Methods

int getLength()
Returns
the number of elements
Throws RuntimeException
the runtime's RangeError when the length is above 2^31 - 1
int setLength(int length)

xs.length = length: shortens the array.

length
the new length, from 0 to getLength
Returns
length
Throws IllegalArgumentException
when length is negative or larger than the current length, which would create holes
boolean has(int index)
index
any index
Returns
whether the array has an element at index; a hole is not an element, a stored undefined is
E get(int index)

xs[index].

index
the index of an element
Returns
the element: null for undefined and for a hole, Null.INSTANCE for null, the array itself for a nested array, and the stored box for a number
Throws IndexOutOfBoundsException
when index is negative or not below getLength
E set(int index, E value)

xs[index] = value: replaces the element at index, or appends value at getLength.

index
the index of an element, or getLength
value
the new element: null is undefined, Null.INSTANCE is null
Returns
the element that was at index, as get returns it, or null for an append
Throws IndexOutOfBoundsException
when index is larger than getLength or negative; the array is unchanged
Array<E> fill(E value)

xs.fill(value): every element becomes value.

value
the new value of every element: null is undefined, Null.INSTANCE is null
Returns
this array
int atIndex(int index)

The index that xs.at(index) reads.

index
an index; a negative index counts from the end
Returns
the index, or -1 when it is outside the array
int push(E value)

Appends an element.

value
the new last element: null is undefined, Null.INSTANCE is null
Returns
the new length
E removeLast()

Removes the last element.

Returns
the removed element, as get returns it
Throws IndexOutOfBoundsException
when the array is empty
Array<E> splice(int start)

ECMAScript splice(start): removes every element from start.

start
the first index to remove; a negative index counts from the end
Returns
the removed elements, as a new array
Array<E> splice(int start, int deleteCount)

ECMAScript splice(start, deleteCount). splice also inserts.

start
the first index to remove; a negative index counts from the end
deleteCount
the number of elements to remove; a negative count removes nothing
Returns
the removed elements, as a new array
Array<E> slice(int start, int end)

ECMAScript slice(start, end) with both bounds given. slice() is slice(0, getLength()). Holes stay holes.

start
the first index to copy; a negative index counts from the end
end
the index after the last one to copy; a negative index counts from the end
Returns
the copied elements, as a new array
<A> A[] copyInto(A[] target)

Copies the elements into target, as get returns them. The caller creates the typed Java array, for example xs.copyInto(new String[xs.getLength()]).

A
the component type of target
target
an array whose length is getLength
Returns
target
Throws IllegalArgumentException
when the length of target differs from getLength
Throws ArrayStoreException
when an element is not an A
int getInt(int index)

xs[index]! of an int[] without a box.

index
the index of an element
Returns
the element
Throws IndexOutOfBoundsException
when index is outside the array
Throws IllegalArgumentException
when the element is not an integral number in the int range
int setInt(int index, int value)

xs[index] = value of an int[] without a box.

index
the index of an element, or getLength
value
the new element
Returns
value
int pushInt(int value)

Appends an int without a box.

value
the new last element
Returns
the new length
int[] toIntArray()
Returns
a new Java array with the elements of an int[], which shares nothing with this array
double getNumber(int index)

xs[index]! of a number[] without a box.

index
the index of an element
Returns
the element
Throws IndexOutOfBoundsException
when index is outside the array
Throws IllegalArgumentException
when the element is not a number
double setNumber(int index, double value)

xs[index] = value of a number[] without a box.

index
the index of an element, or getLength
value
the new element
Returns
value
Throws IllegalArgumentException
on an int[] array that stores its elements unboxed, when value is not an int other than -0
int pushNumber(double value)

Appends a number without a box.

value
the new last element
Returns
the new length
Throws IllegalArgumentException
on an int[] array that stores its elements unboxed, when value is not an int other than -0
double[] toNumberArray()
Returns
a new Java array with the elements of a number[], which shares nothing with this array
java.util.Iterator<E> iterator()

An iterator for Java and Kotlin callers, for (String s : xs). Each step is get at the next index below the current length, so it sees elements pushed during the loop, and a hole reads as null. Its remove throws.

Generated when the website builds, from Array.java.

Language

TypeScriptNo TypeScript typeJVMJava and KotlinSwiftNot generated yet

Remembered on every API page

On this page