【问题标题】:How to add to a list the largest number of selected items in a list when there are two groups of selected items?当有两组选定项时,如何将列表中最大数量的选定项添加到列表中?
【发布时间】:2010-12-29 05:53:15
【问题描述】:

我有一个列表框,其中大约有 10 个项目。现在我在顶部选择一些项目(超过两个),在中间选择一些项目。我想创建一个列表,其中包含这两个不同选择中最多连续选择的项目。也就是说,如果顶部的少数选定项目大于中间的少数选定项目,那么顶部的少数项目应该进入列表。怎么做?我已经检查了连续性,如下所示:

 for (int i = 0; i < lb.SelectedIndices.Count - 1; i++)
{
    if (lb.SelectedIndices[i] < lb.SelectedIndices[i + 1] - 1)
        return false;
    return true;
}

【问题讨论】:

    标签: winforms c#-3.0


    【解决方案1】:

    只需跟踪当前子序列的长度,当您遇到乱序索引时,检查迄今为止看到的最长连续子序列。

    var indices = lb.SelectedIndices;
    int count = 0;
    int max = 0;
    int first = -1;
    for (int i = 0; i < indices.Count; i++)
    {
        ++count;
        if (i == indices.Count - 1 || indices[i]  + 1 != indices[i + 1])
        {
            if (count > max)
            {
                max = count;
                first = i - count + 1;
            }
            count = 0;
        }
    }
    // max is the longest consecutive subsequence
    // first is its starting index
    

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 2016-08-18
      • 2015-04-04
      相关资源
      最近更新 更多