Дата публикации: 04.11.2013 13:31:47
If you come across square brackets in JavaFX script, do not think that JavaFX supports arrays. These are sequences. Sequences are not arrays although they are similar. Before reading this blog post I recommend that you familiarize yourself with the lesson about Sequences in Learning the JavaFX Script Programming Language.
If a variable is already declared as a sequence variable, square brackets are not necessary when declaring this variable as a sequence with a single element. For example, many of my samples usually contain the following construction.
Stage { ... scene: Scene { ... content: MyCustomNode { ... } } }
It is rather convenient that comma-separators are also not required. This way the content creation looks more accurate:
content: [ Rectangle { ... } Circle { ... } ... ]
But do not repeat my mistakes and always use separators with built-in types including strings. As a matter of fact, the [1 2 3] declaration creates a sequence as expected. But if you use negative numbers, the result will be unexpected, since the script evaluates all expressions before it creates a sequence. With strings, you can get even more interesting behavior. Several string literals will be concatenated by a compiler. This is a very useful feature that allows splitting long string literals among several lines, but if you create a sequence you might get a confusing result.
println([ 1 2 3 ]); // prints [ 1, 2, 3 ] println([ -1 2 -3 ]); // prints [ -1, -1 ] println([ "1" "2" "3" ]); // prints [ 123 ]
You can determine the size of a sequence by using the sizeof operator followed by the name of the sequence. Sometimes you need to determine the index of an element in a sequence, but JavaFX only supports the foreach-modification of the for-cycle. In this case, instead of creating an auxiliary sequence with indices, you can use the indexof operator followed by the name of the for-variable.
var values = [1 2 3]; println(sizeof values); // prints 3 for (value in values) println(indexof value); // prints 0, then 1, etc...
Note that the for-cycle is not a statement but an expression which returns a sequence. Each element in this sequence is a result of the last expression in a cycle body. This fact also simplifies sequence creation.
content: for (index in [0..9]) { var i = 10 + 40 * index; var j = 5 + 20 * index; Rectangle { x: i y: j ... } }
Another difference between sequences and arrays is that you cannot add a null value to a sequence. On the one hand, it slightly reduces memory overhead. On the other hand, it impedes the creation of sparse arrays. A direct insert of a null value is prohibited by the compiler. When inserted indirectly, the null value is ignored at runtime.
println(["first",null,"last"]); // compilation error var zero: String = null; println(["first",zero,"last"]); // prints [ first, last ]
In addition, JavaFX does not support multidimensional sequences. Even if you declare a sequence of sequences, all elements of inner sequences will be added to the top-level sequence.
println([[1,2],[3,4]]); // prints [ 1, 2, 3, 4 ] var one = [1 2]; var two = [3 4]; println([one two]); // prints [ 1, 2, 3, 4 ]
In whole, sequences have both advantages and drawbacks. Programmers should get accustomed to the sequence notation and its limitations.
PS. This article was originally posted on the Java.net site.