加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将元素均匀添加到List

发布时间:2020-12-15 21:59:25 所属栏目:百科 来源:网络整理
导读:我有一个带有“Index”属性的“Element”类,用于对元素列表进行排序. 当我在列表中添加元素时,我想根据列表中的现有元素均匀地展开新元素. 这意味着如果我有6个元素并想要添加3个元素,结果应该如下图所示: 到目前为止,我对我的代码(见下文)的问题是它使用了
我有一个带有“Index”属性的“Element”类,用于对元素列表进行排序.
当我在列表中添加元素时,我想根据列表中的现有元素均匀地展开新元素.

这意味着如果我有6个元素并想要添加3个元素,结果应该如下图所示:

到目前为止,我对我的代码(见下文)的问题是它使用了错误的索引,所以如果我有2个现有元素并添加9个元素,那么最后一个元素索引是18,我真的不明白.

public List<Element> AddElements()
{   
    // The elements are inserted before this
    List<Element> existingElements = new List<Element>();
    List<Element> elementsToAdd = new List<Element>();

    int elementsLeft = 1;

    foreach (Element element in elementsToAdd)
    {
        // Generate the next index
        int nextIndex = 1;

        // Only proceed if any elements exists
        if (existingElements.Count > 0)
        {
            // divisonResult = 12 / 4 = 3
            double divisonResult = Math.Floor(Convert.ToDouble(existingElements.Count) / Convert.ToDouble(elementsToAdd.Count));

            // modulusResult = 12 % 2 = 0
            double modulusResult = Convert.ToDouble(existingElements.Count) % Convert.ToDouble(elementsToAdd.Count);

            // NextPosition = (3 + 1) * 1 = 4
            // NextPosition = (3 + 1) * 2 = 8
            // NextPosition = (3 + 1) * 3 = 12
            // NextPosition = (3 + 1) * 4 = 16
            if (modulusResult <= 0 && elementsToAdd.Count > 1)
                nextIndex = Convert.ToInt16(divisonResult) * elementsLeft;
            else
                nextIndex = (Convert.ToInt16(divisonResult) + 1) * elementsLeft;

            elementsLeft++;

            // Move existing elements
            var elementsToBeMoved = existingElements.Where(elementQuery => elementQuery.Index >= nextIndex);

            foreach (Element elementToBeMoved in elementsToBeMoved)
            {
                elementToBeMoved.Index++;
            }
        }

        // Add element to existing elements
        existingElements.Add(new Element { Index = nextIndex });
    }

    // Return new list
    return existingElements;
}

解决方法

将原始元素数除以要混合的列表. 6/3 1 = 3(每3个项目将来自list2).运行一个循环(var i = 0; i< list1.Count list2.Count; i)在每个循环中检查新列表的位置是否在你应该从list2插入项目的位置,否则插入下一个项目来自list1.这是一种扩展方法......

class Program
{
    static void Main(string[] args)
    {
        var existingElements  = new List<int> { 1,2,3,4,5,6 };
        var elementsToAdd = new List<int> { 100,101,102 };
        existingElements = existingElements.Mix(elementsToAdd).ToList();
        Console.WriteLine(String.Join(",",existingElements));
        Console.ReadKey();
    }
}

public static class ExtensionMethods
{
    public static IEnumerable<T> Mix<T>(this IEnumerable<T> source,IEnumerable<T> mix)
    {
        var list1 = source.ToArray();
        var list2 = mix.ToArray();
        var total = list1.Count() + list2.Count();
        var skip = (list1.Count() / list2.Count()) + 1;
        var count1 = 0;
        var count2 = 0;
        var finalList = new List<T>();

        for (var i = 0; i < total; i++)
        {
            var count = i + 1;
            if (count % skip == 0)
            {
                finalList.Add(list2[count2]);
                count2++;
            }
            else
            {
                finalList.Add(list1[count1]);
                count1++;
            }
        }

        return finalList;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读