unity3d 检查列表值是否< Int32>连续以及哪个索引是全部打印的[已关闭]

ryoqjall  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(89)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

4天前关闭。
Improve this question

bool secuence=true;
List<int> list= new List<int> { 1, 2, 3, 4, 6, 8, 9, 10 };

for (int i = 1; i < list.Count; i++)
{
    //check if current value is +1 greater than last value
    if (list[i] + 1 == list[i - 1] || list[i] - 1 == list[i - 1] && secuence == true)
    {   //continue counting if consecutive
        secuence = true;
       
        if (secuence)
        {
           Debug.Log(i + " seqqq");
        }
    }
    else
    {   //Stop Counting if not consecutive
        secuence = false;
    }
}

----解决了

var list = new List<int> { 1, 2, 3, 6, 4, 8, 9, 10 ,14};
    list.Sort();
    var findlist = new List<int> { };
    for (int i = 0; i <= list.Count-1; i++)
    {
        if (i + 1 < list.Count)
        {
            if (list[i] + 1 == list[i + 1])
            {
                //Debug.Log(list[i] + " seqqq");
                findlist.Add(list[i]);
            }
            else if ((list[i] + 1 != list[i + 1]) && (list[i] - 1 
 == list[i - 1]))
            {
                //Debug.Log(list[i] + " seqqq");
                findlist.Add(list[i]);
            }
        }
        else 
        {
            if (list[i] - 1 == list[i - 1])
            {
                //Debug.Log(list[i] + " seqqq");
                findlist.Add(list[i]);
            }
        }
    }
    var diffs = 
 list.Union(findlist).Except(list.Intersect(findlist));
    foreach (var item in diffs)
    {
        Debug.Log(item);
    }

找到连续编号后,将哪个编号删除主列表,如新建列表={6};例如,主列表是{ 1,6,3,4,6,5,9,11 };连续数是3 4 5 6,新列表是{1,6,9,11};
我通过使用2个单独的列表解决了这个问题。我可以单独使用连续的,我可以删除那些不在列表中的。

e37o9pze

e37o9pze1#

如果要了解序列中的所有数字是否都递增:

using System.Linq;

var list = new List<int> { 1, 2, 3, 6, 4, 8, 9, 10 };
var diffs = list.Select((item, index) =>
{
    if (index == 0)
    {
        return item;
    }
    else
    {
        return item - list[index - 1];
    }
});

var isIncremental = diffs.All((n) => n >= 0);

Console.WriteLine(string.Join(", ", diffs.Select(n => n.ToString())));
Console.WriteLine("Is incremental: {0}", isIncremental);

或者用一种速记方式:

using System.Linq;

var list = new List<int> { 1, 2, 3, 6, 4, 8, 9, 10 };
var diffs = list.Select((item, index) =>
    (index == 0) ? item : item - list[index - 1]
);

var isIncremental = diffs.All((n) => n >= 0);

Console.WriteLine(string.Join(", ", diffs.Select(n => n.ToString())));
Console.WriteLine("Is incremental: {0}", isIncremental);

相关问题