CObArray::InsertAt

Syntax

void InsertAt( int nIndex, CObject* newElement, int nCount = 1 )
throw( CMemoryException );

void InsertAt( int nStartIndex, CObArray* pNewArray )
throw( CMemoryException );

Parameters

nIndex

An integer index that may be greater than GetUpperBound().

newElement

The CObject pointer to be placed in this array. A newElement of value NULL is allowed.

nCount

The number of times this element should be inserted (defaults to 1).

nStartIndex

An integer index that may be greater than GetUpperBound().

pNewArray

Another array that contains elements to be added to this array.

Remarks

The first version of InsertAt inserts one element (or multiple copies of an element) at a specified index in an array. In the process, it shifts up (by incrementing the index) the existing element at this index, and it shifts up all the elements above it.

The second version inserts all the elements from another CObArray collection, starting at the nStartIndex position.

The SetAt function, in contrast, replaces one specified array element and does not shift any elements.

Example

CObArray array;

array.Add( new CAge( 21 ) ); // Element 0

array.Add( new CAge( 40 ) ); // Element 1 (will become 2)

array.InsertAt( 1, new CAge( 30 ) ); // New element 1

#ifdef _DEBUG

afxDump.SetDepth( 1 );

afxDump << "InsertAt example: " << &array << "\\n";

#endif

The results from this program are as follows:

InsertAt example: A CObArray with 3 elements

[0] = a CAge at $45C8 21

[1] = a CAge at $4646 30

[2] = a CAge at $4606 40

See Also

CObArray::SetAt, CObArray::RemoveAt