简体   繁体   中英

How to add certain number of strings from one list to another in c#

What I am trying to do is make two lists containing some number of students each. I want to ask the user how many students they want to send from group1 to group2. After the user's input, the program should remove that amount of students from the first list(group1) and add them to the second(group2).

Here is what I have written so far. my problem is I don't know how to add a certain number of strings from one list to another. till now I only did add all elements from one group to another but not a certain number of them.

 class Program
    {
        static void Main(string[] args)
        {
            var groupOne = new List<string>() {"student_G1_01", "student_G1_02", "student_G1_03", "student_G1_04", "student_G1_05" };
            var groupTwo = new List<string>() { "student_G2_01", "student_G2_02", "student_G2_03", "student_G2_04", "student_G2_05", "student_G2_06" };

          
            Console.WriteLine("Student of group 1:");
            foreach (var strudent in groupOne)
            {

                Console.WriteLine(strudent);
            }

            Console.WriteLine("\n total number of students:" + groupOne.Count);



            Console.WriteLine("\n -------------- \n");



            Console.WriteLine("Students of group 2:");
            foreach(var student in groupTwo)
            {
                Console.WriteLine(student);
            }

            Console.WriteLine("\n total number of students:" + groupTwo.Count);



            Console.WriteLine("\n -------------- \n");


            //adding all the students from group2 to group1 

            groupOne.AddRange(groupTwo);


            foreach (var strudent in groupOne)
            {

                Console.WriteLine(strudent);
            }

            Console.WriteLine("\n total number of students:" + groupOne.Count);

        }

try this, it was tested

var groupOne = new List<string>() { "student_G1_01", "student_G1_02", "student_G1_03", "student_G1_04", "student_G1_05" };
var groupTwo = new List<string>() { "student_G2_01", "student_G2_02", "student_G2_03", "student_G2_04", "student_G2_05", "student_G2_06" };

    var userInput = 3; // user input
    var take= groupOne.Take(userInput);
    groupTwo.AddRange(take);
   groupOne.RemoveRange(0,userInput) ;
    
     Console.WriteLine("------Group 1-------");
    foreach (var group in groupOne)
    {
        Console.WriteLine(group);
    }
   Console.WriteLine("------Group 2-------");
    foreach (var group in groupTwo)
    {
        Console.WriteLine(group);
    }

test result

------Group 1-------
student_G1_04
student_G1_05
------Group 2-------
student_G2_01
student_G2_02
student_G2_03
student_G2_04
student_G2_05
student_G2_06
student_G1_01
student_G1_02
student_G1_03

Try this,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace HelloWorld
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var groupOne = new List<string>() {"student_G1_01", "student_G1_02", "student_G1_03", "student_G1_04", "student_G1_05" };
            var groupTwo = new List<string>() { "student_G2_01", "student_G2_02", "student_G2_03", "student_G2_04", "student_G2_05", "student_G2_06" };
        
            var userInput = 3; // user input
            groupTwo.AddRange(groupOne.Take(userInput));
          
            foreach(var group in groupTwo){
              Console.WriteLine(group);  
            }             
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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