0

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

A Redfearn
  • 506
  • 5
  • 15
Erm
  • 31
  • 6
  • You could add everything to a master list and then chuck that list into lists of 5 items: https://stackoverflow.com/a/24087164/4610605 - **also please show what you have tried so far !** – Felix D. Dec 06 '18 at 08:59
  • 1
    Hi, we expect here questions related to programming problems. I can't see any problems in your question. What problem did you faced during implementation? – Renatas M. Dec 06 '18 at 09:02
  • _"and the rest 1 item will stay in the second list"_ means you want to remove added items from the source list? – Tim Schmelter Dec 06 '18 at 09:10

5 Answers5

1

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.

fubo
  • 44,811
  • 17
  • 103
  • 137
1

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));
Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
1
int addCount = 5 - list1.Count;
if(addCount > 0)
    list1.AddRange(list2.Take(addCount));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

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();
        }
Erm
  • 31
  • 6
0

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;
        }
    }
A Redfearn
  • 506
  • 5
  • 15