日期:2014-05-17 浏览次数:21105 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// 一个泛型应用示例
// 应用场景: 将一组已经排序的条形码按照"相邻编号连续"分组
public class MyGen<T> where T : IMyBar
{
private List<T> list = null;
public MyGen()
{
list = new List<T>();
}
public void Add(T t)
{
list.Add(t);
}
public void AddRange(List<T> otherList)
{
list.AddRange(otherList);
}
// 计算出分组列表
public List<Range<T>> GetRangeList()
{
List<Range<T>> resultList = new List<Range<T>>();
list.Sort((a, b) => String.Compare(a.Barcode, b.Barcode)); //按照条形码编号排序
for (int i = 0; i < list.Count; i++)
{
if (resultList.Count > 0)
{
Range<T> curRange = resultList.Last();
string nextBarcode = curRange.Start.GetEnd(curRange.Count + 1);
if (list[i].Barcode.Equals(nextBarcode))
{
curRange.Add(list[i]);
}
else
{
resultList.Add(new Range<T>(list[i]));
}
}
else
{
resultList.Add(new Range<T>(list[i]));
}
}
&