ResizeArray

See Also: SizeOfArray, Array Functions, Array Variable Assignments, Working with Arrays

Purpose

The ResizeArray function returns a new array that is a copy of the specified array and resized accordingly.

Return Type

Array

Syntax

(ResizeArray( {ArrayId}, {iArraySize} [, {assignVal} ] ))

Where:

What it Does

Specifying a size that is larger than the current member count will append n elements to the end of the array, where n is the difference between the new and current (prior to resizing) number of elements. The new elements are initialized to the array type's null value. This is useful for preparing an array for many insertions (quickly and efficiently expanding to a specified size).

Specifying a smaller size than the current size of the specified array removes n elements from the end, effectively shrinking the array down to the specified size. Elements that were above the new array size are erased. To erase all elements of the array, simply specify a size of zero.

Examples

Procedure Test

    Integer[] iValues

    Integer iCount icCount

 

    Move (ResizeArray(iValues, 100)) to iValues // creates 100 array elements

    Move (SizeOfArray(iValues)) to icCount

 

    For iCount from 0 to (icCount – 1)

        Move iCount to iValues[iCount]          // move value to each element

    Loop

 

    Move (ResizeArray(iValues, 0)) to iValues   // clears the array

End_Procedure

The next example demonstrates how to resize the second dimension in a jagged array:

Procedure Test

    Integer[][] iJaggedRows  // declare a jagged array

    Integer iCount

 

    // create 10 items in row 0

    for iCount from 0 to 9

        Move iCount to iJaggedRows[0][iCount]

    loop

 

    // now resize row 0 to only contain 5 elements.

    Move (ResizeArray(iJaggedRows [0], 5)) to iJaggedRows [0]

End_Procedure

The important thing to notice in this example is how the return value of the ResizeArray function is moved to the first row of iJaggedRows i.e. iJaggedRows[0].

Example

This sample a dynamic array of integers and assigns the value 3 to the first 5 array elements. When ResizeArray is called, it resizes the array to 10 elements and assigns the value 7 to the 5 newly created array elements.

// fires when the button is clicked

Procedure OnClick

    Integer[] iValues

    Integer i iArraySize

    

    For i From 0 to 4

        move 3 to iValues[i]

    Loop

    

    Move (ResizeArray(iValues, 10, 7)) to iValues

 

    // display array values

    Move (SizeOfArray(iValues)) to iArraySize

    For i From 0 to (iArraySize-1)

        showln iValues[i]

    Loop

End_Procedure

Notes