List (Arraylist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
 
(40 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Template:Arraylist:List subtitle}}
{{Template:Arraylist:List subtitle}}
The <var>List</var> shared function is a virtual constructor, or factory method, for <var>Arraylistd</var>.  <var>List</var> invokes the creation of a new <var>Arraylist</var> instance, then populates that instance with items that are, respectively, the values of the method arguments.


This shared function is a virtual constructor,
or factory method, for <var>Arraylist</var>s.
The <var>List</var> method invokes the creation of a new <var>Arraylist</var>
instance, then populates that instance with items that are, respectively,
the values of the method arguments.
<var>List</var> is available in ''Sirius Mods'' version 7.3 and later.
==Syntax==
==Syntax==
{{Template:Arraylist:List syntax}}
{{Template:Arraylist:List syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th><i><var class="term">al</var></i></th>
<tr><th>%newlist</th>
<td>A declared or existing <var>Arraylist</var> object. </td></tr>
<td>A declared or existing <var>Arraylist</var> object. </td></tr>
<tr><th><i>itemlist</i></th>
 
<td>A comma-delimited set of values or variables of the same type as specified on the ''<var class="term">al</var>'' declaration. Each of these, from left to right, becomes an item in the resultant new <var>Arraylist</var>, ''<var class="term">al</var>''.</td></tr>
<tr><th nowrap="true"><var>[%(Arraylist Of </var>itemType<var>):]</var></th>
<td>The optional class name in parentheses denotes a [[Notation conventions for methods#Constructors|virtual constructor]] method. See "Usage notes," below, for more information about invoking a collection constructor.</td></tr>
 
<tr><th>itemList</th>
<td>A comma-delimited set of a combination of one or more of these: 
<ul>
<li>Values or variables of the same type as specified on the <var class="term">al</var> declaration  
<li>Collections of objects of the same type as specified on the <var class="term">al</var> declaration
</ul>
 
Each of these, from left to right, becomes an item or items in the resultant new <var>Arraylist</var>, <var class="term">al</var>.
 
The ability to include a collection in <var class="term">itemList</var> was added in <var class="product">Sirius Mods</var> 7.9.</td></tr>
</table>
</table>
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>For more information about factory methods, see [[??]] refid=vconst..
<li><var>List</var> is a virtual 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:
<li>The maximum number of ''itemlist'' values is 62.
<p class="code">%al = List(<var class="term">items</var>)
<li><var>List</var> is a constructor and as such can be called with no method
 
object, with an explicit class name, or with an object variable,
%al = %(Arraylist of float):List(<var class="term">items</var>)
even if that object is null:
 
<pre style="xmp">
%al = %al:List(<var class="term">items</var>)
    <var class="term">al</var> = <var>List</var>(&'italic(itemlist))
</p>
    <var class="term">al</var> = %(<var>Arraylist</var>):<var>List</var>(&'italic(itemlist))
<b>Note: </b>
    <var class="term">al</var> = <var class="term">al</var>:<var>List</var>(&'italic(itemlist))
As shown above, when explicitly indicating the
</pre>
class, both the collection and the item datatype must be
:note
specified just as they are on the collection variable's declaration:
If using the second of these syntax options, which explicitly indicates the
<p class="code">%al is collection Arraylist of longstring
class, both the collection and item datatypes must be
%al = %(Arraylist of longstring):List('Jack', 'and', 'Jill')
specified exactly as they are on the collection variable's declaration:
</p>
<pre style="xmp">
<li>The maximum number of <var class="term">items</var> values is 62.
    <var class="term">al</var>  is collection <var>Arraylist</var> of longstring
<li>You can also use the <var>List</var> method in contexts where an <var>Arraylist</var> object is a method parameter.
    <var class="term">al</var>  = %(<var>Arraylist</var> of longstring):<var>List</var>(&'italic(itemlist))
<li><var>List</var> is available in <var class="product">[[Sirius Mods|Sirius Mods]]</var> Version 7.3 and later.
</pre>
<li>You can also use the <var>List</var> method in contexts where an <var>Arraylist</var> object
is a method parameter.
</ul>
</ul>
==Examples==
==Examples==
<ul>
====Creating an Arraylist of numbers====
<li>The following <var>List</var> method call creates a new <var>Arraylist</var> instance
The following <var>List</var> method call creates a new <var>Arraylist</var> instance of 11 integer items that begins with 1 and whose subsequent items are, respectively, the sum of the preceding two integers:
of 11 integer items that begins with 1 and whose subsequent items are,
<p class="code">%fib    is collection arraylist of float
respectively, the sum of the preceding two integers:
%fib = list(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)
<pre style="xmp">
</p>
    %fib    is collection arraylist of float
 
    %fib = list(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)
====Creating an Arraylist of objects====
</pre>
The following <var>List</var> call creates a new <var>Arraylist</var> instance whose items are objects of user class <var class="term">freight</var>:
<li>The following <var>List</var> call creates a new <var>Arraylist</var> instance whose
<p class="code">class freight
items are objects of user class Freight:
public
<pre style="xmp">
    variable a is float
    class freight
    variable b is float
      public
    constructor newf(%a is float, %b is float)
        variable a is float
end public
        variable b is float
...
        constructor newf(%a is float, %b is float)
end class
      end public
...
      ...
%ark is arraylist of object freight
    end class
%ark = list(newf(11, 14), newf(4, 6), newf(9, 77),  -
      ...
            newf(-7, 1234))
    %ark is arraylist of object freight
</p>
    %ark = list(newf(11, 14), newf(4, 6), newf(9, 77),  -
 
                newf(-7, 1234))
====Creating an Arraylist from objects and Arraylists====
</pre>
Under <var clas="product">Sirius Mods</var> 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 <var>Arraylist</var>.
</ul>
The following <var>List</var> call creates a new <var>Arraylist</var> instance whose items are objects of user class <var class="term">freight</var>, some of them from existing <var>Arraylists</var> <code>%ark1</code> and <code>%ark2</code> and some newly created objects.
<p class="code">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))
</p>
 
==See also==
==See also==
{{Template:Arraylist:List footer}}
{{Template:Arraylist:List footer}}

Latest revision as of 16:08, 1 November 2012

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 Of itemType):] The optional class name in parentheses denotes a virtual constructor method. See "Usage notes," below, for more information about invoking a collection constructor.
itemList A comma-delimited set of a combination of one or more of these:
  • Values or variables of the same type as specified on the al declaration
  • 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 collection in itemList was added in Sirius Mods 7.9.

Usage notes

  • List is a virtual 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 of float):List(items) %al = %al:List(items)

    Note: As shown above, when explicitly indicating the class, both the collection and the item datatype must be specified just as they are on the collection variable's declaration:

    %al is collection Arraylist of longstring %al = %(Arraylist of longstring):List('Jack', 'and', 'Jill')

  • The maximum number of items values is 62.
  • 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))

See also