繁体   English   中英

C#混合两个列表

[英]C# mixing two lists

在C#中,我想混合两个列表(将一个列表添加到另一个列表),但是它需要取决于列表中的项目数。 列表中的项目数不能超过5。 如果两个列表都包含3个项目,则当我将第二个列表添加到第一个列表时,第一个列表只能再包含2个项目(因为它变成5个),而其他1个项目将保留在第二个列表中。

有没有简单的方法可以做到这一点?

提前致谢,

EA

AddRange()Take() AddRange()组合是解决此问题的一种方法。

List<int> list1 = new List<int>() { 1, 2, 3 };
List<int> list2 = new List<int>() { 4, 5, 6 };

int maxItems = 5;
list1.AddRange(list2.Take(maxItems - list1.Count));

更新:刚刚注意到,如果list1 > maxItems ,则不需要特殊处理

采取():

如果count小于或等于零,则不枚举source,并且返回一个空的IEnumerable。

您可以使用Linq的Take来从列表的开头返回X个元素:

var list1 = new List<int> {1, 2, 3};
var list2 = new List<int> {4, 5, 6, 7, 8, 9, 10};
var result = list1.Take(5).ToList();
var missing = 5 - list1.Count;
result.AddRange(list2.Take(missing));
int addCount = 5 - list1.Count;
if(addCount > 0)
    list1.AddRange(list2.Take(addCount));

这是我的解决方案:

在课堂里:

class Potion 
public void MixIngredient(Potion toAddPotion)
        {
            if (MyIngredients.Count < 4)
            {
                for (int i = 0; i < toAddPotion.MyIngredients.Count; i++)
                {
                    if (MyIngredients.Count < 4)
                    {
                        Ingredients item = toAddPotion.MyIngredients[i];
                        MyIngredients.Add(item);
                        toAddPotion.MyIngredients.Remove(item);
                    }                    
                }  
            }          
        }

并在MainWindow中:

public void Slot1Button_Click(object sender, RoutedEventArgs e)
        {

            mixerSlot1 = new Potion("", "");

            if (selectedPotion.PotionNumber == slot2Label.Content)
            {
                MessageBox.Show("A potion can not be mixed with itself!", "Help Window", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            else
            {
                mixerSlot1.MyIngredients = selectedPotion.MyIngredients;
                slot1Label.Content = selectedPotion.PotionNumber;
            }
        }

        public void Slot2Button_Click(object sender, RoutedEventArgs e)
        {

            mixerSlot2 = new Potion("", "");

            if (selectedPotion.PotionNumber == slot1Label.Content)
            {
                MessageBox.Show("A potion can not be mixed with itself!", "Help Window", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            else
            {
                mixerSlot2.MyIngredients = selectedPotion.MyIngredients;
                slot2Label.Content = selectedPotion.PotionNumber;
            }
        }

        public void MixButton_Click(object sender, RoutedEventArgs e)
        {
            if (mixerSlot1 == null || mixerSlot2 == null)
            {
                if (mixerSlot1 == null)
                {
                    MessageBox.Show("Please put a potion to slot 1.", "Help Window", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else if (mixerSlot2== null)
                {
                    MessageBox.Show("Please put a potion to slot 2.", "Help Window", MessageBoxButton.OK, MessageBoxImage.Information);
                }                
            }
            else
        {
            mixerSlot1.MixIngredient(mixerSlot2);
            MessageBox.Show("Selected potions mixed!", "Help Window", MessageBoxButton.OK, MessageBoxImage.Information);
            slot1Label.Content = "...";
            slot2Label.Content = "...";
            RefreshIngredientsList();
        }

您可以使用for循环。 循环浏览要从后面获取的列表,并随手删除。 一旦达到5的极限,就跳出循环。

   var list1 = new List<int>() {1, 2, 3};
   var list2 = new List<int>() {4, 5, 6};

    for (int i = list1.Count - 1; i >= 0; i--)
    {
        list2.Add(list1[i]);
        list1.Remove(list1[i]);
        if (list2.Count == 5)
        {
            break;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM