Recipe 8.4. Inserting into an Array


Problem

You need to insert a new value at an arbitrary location in the middle of an array.

Solution

Sample code folder: Chapter 08\ArrayInsertion

Unlike some of the collection classes in .NET, arrays do not include a method that lets you insert an element in the middle of an array. Instead, you have to create a new array and copy the elements of the original array into it, reserving space for the new element. The code in this recipe implements such a method.

Discussion

Because arrays can be created using any data type, we will require a generic method capable of handling any data:

 Public Sub InsertArrayElement(Of T) ( _       ByRef sourceArray( ) As T, _       ByVal insertIndex As Integer, _       ByVal newValue As T)    ' ----- Insert a value in the middle of an array.    Dim newPosition As Integer    Dim counter As Integer    ' ----- Get a valid positon, checking for boundaries.    newPosition = insertIndex    If (newPosition < 0) Then newPosition = 0    If (newPosition > sourceArray.Length) Then _       newPosition = sourceArray.Length    ' ----- Make room in the array.    Array.Resize(sourceArray, sourceArray.Length + 1)    ' ----- Move the after-index items.    For counter = sourceArray.Length - 2 To newPosition Step -1       sourceArray(counter + 1) = sourceArray(counter)    Next counter    ' ----- Store the new element.    sourceArray(newPosition) = newValue End Sub 

The code stretches the initial array, making it one position larger. It then shifts some of the elements one position higher to make room for the new element. Finally, it saves the new element at the desired position.

To use this method, pass it an array of any type, and also indicate the type used for the generic parameter.

You can insert the new value at position 0, just before the very first element, or at a position one greater than the maximum current index of the array. Insert positions outside this range adjust themselves to fit the valid range.

The following example demonstrates calling the InsertArrayElement() method by first creating a string array of fruit names and then inserting an element in the middle:

 Dim result As New System.Text.StringBuilder Dim arrayInsert( ) As String = { _    "Oranges", "Apples", "Grapes", "Bananas", "Blueberries"} ' ----- Show the contents before insertion. result.AppendLine("Before insertion:") For Each fruit As String In arrayInsert    result.AppendLine(fruit) Next fruit ' ----- Insert more fruit. InsertArrayElement(Of String)(arrayInsert, 2, "Lemons") ' ----- Show the contents after insertion. result.AppendLine( ) result.AppendLine("After insertion:") For Each fruit As String In arrayInsert    result.AppendLine(fruit) Next fruit MsgBox(result.ToString( )) 

The string "Lemons" is inserted at position 2 (counting from zero) in the array. The results are shown in Figure 8-4.

Figure 8-4. Inserting values into an array


See Also

Recipe 8.7 also discusses adding elements to an array.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net