insert()/insertElement()

 

The insert() method inserts one array element into the specified position. It accepts one or two arguments:

 

array_variable.insert(index ,[value])

 

The index argument is an integer expression. It specifies the position where the element is to be added. The value parameter specifies the value to be stored in this element, it should be compatible with the data type of the array. If only one parameter is provided, it is treated as the index and is added at the specified position.

The InsertElement() method inserts one empty element to a specified position. The method needs an argument specifying the position to which the element is to be inserted.

 

array_variable.insertElement(index)

 

When an element is inserted, all the subsequent array elements are moved downwards. The dynamic array size increases by 1.

 

DEFINE ar DYNAMIC ARRAY OF INT

...

FOR i = 1 TO 5                   #the array has 5 elements

   LET ar[i]=i

END FOR

 

CALL ar.insert(2)                #inserts 0 as the second element, now array has 6  #elements

CALL ar.insert(3, 1000)          #inserts 1000 as the third element, the array now     #has 7 elements