110,533
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
List<Point> points = new List<Point>();
points.Add(new Point() { Name = "j1", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j2", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j3", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j4", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j1", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j5", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j6", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j7", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j8", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j5", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j9", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j1", X = 1.0, Y = 1.0 });
points.Add(new Point() { Name = "j9", X = 1.0, Y = 1.0 });
List<List<Point>> listP = new List<List<Point>>();
string name = "";
List<Point> arrP=null;
//遍历数组进行分组
foreach (Point p in points)
{
if (name == "")
{
name = p.Name;
arrP = new List<Point>();
listP.Add(arrP);
arrP.Add(p);
continue;
}
if (name != p.Name)
{
arrP.Add(p);
}
else
{
arrP.Add(p);
name = "";
}
}
//显示结果
foreach (List<Point> arr in listP)
{
Console.WriteLine("---------------------");
foreach (Point p in arr)
{
Console.Write(p.Name);
}
Console.WriteLine("");
}
Console.Read();
}
}
public class Point
{
public string Name { get; set; }
public double X { get; set; }
public double Y { get; set; }
}
static void Main(string[] args)
{
//申明测试数据
List<Tuple<string, int, int>> list = new List<Tuple<string, int, int>>(){
new Tuple<string, int, int>("j1", 198, 97),
new Tuple<string, int, int>("j3", 198, 97),
new Tuple<string, int, int>("j2", 198, 97),
new Tuple<string, int, int>("j4", 198, 97),
new Tuple<string, int, int>("j5", 198, 97)
};
//给数据排序
list = list.OrderBy(a => a.Item1).ToList();
foreach (var item in list)
{
Console.WriteLine(item.Item1);
}
Console.ReadKey();
}
List<Tuple<string, int, int>> list = new List<Tuple<string, int, int>>(){
new Tuple<string, int, int>("j1", 198, 97),
new Tuple<string, int, int>("j3", 198, 97),
new Tuple<string, int, int>("j2", 198, 97),
new Tuple<string, int, int>("j4", 198, 97),
new Tuple<string, int, int>("j5", 198, 97),
new Tuple<string, int, int>("j1", 198, 97),
new Tuple<string, int, int>("j6", 198, 97),
new Tuple<string, int, int>("j7", 198, 97),
new Tuple<string, int, int>("j8", 198, 97),
new Tuple<string, int, int>("j6", 198, 97)
};
int start = list.IndexOf(list.First(l => l.Item1 == "j1"));
int end = list.LastIndexOf(list.First(l => l.Item1 == "j1"));
for (int i = start; i <= end; i++)
Console.WriteLine(list[i]);
好像不怎么对吧。。。。 这样只生成了一个数组啊。。。 我写的数据至少会生成3个的。。。[/quote]