Insert (Arraylist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (add comment to example)
 
(34 intermediate revisions by 6 users not shown)
Line 1: Line 1:
<span style="font-size:120%; color:black"><b><section begin=dpl_desc/>Insert item into Arraylist<section end=dpl_desc/></b></span>
{{Template:Arraylist:Insert subtitle}}
[[Category:Arraylist methods|Insert function]]
This [[Notation conventions for methods#Callable functions|callable]] function inserts one or more new items before the <var class="term">number</var> item in the <var>Arraylist</var>.
<!--DPL?? Category:Arraylist methods|Insert function: Insert item into Arraylist-->
 
<p>
==Syntax==
Insert is a member of the [[Arraylist class]].
{{Template:Arraylist:Insert syntax}}
</p>
 
===Syntax terms===
<table class="syntaxTable">
<tr><th>%count</th>
<td>An, optional, numeric variable to return the number of items in the indicated <var>Arraylist</var> after the items have been inserted. </td></tr>
 
<tr><th>al</th>
<td>An <var>Arraylist</var> object. </td></tr>


This callable function
<tr><th>number</th>
inserts an item before the ''number'' item in the Arraylist.
<td>A whole number ordinal, from 1 to the number of items in <var class="term">al</var> plus one, to identify an <var class="term">al</var> item <b><i>before</i></b> which the items in <var class="term">itemList</var> will be inserted.</td></tr>
===Syntax===
  [%num =] %arrayl:Insert(number, item)
====Syntax Terms====
<dl>
<dt><i>%num</i>
<dd>A numeric variable to contain the number of
items in the indicated Arraylist after the item has been inserted.
<dt><i>%arrayl</i>
<dd>An Arraylist object.
<dt><i>number</i>
<dd>A whole number greater than 0 to identify an ''%arrayl'' item.
<dt><i>item</i>
<dd>A value or variable of the same
type as specified on the ''%arrayl'' declaration, or a value or variable
convertible to that type.


</dl>
<tr><th>itemList</th>
===Usage Notes===
<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>
<ul>
<li>If ''number'' is less than or equal to the number of items
<li>Values or variables of the same type as specified on the <var class="term">al</var> declaration
in the Arraylist, the new item is inserted before the indicated item.
<li>Collections of objects of the same type as specified on the <var class="term">al</var> declaration
<li>If ''number'' is equal to the number of items in the Arraylist
</ul>
plus one, the new item is added to the end of the Arraylist.
 
<li>If ''number'' is greater than the number of items in the Arraylist
Each of these, from left to right, becomes an item or items inserted into the method object <var>Arraylist</var>.
plus one, or less than or equal to zero, the request is cancelled.
 
<li>For an example using the Insert method, see [[Arraylist class]].
Under <var class="product">Sirius Mods</var> 7.8 and earlier, <var>itemList</var> is only a single item to be inserted into <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>
 
==Usage notes==
<ul>
<li>If <var class="term">number</var> is less than or equal to the number of items in the <var>Arraylist</var>, the new items are inserted before the indicated item. </li>
 
<li>If <var class="term">number</var> is equal to the number of items in the <var>Arraylist</var> plus one, the new items are added to the end of the <var>Arraylist</var>. </li>
 
<li>If <var class="term">number</var> is greater than the number of items in the <var>Arraylist</var> plus one, or less than or equal to zero, the request is cancelled.
</ul>
</ul>
==Examples==
For more examples of <var>Insert</var> calls, see [[Arraylist class]].
====Inserting numbers into an Arraylist====
In the following example, a literal number, a numeric variable and another <var>Arraylist</var> of numbers is inserted into an <var>Arraylist</var>:
<p class="code">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:insert(2, 16, %perfect, %primes)
%nums:print
end
</p>
This outputs:
<p class="output">1: 0
2: 16
3: 6
4: 2
5: 3
6: 5
7: 7
8: 11
9: 13
10: 17
11: 23
12: 1
</p>
====Inserting objects into an Arraylist====
In the following example, a new object, an object variable and another <var>Arraylist</var> of objects are inserted into an <var>Arraylist</var>:
<p class="code">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:insert(2, new(4, 3), %where, %locations)
%points:print
end
</p>
This outputs:
<p class="output">1: coordinate: x=6, y=9
2: coordinate: x=4, y=3
3: coordinate: x=17, y=-3
4: coordinate: x=1, y=1
5: coordinate: x=2, y=5
6: coordinate: x=3, y=11
7: coordinate: x=-4, y=3
</p>
====Inserting a copy of the method object Arraylist====
In the following example, the method object <var>Arraylist</var> is also an item to be inserted:
<p class="code">b
%s is arraylist of float
%s = list(1, 2)
%s:insert(2, 99, %s)
%s:print
end </p>
This outputs:
<p class="output">1: 1
2: 99
3: 1
4: 2
5: 2 </p>
<p>
As this example demonstrates, if a method object <var>Arraylist</var> is also in the list of items to be inserted, the content of the object is copied before the insertion, then that copy is used as the inserted item. </p>
==See also==
{{Template:Arraylist:Insert footer}}

Latest revision as of 23:11, 27 July 2017

Insert items into Arraylist (Arraylist class)

This callable function inserts one or more new items before the number item in the Arraylist.

Syntax

[%count =] al:Insert( number, itemList)

Syntax terms

%count An, optional, numeric variable to return the number of items in the indicated Arraylist after the items have been inserted.
al An Arraylist object.
number A whole number ordinal, from 1 to the number of items in al plus one, to identify an al item before which the items in itemList will be inserted.
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 inserted into the method object Arraylist.

Under Sirius Mods 7.8 and earlier, itemList is only a single item to be inserted into 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

  • If number is less than or equal to the number of items in the Arraylist, the new items are inserted before the indicated item.
  • If number is equal to the number of items in the Arraylist plus one, the new items are added to the end of the Arraylist.
  • If number is greater than the number of items in the Arraylist plus one, or less than or equal to zero, the request is cancelled.

Examples

For more examples of Insert calls, see Arraylist class.

Inserting numbers into an Arraylist

In the following example, a literal number, a numeric variable and another Arraylist of numbers is inserted into 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:insert(2, 16, %perfect, %primes) %nums:print end

This outputs:

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

Inserting objects into an Arraylist

In the following example, a new object, an object variable and another Arraylist of objects are inserted into 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:insert(2, 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=17, y=-3 4: coordinate: x=1, y=1 5: coordinate: x=2, y=5 6: coordinate: x=3, y=11 7: coordinate: x=-4, y=3

Inserting a copy of the method object Arraylist

In the following example, the method object Arraylist is also an item to be inserted:

b %s is arraylist of float %s = list(1, 2) %s:insert(2, 99, %s) %s:print end

This outputs:

1: 1 2: 99 3: 1 4: 2 5: 2

As this example demonstrates, if a method object Arraylist is also in the list of items to be inserted, the content of the object is copied before the insertion, then that copy is used as the inserted item.

See also