JavaFX introduces a new data type called a sequence. A sequence is an unordered list of objects, similar to an array.
Declaring Sequences
A sequence is declared as comma delimited list, or range surrounded by square brackets [ ].
var num1 = [1, 2, 3, 4, 5]; var num2 = ["one", "two", "three", "four", "five"];
var num1 = [1..5] //Inclusive range [1, 2, 3, 4, 5] var num2 = [1..<5] //Exclusive range [1, 2, 3, 4]
The object type of a sequence is automatically inferred, however it can also be explicitly declared:
var num1: Integer[] = [1, 2, 3, 4, 5]; var num2: String[] = ["one", "two", "three", "four", "five"];
Sequences cannot contain other sequences. Nested sequences are automatically flattened such that:
[[1, 2, 3], 4, [], [5]] becomes [1, 2, 3, 4, 5]
Adding or Removing Items
To add an item at the end of the sequence use the syntax insert item into sequence
var num = [1, 2, 3, 4]; insert 5 into num; // num now equals [1, 2, 3, 4, 5]
To insert an item before or after a certain index use the syntax insert item before/after sequence[index], where the first item in the sequence is at index 0.
var num = [2, 3, 5]; insert 4 after num[1]; insert 1 before num[0]; // num now equals [1, 2, 3, 4, 5]
To delete an item from a certain index use delete sequence[index], where the first item in the sequence is at index 0.
To delete ALL items in a sequence use delete sequence
var num = [1, 2, 100, 3, 4, 5]; delete num[2]; // num now equals [1, 2, 3, 4, 5] delete num; // num now equals []
Accessing Items
There are several ways to access data in a sequence.
var num = [1, 2, 3, 4, 5, 6, 7]; println( num[0] ); // Index: 0 println( num[2] ); // Index: 2 println( num[1..3] ); // Range println( num[1..<3] ); // Range (exclusive) println( num[1..] ); // Range (until end) println( num[1..<] ); // Range (until end exclusive) println( num[n | n>3 ] ); // All items > 3 println( reverse num ); // Reverse order println( sizeof num ); // Length of sequence /* Output: 1 3 [2, 3, 4] [2, 3] [2, 3, 4, 5, 6, 7] [2, 3, 4, 5, 6] [4, 5, 6, 7] [7, 6, 5, 4, 3, 2, 1] 7 */
Comparing Sequences
Sequences are compared by looking at the value at each index.
var seq1; var seq2; seq1 = [1, 2, 3]; seq2 = [1, 2, 3]; // seq1 == seq2 -> true seq1 = [1, 2, 3]; seq2 = [3, 2, 1]; // seq1 == seq2 -> false seq1 = [1, 2, 3]; seq2 = [1, 2]; // seq1 == seq2 -> false
Additional Notes
When initializing sequences with only a single item, the square brackets can be omitted as long as the variable has already been declared to be a sequence.
var seq: Integer[]; seq = 5;
Scene { content: Text{ } }
In some cases the comma that seperates items may be omitted.
Scene { content: [ Text { // ... } Circle { // ... } ] }
Nevertheless, to avoid unexpected behavior commas should always be used when seperating primitive types such as Integers or Strings. For example, var seq = [ -2 -1 0 1 2]; will first evaluate the expression -2 - 1, creating the sequence [-3 0 1 2].
A null value is interpreted as an empty sequence, [].
Sequences are immutable. This means that under the hood, when a value within in a sequence is changed or an item is added to or deleted from a sequence, an entirely new sequence is created to replace the old sequence.
