Recipe 9.8 Inserting and Removing Items from an Array
Problem
You need the ability to insert
and remove items from a standard array
(System.Array). When an item is inserted, it
should not overwrite the item where it is being inserted; instead, it
should be inserted between the element at that index and the previous
index. When an item is removed, the void left by the element should
be closed by shifting around the other elements in the array.
However, the Array type has no usable method to
perform these operations.
Solution
If possible, switch to an ArrayList instead. If
this is not possible, use the approach shown in the following class.
Two methods insert and remove items from the array. The
InsertIntoArray method will insert an item into
the array without overwriting any data that already exists in the
array. The RemoveFromArray will remove an element
from the array:
using System;
public class ArrayUtilities
{
public void InsertIntoArray(Array target,
object value, int index)
{
if (index < target.GetLowerBound(0) ||
index > target.GetUpperBound(0))
{
throw (new ArgumentOutOfRangeException("index", index,
"Array index out of bounds."));
}
else
{
Array.Copy(target, index, target, index + 1,
target.Length - index - 1);
}
target.SetValue(value, index);
}
public void RemoveFromArray(Array target, int index)
{
if (index < target.GetLowerBound(0) ||
index > target.GetUpperBound(0))
{
throw (new ArgumentOutOfRangeException("index", index,
"Array index out of bounds."));
}
else if (index < target.GetUpperBound(0))
{
Array.Copy(target, index + 1, target, index,
target.Length - index - 1);
}
target.SetValue(null, target.GetUpperBound(0));
}
}
Discussion
The InsertIntoArray and
RemoveFromArray methods make use of the
Array.Copy static
method to perform their operations. Initially, both methods test to
see whether an item is being added or removed within the bounds of
the array target. If the item passes this test,
the Array.Copy method is used to shift items
around to either make room for an element to be inserted or to
overwrite an element being removed from the array.
The RemoveFromArray method accepts two parameters.
The first parameter, target, is the array
from which an element is to be removed; the second parameter,
index, is the zero-based position of the
element to be removed in the array. Elements at and above the
inserted element are shifted down by one. The last element in the
array is set to the default value for the array type.
The InsertIntoArray method accepts three
parameters. The first parameter, target,
is the array that is to have an element added,
value is the element to be added, and
index is the zero-based position at which
value is to be added. Elements at and
above the inserted element are shifted up by one. The last element in
the array is discarded.
The following code illustrates the use of the
InsertIntoArray and
RemoveFromArray methods:
class CTest
{
static void Main( )
{
ArrayUtilities arrlib = new ArrayUtilities ( );
string[] numbers = {"one", "two", "four", "five", "six"} ;
arrlib.InsertIntoArray(numbers, "three", 2);
foreach (string number in numbers)
{
Console.WriteLine(number);
}
Console.WriteLine( );
arrlib.RemoveFromArray(numbers, 2);
foreach (string number in numbers)
{
Console.WriteLine(number);
}
}
}
This code displays the following:
one
two
three
four
five
one
two
four
five
See Also
See the "Array Class" and
"ArrayList Class" topics in the
MSDN documentation.
|