/// <summary> /// 从指定的目标索引开始查找count个元素,找到符合条件的元素的索引 /// </summary> /// <param name="startindex"></param> /// <param name="count"></param> /// <param name="match"></param> /// <returns></returns> public int FindIndex(int startindex, int count, 内置委托.Predicate <T> match) { if (startindex > this.Count) { throw new Exception("开始索引不能大于集合中的元素的总的个数"); } if (count < 0 || count > this.Count - startindex) { throw new Exception("查找个数不能小于0个或者集合中的元素个数自查找的索引器没有了要查找元素的个数"); } if (match == null) { throw new Exception("条件不能为空"); } int index = startindex + count;//开始出的索引加上要查找几个等于查找的索引的区间 for (int i = 0; i < index; i++) { if (match(_array[i])) { return(i); } } return(-1); }
/// <summary> /// 查找所有符合条件的元素并返回一个IEnumerable类型 /// </summary> /// <param name="match"></param> /// <returns></returns> public System.Collections.Generic.IEnumerable <T> FindAll(内置委托.Predicate <T> match) { if (match == null) { throw new Exception("条件不能为空"); } for (int i = 0; i < this.Count; i++) { if (match(_array[i])) { yield return(_array[i]); } } }
/// <summary> /// 是否存在匹配指定条件的元素 /// </summary> /// <param name="match"></param> /// <returns></returns> public bool Exists(内置委托.Predicate <T> match) { if (match == null) { throw new Exception("判断条件不能为空"); } for (int i = 0; i < this.Count; i++) { if (match(_array[i])) { return(true); } } return(false); }
/// <summary> /// 查找符合条件的第一个匹配元素 /// </summary> /// <param name="match"></param> /// <returns></returns> public T Find(内置委托.Predicate <T> match) { if (match == null) { throw new Exception("筛选条件不能为空"); } for (int i = 0; i < this.Count; i++) { if (match(_array[i])) { return(_array[i]); } } return(default(T)); }
/// <summary> /// 从指定的位置开始符合条件的第一个元素的位置的索引 /// </summary> /// <param name="startindex"></param> /// <param name="match"></param> /// <returns></returns> public int FindIndex(int startindex, 内置委托.Predicate <T> match) { return(this.FindIndex(startindex, this.Count - startindex, match)); }
/// <summary> /// 从集合中查找符合条件的第一个元素的索引 /// </summary> /// <param name="match"></param> /// <returns></returns> public int FindIndex(内置委托.Predicate <T> match) { return(this.FindIndex(0, this.Count, match)); }