List (Arraylist function): Difference between revisions
m (1 revision) |
m (1 revision) |
(No difference)
|
Revision as of 21:18, 1 July 2011
Construct and populate a new Arraylist (Arraylist class)
The List shared function is a virtual constructor, or factory method, for Arraylistd. List invokes the creation of a new Arraylist instance, then populates that instance with items that are, respectively, the values of the method arguments.
Syntax
%newList = [%(Arraylist Of itemType):]List( itemList)
Syntax terms
%newlist | A declared or existing Arraylist object. |
---|---|
%(Arraylist) | The class name in parentheses denotes a ÝÝNotation conventions for methods#Shared methods|shared]] method. List can also be invoked via an Arraylist object variable, which may be null. |
itemList | A comma-delimited set of values or variables of the same type as specified on the al declaration or collections of objects of the same type as specified on the al declaration. Each of these, from left to right, becomes an item or items in the resultant new Arraylist, al. The ability to include a collections in itemList was added in Sirius Mods 7.9. |
Usage notes
- For more information about factory methods, see Virtual Constructor methods.
- The maximum number of items values is 62.
- List is a constructor and as such can be called with no method object, with an explicit class name, or with an object variable, even if that object is null:
%al = List(items) %al = %(Arraylist):List(items) %al = %al:List(items)
Note: If using the second of these syntax options, which explicitly indicates the class, both the collection and item datatypes must be specified exactly as they are on the collection variable's declaration:
%al is collection Arraylist of longstring %al = %(Arraylist of longstring):List('Jack', 'and', 'Jill')
- You can also use the List method in contexts where an Arraylist object is a method parameter.
- List is available in Sirius Mods Version 7.3 and later.
Examples
Creating an Arraylist of numbers
The following List method call creates a new Arraylist instance of 11 integer items that begins with 1 and whose subsequent items are, respectively, the sum of the preceding two integers:
%fib is collection arraylist of float %fib = list(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)
Creating an Arraylist of objects
The following List call creates a new Arraylist instance whose items are objects of user class freight:
class freight public variable a is float variable b is float constructor newf(%a is float, %b is float) end public ... end class ... %ark is arraylist of object freight %ark = list(newf(11, 14), newf(4, 6), newf(9, 77), - newf(-7, 1234))
Creating an Arraylist from objects and Arraylists
Under Sirius Mods 7.9 and later it is possible to include collections in the list of items being added. If a collection is in the list, each item in the collection is added to the target Arraylist.
The following List call creates a new Arraylist instance whose items are objects of user class freight, some of them from existing Arraylists %ark1
and %ark2
and some newly created objects.
class freight public variable a is float variable b is float constructor newf(%a is float, %b is float) end public ... end class ... %ark is arraylist of object freight %ark1 is arraylist of object freight %ark2 is arraylist of object freight %ark = list(newf(11, 14), %ark1, newf(9, 77), %ark2, - newf(-7, 1234))