我有一个带有“Index”属性的“Element”类,用于对元素列表进行排序。 当我在列表中添加元素时,我想根据列表中的现有元素均匀地展开新元素。
这意味着如果我有6个元素并且想要添加3个元素,结果应该如下图所示:
到目前为止我的代码问题(见下文)是使用了错误的索引,所以如果我有2个现有元素并添加9个元素,那么最后一个元素索引是18,我真的不明白。< / p>
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;
}
答案 0 :(得分:1)
将原始元素数除以要混合的列表。 6/3 + 1 = 3(每3个项目将来自list2)。运行一个循环(var i = 0; i&lt; 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;
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
我这样开发:
public static class IEnumerableExtender
{
public static IEnumerable<T> Mix<T>(this IEnumerable<T> first, IEnumerable<T> second)
{
var firstCount = first.Count();
var secondCount = second.Count();
// it is important that `first` is equal or larger
// than `second`, if it is not, we swap
if (firstCount < secondCount)
{
var a = first;
first = second;
second = a;
firstCount = first.Count();
secondCount = second.Count();
}
// at every `N` number of elements we will insert
// one from the `second` list
var insertAtEvery = Math.Floor(firstCount / (double)secondCount) + 1;
int totalLength = firstCount + secondCount;
var listResult = new List<T>(totalLength);
for (int i = 0, x = 0, y = 0; i < totalLength; ++i)
{
// if it is time to insert an element from `second`
// and there is still something to be inserted
if ((i % insertAtEvery) == 0 && y < secondCount)
{
// insert and move the index from the `second`
listResult.Add(second.ElementAt(y++));
}
else
{
// insert and move the index from the `first`
listResult.Add(first.ElementAt(x++));
}
}
return listResult;
}
}
这里只是一个测试输出的控制台代码:
public class Program
{
static void Main(string[] args)
{
int size1, size2;
while (true)
{
Console.Write("Size of list #1: ");
int.TryParse(Console.ReadLine(), out size1);
if (size1 <= 0)
break;
Console.Write("Size of list #2: ");
int.TryParse(Console.ReadLine(), out size2);
if (size2 <= 0)
break;
var list1 = Enumerable.Range(0, size1).Select(o => '.').ToList();
var list2 = Enumerable.Range(0, size2).Select(o => '#').ToList();
var result = list1.Mix(list2);
Console.WriteLine(String.Join(" ", result));
Console.WriteLine();
}
}
}
示例输出