Erm
Erm

Reputation: 31

C# mixing two lists

In C#, I would like to mix two Lists (Add one List to another) but it needs to be dependent on the number of items in the list. The number of items in a list can not be more than 5. For example; If both lists have 3 items, when I add second list to the first one, first list can only take 2 more items (as it becomes 5), and the other 1 item will stay in the second list.

Is there a simple way to do this?

Thanks in advance,

EA

Upvotes: 0

Views: 409

Answers (5)

Erm
Erm

Reputation: 31

Here is my solution:

In the Class:

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);
                    }                    
                }  
            }          
        }

And in the 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();
        }

Upvotes: 1

fubo
fubo

Reputation: 45947

AddRange() combined with Take() is one way to solve this.

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));

Update: just noticed, there is no special treatment required if list1 > maxItems

Take():

If count is less than or equal to zero, source is not enumerated and an empty IEnumerable is returned.

Upvotes: 1

A Redfearn
A Redfearn

Reputation: 528

You could use a for loop. Looping through the list you want to take from backwards and removing as you go. Once you reach your limit of 5 break out of the loop.

   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;
        }
    }

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460098

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

Upvotes: 1

Owen Pauling
Owen Pauling

Reputation: 11841

You can make use of Linq's Take to return X elements from the start of the list:

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));

Upvotes: 1

Related Questions