C#混合两个列表

时间:2018-12-06 08:58:34

标签: c# list

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

有一种简单的方法吗?

预先感谢

EA

5 个答案:

答案 0 :(得分:1)

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

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

不需要特殊处理
  

Take():

     

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

答案 1 :(得分:1)

int addCount = 5 - list1.Count;
if(addCount > 0)
    list1.AddRange(list2.Take(addCount));

答案 2 :(得分:0)

您可以使用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));

答案 3 :(得分:0)

您可以使用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;
        }
    }

答案 4 :(得分:0)

这是我的解决方法:

在班上:

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();
        }
相关问题