我有一个列表:
List<string> letters = {"a", "b", "c", "d", "e", "f"};
还有另一个包含其他字符串的列表:
List<string> myList1 = { "f3", "g4", "h5" };
List<string> myList2 = { "z5", "w7", "q9" };
List<string> myList3 = { "k5", "n7" };
我想用条件中的letters
列表填充myLists:
每个列表总共可以包含5个元素,并且同一列表不能重复添加字母。
在上面的示例中,
myList1 = { "f3", "g4", "h5", "a", "b"};
myList2 = { "z5", "w7", "q9", "c", "d"};
myList3 = { "k5", "n7", "e", "f", "d" };
关于myList3
,d
是随机添加的(不要忘记我不想重复添加“ e”或“ f”)。
请注意,如果我遇到这种情况:
List<string> myList1 = { "f3", "g4", "h5", "t3", "u6" };
List<string> myList2 = { "z5", "w7", "q9", "k9" };
List<string> myList3 = { "k5", "n7", "d3", "n6" };
输出为:
myList1 = { "f3", "g4", "h5", "t3", "u6" };
myList2 = { "z5", "w7", "q9", "k9", "a" };
myList3 = { "k5", "n7", "d3", "n6", "b" };
如果有帮助,myList1在声明时比myList2
和myList3
具有更多/相等的元素。
我试图这样做,但是我有很多条件使其无法读取。
任何帮助表示赞赏。
答案 0 :(得分:0)
有很多方法可以解决这个问题,如果您能分享您的方法,我们会更好的帮助您。无论如何,以下是您可以选择的一种方法。
您可以编写一个名为Fill的扩展方法。
public static class Extension
{
public static IEnumerable<T> Circle<T>(this IEnumerable<T> list, int startIndex)
{
return list.Skip(startIndex).Concat(list.Take(startIndex));
}
public static void Fill<T>(this List<T> source, List<T> reference, int maxCount,ref int index)
{
if(source.Count() >= maxCount) return;
var difference = source.Count() - maxCount;
var newReferenceList = reference.Circle(index);
source.AddRange(newReferenceList.Where(x=>!source.Contains(x)).Take(maxCount- source.Count()));
index+=Math.Abs(difference);
if(index > maxCount) index = 0;
}
}
然后在您的客户中
int index = 0;
myList1.Fill(letters,5,ref index);
myList2.Fill(letters,5,ref index);
myList3.Fill(letters,5,ref index);
答案 1 :(得分:0)
我认为这就是您要寻找的东西
public MainWindow()
{
InitializeComponent();
letters = new List<string> { "a", "b", "c", "d", "e", "f" };
myList1 = new List<string> { "f3", "g4", "h5" };
myList2 = new List<string> { "z5", "w7", "q9" };
myList3 = new List<string> { "k5", "n7" };
FillLists(letters, new List<List<string>> { myList1, myList2, myList3 });
}
private void FillLists(List<string> listToFillWith, List<List<string>> allLists)
{
char lastItemInList = listToFillWith.Last()[0]; //We get the last item inside the listToFillWith list and with the [0] we convert this string to a char
foreach (List<string> list in allLists) //We loop through each list inside allLists
{
while(list.Count != 5) //While either list 1, 2 or 3 does not have 5 items
{
if (listToFillWith.Count > 0) //Make sure our listToFillWith still has items to fill our list
{
list.Add(listToFillWith[0]); //Add the first item from listToFillWith to our list
listToFillWith.Remove(listToFillWith[0]); //Remove the first item from listToFillWith so we don't add it again
}
else //If listToFillWith is empty
{
char nextLetter;
//Here we check if the last item in the listToFillWith is a Z or z
if (lastItemInList == 'z')
{
nextLetter = 'a';
}
else if (lastItemInList == 'Z')
{
nextLetter = 'A';
}
else //If the last item in the listToFillWith is not a Z or z we get the last letter and go to the next letter in the alphabet
{
nextLetter = (char)(((int)lastItemInList) + 1);
}
list.Add(nextLetter.ToString()); //Add the next letter in the alphabet to the list
}
}
}
}