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.
typescriptnative.ArrayArtifact: com.margelo:typescript-native-stdlibJVM onlyType mapping
| TypeScript | JVM | Notes |
|---|---|---|
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
nullis undefined, in both directions.
Null.INSTANCEis TypeScript's null, in both directions.
- A hole, an index below the length without an element, reads as
null, as undefined, fromgetand the iterator.hasis 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.
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.
static Array<Integer> ofInts(int... values)A new int[] array with a copy of the values, stored unboxed.
static Array<Double> ofNumbers(double... values)A new number[] array with a copy of the values, stored unboxed.
static <E> int pushAll(Array<E> array, E... values)Appends the values in order, as array.push(...values) does.
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.
Methods
int getLength()boolean has(int index)E set(int index, E value)xs[index] = value: replaces the element at index, or appends value at getLength.
Array<E> splice(int start, int deleteCount)ECMAScript splice(start, deleteCount). splice also inserts.
Array<E> slice(int start, int end)ECMAScript slice(start, end) with both bounds given. slice() is slice(0, getLength()). Holes stay holes.
<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()]).
int[] toIntArray()double[] toNumberArray()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.