Add (Arraylist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 4: Line 4:
==Syntax==
==Syntax==
{{Template:Arraylist:Add syntax}}
{{Template:Arraylist:Add syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%count</th>
<tr><th>%count</th>
<td>An, optional, numeric variable that returns the number of items in the indicated <var>Arraylist</var> after the items have been added. <var class="term">%count</var> is also the item number associated with the last or only added item in the <var>Arraylist</var>. </td></tr>
<td>An, optional, numeric variable that returns the number of items in the indicated <var>Arraylist</var> after the items have been added. <var class="term">%count</var> is also the item number associated with the last or only added item in the <var>Arraylist</var>. </td></tr>
<tr><th>al</th>
<tr><th>al</th>
<td>An <var>Arraylist</var> object. </td></tr>
<td>An <var>Arraylist</var> object. </td></tr>
<tr><th>itemList</th>
<tr><th>itemList</th>
<td>Under <var class="product">Sirius Mods</var> 7.9 and later this can be a comma-delimited set of values or variables of the same type as specified on the <var class="term">al</var> declaration or collections of objects of the same type as specified on the <var class="term">al</var> declaration. Each of these, from left to right, becomes an item or items in the method object <var>Arraylist</var>.
<td>Under <var class="product">Sirius Mods</var> 7.9 and later, this is 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>


Under <var class="product">Sirius Mods</var> 7.8 and earlier <var>itemList</var> can only be a single value or variable of the same type as specified on the <var class="term">al</var> declaration, or a value or variable convertible to that type to be added to the <var class="term">al</var> <var>Arraylist</var> object.
Each of these, from left to right, becomes an item or items in the method object <var>Arraylist</var>.
 
Of course, a single value or variable is just a single item list so can be specified under <var class="product">Sirius Mods</var> 7.9 and later, too.</td></tr>


Under <var class="product">Sirius Mods</var> 7.8 and earlier, <var>itemList</var> is only a single item to be added to <var class="term">al</var>: a value or variable of the same type as specified on the <var class="term">al</var> declaration, or a value or variable convertible to that type.</td></tr>
</table>
</table>


==Usage notes==
==Usage notes==
<ul><li>The <var>[[Push (Arraylist function)|Push]]</var> and <var>[[Enqueue (Arraylist function)|Enqueue]]</var> functions are the same as the <var>Add</var> function with the exception that <var>Push</var> and <var>Enqueue</var> can only add a single item to the target <var>Arraylist</var> while (under <var class="product">Sirius Mods</var> 7.9 and later) <var>Add</var> can add many items to the target <var>Arraylist</var>:
<ul><li>The <var>[[Push (Arraylist function)|Push]]</var> and <var>[[Enqueue (Arraylist function)|Enqueue]]</var> functions are the same as the <var>Add</var> function, with the exception that <var>Push</var> and <var>Enqueue</var> can only add a single item to the target <var>Arraylist</var>, while (under <var class="product">Sirius Mods</var> 7.9 and later) <var>Add</var> can add many items to the target <var>Arraylist</var>:
<ul>
<ul>
<li><var>Push</var> was designed as a convenience for use with the <var>[[Pop (Arraylist function)|Pop]]</var> function.
<li><var>Push</var> was designed as a convenience for use with the <var>[[Pop (Arraylist function)|Pop]]</var> function.
Line 27: Line 33:


==Examples==
==Examples==
For more examples of <var>Add</var> calls, see [[Arraylist class]].
For more examples of <var>Add</var> calls, see [[Arraylist class|"Arraylist class"]].
 
===Adding numbers to an Arraylist===
===Adding numbers to an Arraylist===
In the following example, a literal number, a numeric variable and another <var>Arraylist</var> of numbers is added to an <var>Arraylist</var>:
In the following example, a literal number, a numeric variable and another <var>Arraylist</var> of numbers is added to an <var>Arraylist</var>:

Revision as of 20:06, 20 July 2012

Add Arraylist items (Arraylist class)

This callable function adds an item to the end of an Arraylist.

Syntax

[%count =] al:Add( itemList)

Syntax terms

%count An, optional, numeric variable that returns the number of items in the indicated Arraylist after the items have been added. %count is also the item number associated with the last or only added item in the Arraylist.
al An Arraylist object.
itemList Under Sirius Mods 7.9 and later, this is 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 method object Arraylist.

Under Sirius Mods 7.8 and earlier, itemList is only a single item to be added to al: a value or variable of the same type as specified on the al declaration, or a value or variable convertible to that type.

Usage notes

  • The Push and Enqueue functions are the same as the Add function, with the exception that Push and Enqueue can only add a single item to the target Arraylist, while (under Sirius Mods 7.9 and later) Add can add many items to the target Arraylist:
    • Push was designed as a convenience for use with the Pop function.
    • Enqueue was designed as a convenience for use with the Dequeue function.

Examples

For more examples of Add calls, see "Arraylist class".

Adding numbers to an Arraylist

In the following example, a literal number, a numeric variable and another Arraylist of numbers is added to an Arraylist:

b %nums is arraylist of float %perfect is float %primes is arraylist of float %nums = list(0, 1) %perfect = 6 %primes = list(2, 3, 5, 7, 11, 13, 17, 23) %nums:add(16, %perfect, %primes) %nums:print end

This outputs:

1: 0 2: 1 3: 16 4: 6 5: 2 6: 3 7: 5 8: 7 9: 11 10: 13 11: 17 12: 23

Adding objects to an Arraylist

In the following example, a new object, an object variable and another Arraylist of objects is added to an Arraylist:

b class coordinate public constructor new(%x is float, %y is float) function toString is longstring variable x is float variable y is float end public constructor new(%x is float, %y is float) %this:x = %x; %this:y = %y end constructor function toString is longstring returnText coordinate: x={%x}, y={%y} end function end class %points is arraylist of object coordinate %where is object coordinate %locations is arraylist of object coordinate %points = list(new(6, 9), new(-4, 3)) %where = new(17, -3) %locations = list(new(1, 1), new(2, 5), new(3, 11)) %points:add(new(4, 3), %where, %locations) %points:print end

This outputs:

1: coordinate: x=6, y=9 2: coordinate: x=-4, y=3 3: coordinate: x=4, y=3 4: coordinate: x=17, y=-3 5: coordinate: x=1, y=1 6: coordinate: x=2, y=5 7: coordinate: x=3, y=11

See also