用于存储分组的类
public class info { public int group { get; set; } public List<int> list { get; set; } }
程序主代码
[STAThread] static void Main() { #region 分组 #endregion int[] arr = new int[] { 0, 1, 2, 5, 6, 8, 9 }; List<info> infos = new List<info>(); var group = 0; var number = arr[0]; infos.Add(new info() { group = group, list = new List<int>() { number } }); for (int i = 1; i < arr.Length; i++) { if (arr[i] == number + 1) { var res = infos.Where(o => o.group.Equals(group)).FirstOrDefault(); res.list.Add(arr[i]); number = arr[i]; } else { group++; number = arr[i]; infos.Add(new info() { group = group, list = new List<int>() { number } }); } } Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(infos)); }
标签:arr,group,数字,List,number,分组,连续,new,infos From: https://www.cnblogs.com/wwzorz/p/17853002.html