Learning the JavaFX Script Programming Language

Lesson 5: Sequences

 
Version: JavaFX 1.3 « Previous 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Next »
 
In addition to the basic data types, the JavaFX Script programming language also provides special data structures called sequences. Sequences represent ordered lists of objects (although sequences themselves are not objects). The objects within a sequence are called items. Sequences are declared with square brackets "[]", and each item is separated by a comma. This lesson covers all the basics of creating and using sequences. It also discusses how to work with sequence slices (portions of sequences). For additional information, see Sequence Types in the JavaFX Script Programming Language Reference.
 
Contents
 
Creating Sequences
Using Predicates
Accessing Sequence Items
Inserting Items into a Sequence
Deleting Items from a Sequence
Reversing the Items in a Sequence
Comparing Sequences
Using Sequences Slices
 
Creating Sequences

One way to create a sequence is to explicitly list its items. Each item is separated by a comma and the list is enclosed in square brackets "[" and "]". For example, the following code:

def weekDays = ["Mon","Tue","Wed","Thu","Fri"];
 

declares a sequence and assigns it to weekDays. We use def in this example because we do not plan on changing its value after the sequence has been created. Here the compiler knows that we intend to create a "sequence of strings" because the individual items are all declared as String literals. If the sequence had been declared with Integers (for example, def nums = [1,2,3];), the compiler would know that we want a "sequence of integers" instead.

You can also explicitly specify a sequence's type by modifying its declaration to include the name of the type followed by "[]":

def weekDays: String[] = ["Mon","Tue","Wed","Thu","Fri"];
 

This tells the compiler that weekDays will hold a sequence of Strings (as opposed to a single String).

Sequences can also be declared within other sequences:

def days = [weekDays, ["Sat","Sun"]];
 

In such cases, the compiler will automatically flatten the nested sequences to form a single sequence, making the above equivalent to:

def days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
 

There is also a shorthand notation that makes it easier to create sequences that form an arithmetic series. To create a sequence consisting of the numbers 1 through 100, use the following:

def nums = [1..100];
 

Real-World Example: Display Shelf (Sequence Example #1)

 

This screenshot shows the "Display Shelf" demo application. The code excerpt to the right shows an instance variable (named "content") belonging to the Group object. This variable holds a sequence that has been initialized with two items (the highlighted ImageView and Rectangle objects). You can spot the presence of a sequence by looking for its opening and closing square brackets (also highlighted). Sequence items are delimited by commas, which is why "," appears between the ImageView and Rectangle objects.

Using Predicates

You can use a boolean expression, (also called a predicate), to declare a new sequence that is a subset of an existing sequence. For example, consider the following:

def nums = [1,2,3,4,5];
 

To create a second sequence (based on items found in this first sequence) but containing only numbers greater than 2, use the following:

def numsGreaterThanTwo = nums[n | n > 2];
 

You could express the previous line of code in English like this: "select all items from the num sequence where the value of an item is greater than 2 and assign those items to a new sequence called numsGreaterThanTwo."

In this code:

  1. The newly created sequence is stored in numsGreaterThanTwo.

  2. The code (marked in bold): nums[n | n > 2]; specifies the original sequence that the items are to be copied from. In our example, nums is the name of the original sequence.

  3. This selects the items from nums, and returns a new sequence consisting of those items, in order, for which the expression is true.

  4. The "|" character is used to visually separate the variable "n" from the rest of the code: nums[n | n > 2];

  5. The code (marked in bold): nums[n | n > 2]; defines a boolean expression specifying the criteria to be met before the current item will be copied into the new sequence.
Accessing Sequence Items

Sequence items are accessed by numerical index, starting at 0. To access an individual item, type the sequence name, followed by the item's numerical index enclosed within square brackets:

def days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];

println(days[0]);
println(days[1]);
println(days[2]);
println(days[3]);
println(days[4]);
println(days[5]);
println(days[6]);
 

This prints the following to the screen:

Mon
Tue
Wed
Thu
Fri
Sat
Sun
 

Also, you can determine the size of a sequence using the sizeof operator followed by the name of the sequence:

sizeof days
 

The following code prints "7" to the screen:

def days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
println(sizeof days);
 
Inserting Items into a Sequence

The insert keyword allows you to insert an item into a sequence, before a specific item, or after a specific item.


Note: Technically speaking, sequences are immutable, meaning that once created they never change. When you modify a sequence by inserting or deleting items, for example, behind the scenes a new sequence is created and the sequence variable is reassigned, giving the impression that the sequence was modified.

Let's explore this by re-creating the days sequence. Note that we now declare the days variable with var, because we will be changing its value after the original sequence has been created:

var days = ["Mon"];
 

At this point, the sequence contains only one item, "Mon".

We can insert "Tue" into the end of this sequence using the insert and into keywords:

insert "Tue" into days;
 

Similarly, we could add "Fri", "Sat" and "Sun" as well:

insert "Fri" into days;
insert "Sat" into days;
insert "Sun" into days;
 

The sequence now contains: "Mon", "Tue", "Fri", "Sat", and "Sun".

We can also use the insert and before keywords to insert an item before an item at a given index. Keep in mind that indexing begins at 0, so in our current sequence, "Fri" sits at index position 2. Therefore, we can insert "Thu" before "Fri" as follows:

insert "Thu" before days[2];
 

The sequence now contains: "Mon", "Tue", "Thu", "Fri", "Sat", and "Sun".

To insert "Wed" after "Tue", we can use the insert and after keywords:

insert "wed" after days[1];
 

The sequence now contains all the days of the week: "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", and "Sun".

Deleting Items from a Sequence

The delete and from keywords make it easy to delete items from a sequence:

delete "Sun" from days;
 

The sequence now contains: "Mon", "Tue", "Wed", "Thu", "Fri", and "Sat".

You can also delete an item from a specific index position. The following code deletes "Mon" from the sequence (remember that "Mon" is the first item, so its index position is 0.)

delete days[0];
 

To delete all items from a sequence, use the delete keyword followed by the name of the sequence:

delete days;
 

Note that delete only removes the items from the sequence; it does not delete the days variable from your script. You can still access the days variable and add new items to it as before.

Reversing the Items in a Sequence

You can easily reverse the items in a sequence using the reverse operator:

var nums = [1..5];
nums = reverse nums; // returns [5, 4, 3, 2, 1]
 
Comparing Sequences

There may be times when you will want to compare sequences for equality. Sequences are compared for equality by value: if their lengths are equal, and their items are equal, then they are equal.

Let's test this out by creating two sequences with identical contents:

def seq1 = [1,2,3,4,5];
def seq2 = [1,2,3,4,5];
println(seq1 == seq2);
 

The expression seq1 == seq2 evaluates to true because both sequences have the same number of items, and the value of each item is the same in both sequences. This code therefore prints "true" to the screen.

By changing the number of items in one sequence (but not the other), the sequences now become different lengths:

def seq1 = [1,2,3,4,5];
def seq2 = [1,2,3,4,5,6];
println(seq1 == seq2);
 

Here the output of this script is "false" because the second sequence is longer than the first, thereby making them unequal.

We can also make the two sequences unequal by changing the item values, even if the sequences are still the same length:

def seq1 = [1,2,3,4,5];
def seq2 = [1,3,2,4,5];
println(seq1 == seq2);
 

Again, this code will print "false" because the two sequences are not equal.

Using Sequence Slices

Sequence slices provide access to portions of a sequence.

seq[a..b]

This syntax provides access to the items between index a and index b, inclusive. The following script creates a weekend sequence consisting of only the items "Sat" and "Sun".

def days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
def weekend = days[5..6];
 

seq[a..<b]

Use the "<" character to access the items between index a inclusive and index b exclusive. We could use this on days to create a weekdays sequence consisting of the items "Mon" through "Fri".

def days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
def weekdays = days[0..<5];
 

seq[a..]

By omitting the second index, you can access all items from index a through the end of the sequence. In keeping with the same example, we could create the weekend sequence as follows:

def days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
def weekend = days[5..];
 

seq[a..<]

Finally, you can use "<" without a second index to access everything from index a to the end of the sequence, excluding the last item.

def days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
def days2 = days[0..<];
 

This version creates a days2 sequence consisting of the items "Mon" through "Sat".

Real-World Example: Display Shelf (Sequence Example #2)

 

This code listing from the "Display Shelf" demo application shows the use of two sequence slices. The slices are declared with ranges of "0 .. half-2" and "half..content.size()-1", respectively. To identify a sequence slice, look for ".." inside the square brackets.

 
« Previous 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Next »