有用的通用数组函数
用于追加、调整大小、删除数组元素的有用的泛型数组函数
引言
标准的 .NET Framework 提供了许多用于操作数组的泛型函数,但是,在某些功能方面,需要额外的代码,例如追加到数组、从数组中删除元素或删除满足条件的所有元素。
背景
您应该具备泛型类、列表、数组等的一些基本知识。
Array
对象包含一些有用的泛型功能,例如
ConvertAll<T,O>
Exists<T>
Find<T>
Resize<T>
TrueForAll<T>
但是,这些功能本身有时并不能完成工作。 这就是为什么我稍微扩展了这些功能。
Using the Code
基本上,代码非常简单,可以完成工作。
class ArrayEx {
/// <summary>
/// Appends a list of elements to the end of an array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="items"></param>
public static void Append<T>(ref T[] array, params T[] items)
{
int oldLength = array.Length;
//make room for new items
Array.Resize<T>(ref array, oldLength + items.Length);
for(int i=0;i<items.Length;i++)
array[oldLength + i] = items[i];
}
/// <summary>
/// Remove an Array at a specific Location
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="index">index to remove at</param>
/// <param name="list">The Array Object</param>
public static void RemoveAt<T>(int index, ref T[] list)
{
//pre:
if (index < 0 || list == null | list.Length == 0) return;
//move everything from the index on to the left one then remove last empty
if (list.Length > index + 1)
for (int i = index + 1; i < list.Length; i++)
list[i - 1] = list[i];
Array.Resize<T>(ref list, list.Length - 1);
}
///
///<summary> Thanks to homer.
/// Remove all elements in an array satisfying a predicate
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">The Array Object</param>
/// <param name="condition">A Predicate when the element shall get removed under.
/// </param>
/// <returns>Number of elements removed</returns>
public static int RemoveAll<T>(ref T[] list, Predicate<T> condition)
{
if (null == condition || null == list || 0 == list.Length) return 0;
int length = list.Length;
int destinationIndex = 0;
T[] destinationArray = new T[length];
for (int i = 0; i < list.Length; i++)
{
if (!condition(list[i]))
{
destinationArray[destinationIndex] = list[i];
destinationIndex++;
}
}
if (destinationIndex != length)
{
Array.Resize<T>(ref destinationArray, destinationIndex);
list = destinationArray;
}
return length - destinationIndex;
}
}
首先我们有 Append<T>
public static void Append<T>(ref T[] array, params T[] items)
此函数获取任何数组
,并在数组
的末尾追加更多元素,使数组
的大小等于元素的数量。
请记住,您不希望一遍又一遍地这样做,因为每次追加都会影响性能。 但是,如果正确使用,这意味着要追加的项目仅在操作结束时发生,那么这将很好。
使用方法...
string[] strArray = new string[] { "a", "b", "c" };
string[] strArray2 = new string[] { "d", "e", "f" };
// appends d e f to the first array
ArrayEx.Append<string>(ref strArray, strArray2);
// appends one more element
ArrayEx.Append<string>(ref strArray, "g");
Console.Out.Write(string.Join(strArray));
然后我们有 RemoveAt<T>
public static void RemoveAt<T>(int index, ref T[] list)
此方法将在特定位置删除一个元素,如 IList.RemoveAt
,并返回删除的元素。
使用方法...
string[] strArray = new string[] { "a", "b", "c" };
// remove element b
string Element = ArrayEx.RemoveAt<string>(1,ref strArray);
然后我们有 RemoveAll<T>
public static int RemoveAll<T>(ref T[] list, Predicate<T> condition)
此方法将从满足条件谓词的Array
中删除所有元素。
该函数还返回删除的元素数量。 您可以更改该函数以创建所有删除的元素(如果您愿意)。 然后可以在递归搜索中使用它来减少搜索表面空间。
使用方法...
// create array from sentence
string[] strArray = "My Name is Yang Yu and I'm the best!".Split(' ');
int RemoveCount = Ordering.RemoveAll<string>(ref strArray, new Predicate<string>(
delegate(string element)
{
// remove all elements with ' in them
return element.Contains("'");
}
));
关注点
您可能还想创建一个 InsertAt<T>
或 Merge<T>
等。
此处提供的功能并不能替代包装类 List<T>
中的功能,该类包含大多数这些方法。 确定何时使用 Array
以及何时使用 List
取决于许多变量。 当心...
历史
- 2008 年 4 月 11 日 - 文章创建