目录
数组工具类基本操作
1.排序:升序,降序
2.查找
1)查找最值:最大值,最小值
2)查找满足条件的单个对象
3)查找满足条件的所有对象
4)选取数组中所有对象的某一字段
完整代码
测试
1.测试代码
2.测试结果
本篇文章来分享一下如何实现数组工具类。工具类,一般具有很强的通用性,复用性很强,在需要时就可以移植到项目,可以让我们的工作更加快捷方便。今天要分享的数组工具类实现了有关数组基本操作。
数组工具类基本操作
1.排序:升序,降序
可以参考 【一文读懂】C#如何实现通用的排序功能 进行理解
/// <summary>
/// 选择委托
/// 返回数据类型T的属性Tkey的值
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <returns>选择的属性</returns>
public delegate TKey SelectHandler<T, TKey>(T t);//注意是写在类外
/// <summary>
/// 使用委托对任何数据类型数组进行升序排序
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="selectHandler">选择委托的对象</param>
public static void OrderBy<T, Tkey>(T[] array, SelectHandler<T, Tkey> selectHandler)
where Tkey : IComparable<Tkey>
{
for (int i = 0; i < array.Length; ++i)
{
for (int j = i + 1; j < array.Length; ++j)
{
//将委托已经取得数据类型的属性的进行比较
if (selectHandler(array[i]).CompareTo(selectHandler(array[j])) > 0)
{
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
/// <summary>
/// 使用委托对任何数据类型数组进行降序排序
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="selectHandler">选择委托的对象</param>
public static void OrderByDescending<T, Tkey>(T[] array, SelectHandler<T, Tkey> selectHandler)
where Tkey : IComparable<Tkey>
{
for (int i = 0; i < array.Length; ++i)
{
for (int j = i + 1; j < array.Length; ++j)
{
//将委托已经取得数据类型的属性的进行比较
if (selectHandler(array[i]).CompareTo(selectHandler(array[j])) < 0)
{
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
2.查找
1)查找最值:最大值,最小值
/// <summary>
/// 查找最大值
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="selectHandler">选择委托的对象</param>
/// <returns>返回T类型的数组中字段Tkey最大的数据</returns>
public static T Max<T, Tkey>(T[] array, SelectHandler<T, Tkey> selectHandler)
where Tkey : IComparable<Tkey>
{
T temp = array[0];
for (int i = 1; i < array.Length; ++i)
{
//将委托已经取得数据类型的属性的进行比较
if (selectHandler(temp).CompareTo(selectHandler(array[i])) < 0)
{
temp = array[i];
}
}
return temp;
}
/// <summary>
/// 查找最小值
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="selectHandler">选择委托的对象</param>
/// <returns>返回T类型的数组中字段Tkey最大的数据</returns>
public static T Min<T, Tkey>(T[] array, SelectHandler<T, Tkey> selectHandler)
where Tkey : IComparable<Tkey>
{
T temp = array[0];
for (int i = 1; i < array.Length; ++i)
{
//将委托已经取得数据类型的属性的进行比较
if (selectHandler(temp).CompareTo(selectHandler(array[i])) > 0)
{
temp = array[i];
}
}
return temp;
}
2)查找满足条件的单个对象
/// <summary>
/// 查找条件委托 表示一个查找条件
/// 数据t是否满足某个条件
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="t">数据类型T的对象</param>
/// <returns>bool值</returns>
public delegate bool FindHandler<T>(T t);//注意是写在类外
/// <summary>
/// 查找单个(第一个)满足条件的对象
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="findHandler">查找条件委托</param>
/// <returns>返回第一个满足条件的类型为T的对象</returns>
public static T Find<T>(T[] array, FindHandler<T> findHandler)
{
T temp = default;
for (int i = 0; i < array.Length; ++i)
{
if (findHandler(array[i]))
{
return array[i];
}
}
return temp;
}
3)查找满足条件的所有对象
/// <summary>
/// 查找满足条件的所有对象
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="findHandler">查找条件委托</param>
/// <returns>返回 所有 满足条件的,类型为T的 对象</returns>
///
public static T[] FindAll<T>(T[] array, FindHandler<T> findHandler)
{
List<T> res = new List<T>();
for (int i = 0; i < array.Length; ++i)
{
if (findHandler(array[i]))
{
res.Add(array[i]);
}
}
return res.ToArray();
}
4)选取数组中所有对象的某一字段
/// <summary>
/// 选取数组中所有对象的某一字段
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="selectHandler">选择委托的对象</param>
/// <returns>返回数组中所有对象的某一字段组成的数组</returns>
public static Tkey[] Select<T, Tkey>(T[] array, SelectHandler<T, Tkey> selectHandler)
{
Tkey[] res = new Tkey[array.Length];
for (int i = 0; i < array.Length; ++i)
{
res[i] = selectHandler(array[i]);
}
return res;
}
完整代码
using System;
using System.Collections.Generic;
/// <summary>
/// 选择委托
/// 返回数据类型T的属性Tkey的值
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <returns>选择的属性</returns>
public delegate TKey SelectHandler<T, TKey>(T t);
/// <summary>
/// 查找条件委托 表示一个查找条件
/// 数据t是否满足某个条件
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="t">数据类型T的对象</param>
/// <returns>bool值</returns>
public delegate bool FindHandler<T>(T t);
/// <summary>
/// 数组助手类
/// 排序:升序 降序
/// 查找:最大 最小 单个 全部...
/// </summary>
public static class ArrayHelper
{
/// <summary>
/// 升序排序(冒泡排序)
/// 使用委托对任何数据类型数组进行排序
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="selectHandler">选择委托的对象</param>
public static void OrderBy<T, Tkey>(T[] array, SelectHandler<T, Tkey> selectHandler)
where Tkey : IComparable<Tkey>
{
for (int i = 0; i < array.Length; ++i)
{
for (int j = i + 1; j < array.Length; ++j)
{
//将委托已经取得数据类型的属性的进行比较
if (selectHandler(array[i]).CompareTo(selectHandler(array[j])) > 0)
{
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
/// <summary>
/// 降序排序(冒泡排序)
/// 使用委托对任何数据类型数组进行排序
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="selectHandler">选择委托的对象</param>
public static void OrderByDescending<T, Tkey>(T[] array, SelectHandler<T, Tkey> selectHandler)
where Tkey : IComparable<Tkey>
{
for (int i = 0; i < array.Length; ++i)
{
for (int j = i + 1; j < array.Length; ++j)
{
//将委托已经取得数据类型的属性的进行比较
if (selectHandler(array[i]).CompareTo(selectHandler(array[j])) < 0)
{
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
/// <summary>
/// 查找最大值
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="selectHandler">选择委托的对象</param>
/// <returns>返回T类型的数组中字段Tkey最大的数据</returns>
public static T Max<T, Tkey>(T[] array, SelectHandler<T, Tkey> selectHandler)
where Tkey : IComparable<Tkey>
{
T temp = array[0];
for (int i = 1; i < array.Length; ++i)
{
//将委托已经取得数据类型的属性的进行比较
if (selectHandler(temp).CompareTo(selectHandler(array[i])) < 0)
{
temp = array[i];
}
}
return temp;
}
/// <summary>
/// 查找最小值
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="selectHandler">选择委托的对象</param>
/// <returns>返回T类型的数组中字段Tkey最大的数据</returns>
public static T Min<T, Tkey>(T[] array, SelectHandler<T, Tkey> selectHandler)
where Tkey : IComparable<Tkey>
{
T temp = array[0];
for (int i = 1; i < array.Length; ++i)
{
//将委托已经取得数据类型的属性的进行比较
if (selectHandler(temp).CompareTo(selectHandler(array[i])) > 0)
{
temp = array[i];
}
}
return temp;
}
/// <summary>
/// 查找(单个)第一个满足条件的对象
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="findHandler">查找条件委托</param>
/// <returns>返回第一个满足条件的类型为T的对象</returns>
public static T Find<T>(T[] array, FindHandler<T> findHandler)
{
T temp = default;
for (int i = 0; i < array.Length; ++i)
{
if (findHandler(array[i]))
{
return array[i];
}
}
return temp;
}
/// <summary>
/// 查找所有满足条件的对象
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="findHandler">查找条件委托</param>
/// <returns>返回所有满足条件的类型为T的对象</returns>
///
public static T[] FindAll<T>(T[] array, FindHandler<T> findHandler)
{
List<T> res = new List<T>();
for (int i = 0; i < array.Length; ++i)
{
if (findHandler(array[i]))
{
res.Add(array[i]);
}
}
return res.ToArray();
}
/// <summary>
/// 选取数组中所有对象的某一字段
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <typeparam name="Tkey">数据类型T的字段</typeparam>
/// <param name="array">数据类型T的数组</param>
/// <param name="selectHandler">选择委托的对象</param>
/// <returns>返回数组中所有对象的某一字段组成的数组</returns>
public static Tkey[] Select<T, Tkey>(T[] array, SelectHandler<T, Tkey> selectHandler)
{
Tkey[] res = new Tkey[array.Length];
for (int i = 0; i < array.Length; ++i)
{
res[i] = selectHandler(array[i]);
}
return res;
}
}
测试
1.测试代码
排序功能测试可查看【一文读懂】C#如何实现通用的排序功能,如下是对数组工具类的查找功能进行测试
using System;
using UnityEngine;
namespace ArrayHelperTest
{
public class ArrayHelperTest : MonoBehaviour
{
public void Start()
{
int[] intArray = { 4, 1, 5, 0 };
string[] stringArray = { "2", "a", "ab", "hello", "0" };
Student[] studentArray =
{
new Student(){ Id=1001,Name="张三",Age=20 },
new Student(){ Id=1003,Name="李四",Age=18 },
new Student(){ Id=1002,Name="赵六",Age=21 },
new Student(){ Id=1000,Name="王五",Age=19 },
new Student(){ Id=1004,Name="孙七",Age=18 }
};
//查找
//1)查找最值:最大值,最小值
FindMaxAndMinTest<int, int>(intArray, value => value );
FindMaxAndMinTest<string, string>(stringArray, value => value);
FindMaxAndMinTest<Student, int>(studentArray, student => student.Id);
FindMaxAndMinTest<Student, string>(studentArray, student => student.Name);
//2)查找满足条件的单个对象(第一个)
ArrayHelper.Find<int>(intArray, value => value > 0);
ArrayHelper.Find<string>(stringArray, value => value == "a");
ArrayHelper.Find<Student>(studentArray, student => student.Age==18);
//3)查找满足条件的所有对象
Student[] students = ArrayHelper.FindAll<Student>(studentArray, student => student.Age == 18);
string studentStr = "";
foreach (var student in students)
{
studentStr += student.Id + " " + student.Name + " " + student.Age + "\n";
}
Debug.Log(studentStr);
//4)选取数组中所有对象的某一字段
string[] names= ArrayHelper.Select<Student,string>(studentArray, student => student.Name);
string nameStr = "";
foreach (var name in names)
{
nameStr += name+" ";
}
Debug.Log(nameStr);
}
private void FindMaxAndMinTest<T, Tkey>(T[] array, SelectHandler<T, Tkey> selectHandler) where Tkey : IComparable<Tkey>
{
Debug.Log(array.GetType() + "测试:\n"+
"最小值:" + ArrayHelper.Min<T, Tkey>(array, selectHandler)+
" 最大值:" + ArrayHelper.Max<T, Tkey>(array, selectHandler));
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
2.测试结果
好了,本次的分享到这里就结束啦,希望对你有所帮助~