Planet JFX


118pages on
this wiki
Add New Page
Add New Page Talk0

Lists Edit

A List is an ordered sequence of values, of any length. List is a parameterized type; the elements of a List<T> must be of type T. Lists may be empty. List-valued variables whose value is null are treated as empty lists.

The type specifier for a List<T> in JavaFX code is T[]. List literals are created with the [ value, value, .... ] notation:

var a = [ 1, 2, 3 ]  			// inferred type List<Number>
var a : Number[] = [ 1, 2, 3 ]   	// explicit type

Lists of numbers can use the range shorthand n..m to mean the numbers from n to m, inclusive:

var a = [ 1, 2, 3..100 ]

Upconversion and downconversion Edit

The type of List<T> is distinct from T and has no super/subtype relationship, but instances are implicitly "upconverted" to lists when a value of type T is assigned to a variable of type List<T>:

var a : Number[];
a = 1;  				// same as a = [ 1 ]
function foo(a : Number[]) { ... }
foo(1)					// same as foo([ 1 ]);

Lists are not implicitly downconverted to instance values, but instead are objects with size(), first(), rest(), and last() methods, so that the user can easily choose how to explicitly downconvert from list to instance. Lists are immutable, so operations that modify a list return a new list instead.

List element retrieval Edit

Elements can be retrieved from a list with the familiar bracket operators: a[e]. The expression in the brackets can be a Number, a List<Number>, or a selection expression.

For a List<T>:

If the index expression is a Number, then a[e] is of type T, and the result is the element whose index is e. If e is out of range, an ArrayIndexOutOfBoundsException is thrown. (?)

var a = [ 1, 2, 3 ]
 -> 1
 -> 3

If the index expression is a List<Number>, then a[e] is of type T[], and the result is a list of elements from a whose indexes are the elements of e. If any elements of e correspond to indexes that are out of range, they are silently ignored. (?)

var a = [ 1..100 ]
a[1, 2, 3]
 -> [1, 2, 3]
 -> [1..100]

If the index expression is a selection expression of the form [ n | boolean predicate involving n ], then a[e] is of type T[], and the result is a list of elements which match the predicate. Predicate expressions may contain "indexof n", where n is the induction variable, which represents the index of the element being considered.

var a = [ 1.. 100 ]
a[n| n > 50]
  -> [51..100]
a[n| indexof n < 5 ]
  -> [ 1..5 ]

Loops Edit

Lists may be used to drive loops:

for (i in [1..100]) { loop body }

Options: The value of a loop expression could be the value of the last iteration, or it could be a list containing the values of all the iterations. The former is like a traditional loop; the latter is a foreach-loop comprehension.

Also on Fandom

Random Wiki