This time we will talk about sequences, these are an addition to the basic types of JavaFX, language provides us with a structure called a sequence which represents an ordered list of objects (Although they are not just objects), each object within the sequence is called item. To declare a sequence we use [] and enter in each item separated by commas. Content
- Creating Sequences Using Sequence Predicates
- Accessing Items Items
- Inserting Into a Sequence
- Deleting Items from a Sequence
- Reversing the Items in a Sequence
- Comparing Sequences Sequences Using Slices
Creating Sequences
We have several ways to declare such a sequence could lock the items in [] separated by commas. Def
weekDays1 = ["Monday", "Tuesday"] / / Here we do not define the data type def
weekDays2: String [] = ["Wednesday", "Thursday", "Friday"] / / Here inform the type of
Items An important feature of the sequences is that they can be constructed based on other sequences, for the example above could do made the following combination / / Here we obtain a sequence with every day
def days = [weekDays1, weekDays2, ["Saturday", "Domingo"]];
println (days) / / Print [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Domingo]
kind
Items Here the compiler actually flattens the above sequences to generate the new sequence. JavaFX script also gives us a simpler way to work with arithmetic sequences. Example def nums = [1 .. 100];
This would create a sequence which possess the numbers 1 to 100 Predicates Using
We refer to predicates when using a Boolean expression to generate a new sequence is formed by a subset of the original sequence.
Example:
def nums = [1,2,3,4,5];
numsGreater def nums = [n we feel the need to access the items that are stored in our streams and for this we can use the indexes of the sequence.
def days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" "Sunday"]; println (days [0]); println (days [ 1]); println (days [2]); println (days [3]);
println (days [4]);
println (days [5]);
println (days [6]);
In this case there would be problems because we know the number of items that exist in our sequence if we wanted to know the number of items that there should use the keyword sizeof
def days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" "Sunday"]; println (sizeof days) / / This would print the number 7
Items Inserting a Sequence Into Another
language keyword is the word insert, this is used to achieve insert item in a sequence. Here we must take into account a fact that is not a minor and that the sequences really are immutable (unchanging means that after being created are never modified), then we really to insert an item in a sequence below creates truth a new sequence and is reassigned automatically, giving the impression that we modify the existing sequence.
/ / initialize the sequence with a single item
var days = ["Monday"];
/ / Insert two items at the end of the sequence
insert "Tuesday" into days;
insert "Thursday" into days; / / Insert a item before the item at position 2 insert "Wednesday" before days [2]; / / Insert a item after item of position 3
insert "Friday" after days [3]; If you use positions
invalid
nothing happens and the items are inserted at the end or beginning depending on the case, for example if will insert an item after days [24] we would really be inserted at the end of the sequence or days Before an item [-1] this is inserted at the beginning. Deleting Items from a Sequence As we have already inserted item should also be possible to delete them and this can be done through using delete keyword in different ways
def days = ["Monday", "Tuesday" "Wednesday", "Thursday", "Friday", "Saturday" "Sunday"];
/ / We delete the item "Monday"
delete "Monday" from days;
/ / We delete the item from position 0
delete days [0];
/ / Clear all item, but the sequence is still useful
delete days;
This is very similar to the above, note that if items are marked or to delete the selected item does not exist, do not perform the deletion. Reversing the Items in a Sequence
This technique allows us to spend an entire sequence using only the keyword reverse. An example to clarify this
/ / start the sequence with the numbers 1 to 5
var nums = [1 .. 5]; / / We turn
reverse sequence nums;
/ / This prints [1 , 2, 3, 4, 5]
println (nums);
/ / invest sequence but now if we assign the result
nums nums = reverse;
/ / This prints [5, 4, 3, 2, 1]
println (nums); The above example shows how the sequence is not allocate their reverse order and in this case must be explicit by the programmer, does not work as delete or insert which is done automatically. Comparing Sequences
To compare the sequences are taken into account two points, the first length and the second correspondence between their item. On completion of these two landmarks can be sure that the strings are equal.
/ / Create two identical sequences SEQ1
def = [1,2,3,4,5];
def seq2 = [1,2,3,4,5];
/ / compare and this comparison returns true
println (SEQ1 == seq2);
Now insert an item to the seq2 to achieve that is different than the other and to re-run the comparison.
/ / Create two identical sequences SEQ1 var = [1,2,3,4,5]; var seq2 = [1,2,3,4,5]; / / Insert an element in seq 6 Into insert
seq2; / / print seq2 to verify that change
println (seq2);
/ / compare and this comparison returns false
println (SEQ1 == seq2);
far we saw the case of equality and the case where the length is greater, we now remains to see if their items have a different position and this also creates an inequality between sequences.
/ / Create two identical sequences SEQ1 var = [1,2,3,4,5]; var seq2 = [1,2,3,4,5]; / / Insert an element in seq seq2 = seq2
reverse; / / print seq2 to verify that your order
change println (seq2);
/ / compare and this comparison returns false
println (SEQ1 == seq2);
We cover all 3 cases comparison between sequences, now valleys last point
Sequences Using Slices Before proceeding to explain the different ways to get a slice, we should understand it is a slice, a slice is basically just a portion of a sequence based on a criterion or selection, you could define a slice as a subset of the sequence A slice is a sequence. slice can be defined in the following ways
seq [a.. B] seq [a.. \u0026lt;B]
seq [a..]
seq [a.. \u0026lt;]
In the above definitions seq sequence would want to get a slice and b the start and end indexes, explain each
seq [a.. b]
def days = ["Monday", "Tuesday" "Wednesday", "Thursday", "Friday", "Saturday" "Sunday"]; / / The new sequence would have the item "Saturday" and "Sunday" def = weekend days [5 .. 6];
This way to create a slice includes two indexes
seq [a.. \u0026lt;b]
def days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday" "Saturday" "Sunday"]; / / The new sequence would have all the item less "Saturday" and "Sunday" weekdays
def = days [0 .. \u0026lt;5];
In this case only the first position is included and the other not.
seq [a..]
def days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" "Sunday"]; / / The new sequence would have the item "Saturday" and "Sunday" weekend def = days [5 ..];
Here we are defining a slice that begins at position 5 and to include that position until the end of the sequence inclusive.
seq [a..
def days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" "Sunday"]; / / The new sequence would have every item except the " Domingo "def
days2 = days [0 ..\u0026lt;]; This type of slice is similar to the previous and beginning at the first position and goes even to the end of the sequence but not including the last item
far we have analyzed the different actions and types and sequences referred to in next post we will be analyzing the binding and trigger in JavaFX, I am saying goodbye and I hope the post will be helpful Regards
0 comments:
Post a Comment