Vectors as Resizeable Arrays

When Visual Basic version 4 was released, many programmers saw the new Collection class as an improved version of arrays. They started automatically replacing their arrays with Collections. Then they discovered the high performance cost and started changing Collections back to arrays.

This is the wrong way to look at it. Collections don’t replace arrays; both are useful data structures with advantages and disadvantages. Choose the one that best fits the need.

The advantage of arrays is their fixed size and structure. If you put an element in a certain position in an array, you can be sure that it will be there when you come back. You can access an element easily and efficiently as long as you re­member its location.

The disadvantage of arrays is their fixed size and structure. If you don’t know how many items you have, you don’t know how big to make the array. If you remove or add items, you have to jump through hoops to keep track of where everything is.

Often, Collections are overkill if a variable number of elements is all you need to handle. If string indexing and other Collection features don’t do anything for you, perhaps you need a simpler, faster data structure known as a resizeable array, or vector.