Quick note about JavaFX Sequences


If you’re learning about JavaFX, you probably already know about some of the basic data types: String, Number, and others. But what do you know about sequences?

A JavaFX sequence looks and behaves much like a Java array. A sequence is an ordered list of objects. Unlike JavaScript, JavaFX Script requires that each item in the list share the same type. The following code shows a sequence of Strings:

var names = ["John", "Gary", "Ruby", "Nick"];

JavaFX infers the correct type, but you can be explicit:

var names: String[] = ["John", "Gary", "Ruby", "Nick"];

Above, the names variable has the “Sequence of Strings” type.

Like arrays, sequences can be accessed by content index. A sequence’s position (index) is a number from 0 through n-1, where n is the number of items in the sequence. You can use the sizeof operator to tell you how many items are in a sequence.

var size = sizeof names;
println("size of names: {size}");

The easiest way to iterate through a sequence is with the for-in loop like this:

var friends: String[] = ["Jack", "Nick", "Matthew"];
for (friend in friends) {
    println("My friend: {friend}");

Note that the friend variable inside the for loop does not have a var declaration.

Leave a Reply

Your email address will not be published. Required fields are marked *