Skip to content

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:

  • {ArrayId} is the id of the array being resized.
  • {iArraySize} is the desired number of array elements.
  • {assignVal} is the value that will be assigned to each newly created array element if iArraySize is larger than the current member count.

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

Example 1

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

Example 2

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 3

This sample creates 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

  • Static arrays cannot be resized.
  • Only the first (left-most) dimension of a dynamic rectangular array can be resized, whereas any dimension of a jagged array can be resized.
  • It is not necessary to use ResizeArray to create new elements of a dynamic array, since DataFlex will create the elements as needed. However, if you are creating a large array, it can be more efficient to create array elements in blocks since adding new members may require the entire array to be moved in memory.
  • Attempting to reference an array element that is beyond the current size of the array will result in error 4509 "Reference Array Index Out Of Bounds".