Sequences (think arrays) are the main data structure in JavaFX Script. Sequences are useful for storing arrays of basic and object data types, as well as providing a structure to which UI components can be bound. For example, the elements in a ListBox can be bound to a sequence of String objects. As a result, if the data in the sequence changes the ListBox elements will change as well. Sequences also provide very powerful list comprehension and manipulation capabilities, some of which are demonstrated in the example program below. This example is from my JavaFX Script book after having been converted to compiled JavaFX Script syntax. It uses the planets to help me explain some sequence related concepts, and bids farewell to Pluto as a planet.
Compile and run this example, and you should see the output that appears after the example.
/*
* SequenceExample.fx - An example creating a sequence literal and using
* some sequence related features of compiled JavaFX
* Script to work with it.
*
* Developed 2008 by James L. Weaver (jim.weaver at lat-inc.com)
*/
import java.lang.System;
// Literally define the planets sequence, leaving out Jupiter and Uranus
var planets:String[] = ["Mercury", "Venus", "Earth", "Mars",
"Saturn", "Neptune"];
// Print out the third element in the array, which is element 2
// since arrays are zero-based.
System.out.println("The third planet is:{planets[2]}");
// Print out all planets by iterating over the planets sequence
for (planet in planets) {
System.out.println("{planet} is a planet in our solar system");
}
System.out.println();
// Insert Uranus before Neptune (which is currently in position 5)
System.out.println("Inserting Uranus before Neptune");
insert "Uranus" before planets[5];
// Insert Jupiter after Mars (which is currently in position 3)
System.out.println("Inserting Jupiter after Mars");
insert "Jupiter" after planets[3];
// Add Pluto to the end of the sequence.
System.out.println("Inserting Pluto as the last planet");
insert "Pluto" into planets;
// Add Sun to the beginning of the sequence
System.out.println("Inserting the Sun into the beginning");
insert "Sun" before planets[0];
// Print out all planets by iterating over a sequence that is the same
// size as the planets sequence, and reference the corresponding element
// in the planets array.
for (i in [0.. sizeof planets - 1]) {
System.out.println("{planets[i]} is a planet in our solar system");
}
System.out.println();
// Delete the Sun (which is currently in position 0)
System.out.println("Deleting the Sun in position 0");
delete planets[0];
// Query for the indexof Pluto and delete it
System.out.println("Querying for the indexof Pluto");
var indices =
for (planet in planets
where planet == "Pluto") {
delete planets[indexof planet];
indexof planet;
}
System.out.println("Pluto was in position {indices} and has been deleted");
// Print all of the planets in reverse order
System.out.println(reverse planets);
System.out.println();
// Use for to query all of the planets with more than 5 characters
var somePlanets:String[];
somePlanets =
for (planet in planets
where planet.length() > 5)
"{planet},";
System.out.println("The {sizeof somePlanets} planets with more than 5 characters are:
{somePlanets}");
// Use for to query for all of the planets that begin with
// the letter "N" or later
somePlanets =
for (planet in planets
where planet.compareTo("N") > 0)
"{planet},";
System.out.println("The {sizeof somePlanets} planets in the second half of the alphabet are:
{somePlanets}");
The Output, After Our Smallest Planet Gets Plutoed;
The third planet is:Earth
Mercury is a planet in our solar system
Venus is a planet in our solar system
Earth is a planet in our solar system
Mars is a planet in our solar system
Saturn is a planet in our solar system
Neptune is a planet in our solar system
Inserting Uranus before Neptune
Inserting Jupiter after Mars
Inserting Pluto as the last planet
Inserting the Sun into the beginning
Sun is a planet in our solar system
Mercury is a planet in our solar system
Venus is a planet in our solar system
Earth is a planet in our solar system
Mars is a planet in our solar system
Jupiter is a planet in our solar system
Saturn is a planet in our solar system
Uranus is a planet in our solar system
Neptune is a planet in our solar system
Pluto is a planet in our solar system
Deleting the Sun in position 0
Querying for the indexof Pluto
Pluto was in position 8 and has been deleted
[ Neptune, Uranus, Saturn, Jupiter, Mars, Earth, Venus, Mercury ]
The 5 planets with more than 5 characters are:
Mercury,Jupiter,Saturn,Uranus,Neptune,
The 4 planets in the second half of the alphabet are:
Venus,Saturn,Uranus,Neptune,
Sequence Literals
I've used sequence literal syntax to literally define a sequence several times already in the "Helping you Become a JavaFXpert" weblog. For example, sequence literals are used in the Simple Compiled JavaFX Script Example of Using Sequences post. That post, by the way, would be a good place to refresh yourself on (or get started developing) compiled JavaFX Script programs.
The first executable line in the example program (see the following excerpt) literally defines a sequence and assigns it to a variable that is explicitly typed as a sequence of String instances. Please note that the :String[] isn’t required, because JavaFX implicitly types a variable based on the expression on the right-hand side of the assignment operator (=).
// Literally define the planets sequence, leaving out Jupiter and Uranus
var planets:String[] = ["Mercury", "Venus", "Earth", "Mars",
"Saturn", "Neptune"];
Accessing a Specific Element of a Sequence
As shown in the following excerpt from the example program, to access a sequence element directly, put the zero-based index of the element in square brackets following the sequence name:
// Print out the third element in the array, which is element 2
// since arrays are zero-based.
System.out.println("The third planet is:{planets[2]}");
Iterating Over a Sequence
The example program uses the for statement to iterate over a sequence a couple of ways, shown following:
// Print out all planets by iterating over the planets sequence
for (planet in planets) {
System.out.println("{planet} is a planet in our solar system");
}
// Print out all planets by iterating over a sequence that is the same
// size as the planets sequence, and reference the corresponding element
// in the planets array.
for (i in [0.. sizeof planets - 1]) {
System.out.println("{planets[i]} is a planet in our solar system");
}
Note the use of the sizeof operator to ascertain the number of elements in the sequence.
Inserting Sequence Elements
To insert an element into a sequence, use the insert statement as shown in the following excerpt:
// Insert Uranus before Neptune (which is currently in position 5)
System.out.println("Inserting Uranus before Neptune");
insert "Uranus" before planets[5];
// Insert Jupiter after Mars (which is currently in position 3)
System.out.println("Inserting Jupiter after Mars");
insert "Jupiter" after planets[3];
// Add Pluto to the end of the sequence.
System.out.println("Inserting Pluto as the last planet");
insert "Pluto" into planets;
// Add Sun to the beginning of the sequence
System.out.println("Inserting the Sun into the beginning");
insert "Sun" before planets[0];
There are a few variations of the insert statement, as shown in the preceding excerpts:
- insert element into sequence: This was used to insert Pluto. This form of the insert statement adds the element to the end of the sequence.
- insert element before sequence[index]: This was used to insert Uranus. This form of the insert statement inserts the element before a specified element that is already in the sequence.
- insert element after sequence[index]: This was used to insert Jupiter. This form of the insert statement inserts the element after a specified element that is already in the sequence.
Querying Sequences
You can query a sequence to retrieve a subset of its elements. The example program shows examples of for, which is the list comprehension operator. Here are some excerpts:
var somePlanets:String[];
somePlanets =
for (planet in planets
where planet.length() > 5)
"{planet},";
System.out.println("The {sizeof somePlanets} planets with more than 5 characters are:
{somePlanets}");
This query chooses all of the elements in the planets sequence that consist of more than five characters. The result is held in a sequence referred to by the variable named somePlanets. A variable named planet is implicitly created that holds a reference to each element that matches the query. As you can see, I decided to concatenate each element that is returned with a comma before placing it in the somePlanets array.
Shown following is another example of a for query that returns a sequence of numbers that are indexes into each element that matches the query. In the process it deletes any elements that meet the criterion. Compiled JavaFX Script is an expression language, and the last expression in a block (in this case, indexof planets) is the value of the block. For more information, check out the for expression and block expression posts.
On a slightly related note, shown following is a statement that prints all of the elements in the planets sequence in reverse:
System.out.println(reverse planets);
Deleting Sequence Elements
To delete an element from a sequence, you can use the delete statement as shown in the following excerpt:
delete planets[0];
This deletes element 0 from the planets sequence. Another way of deleting an element (but isn't shown in the example program) is to supply the value of the element. For example, if you wanted to delete Mars, you could use the following statement:
delete "Mars" from planets
Zeroing Out a Sequence
To cause a sequence to have no elements, use the sequence literal notation with nothing inside the square brackets. For example, if you wanted to zero out the planets sequence you could use the following statement:
planets = [];
As always, if you have any questions, please post a comment.
JavaFX Script Boot Camp Announcement
As a heads-up, I will be offering a JavaFX Script 2.5 day "boot camp" on Wednesday, April 9 through Friday, April 11, 2008 (ending at noon) in Indianapolis, Indiana. This course is designed to get you quickly up to speed in JavaFX Script application development. A primary reference for this course is my JavaFX Script book, but the course has its own syllabus which includes material covered in the book as well as up to the minute developments in compiled JavaFX Script. Registration will open soon, and for this pilot class I am accepting 12 students. The cost of this pilot class will be 900 USD per student. Additional students from the same organization will be 600 USD. You'll need to bring your laptop computer with the latest versions of the JavaFX Script downloads (which I'll specify in more detail as the class date approaches). The prerequisite for the class will be the completion of a JavaFX Script programming assignment that I'll post soon to this weblog. I'm looking forward to teaching this class and hope that you can attend!
0 comments:
Post a Comment