简体   繁体   English

如何在 c# 中将一定数量的字符串从一个列表添加到另一个列表

[英]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.我想问用户他们想从 group1 发送多少学生到 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).用户输入后,程序应从第一个列表(group1)中删除该数量的学生,并将他们添加到第二个列表(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);  
            }             
        }
    }
}

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

相关问题 C#如何将一个对象从一个列表添加到另一个 - C# how to add an object from one list to another C#:如何从另一个类的另一个列表中添加某些元素并打印出来? - C#: How to add certain elements from another list in another class and print out? C#列表:如何将元素从一个列表复制到另一个列表,但仅复制某些属性 - C# Lists: How to copy elements from one list to another, but only certain properties 将元素从一个列表添加到另一个C# - Add elements from one list to another C# C#如何将字符串添加到列表,以便可以从DataSource访问它们 - c# how to add strings to a list so that they will be accessible from DataSource 如何从列表中检索特定字符串,特别是C#中以某个字母开头的字符串? - How can you retreive specific strings from a list, specifically strings that start with a certain letter in C#? 从C#中的字符串列表中提取数字的不同列表 - extract distinct list of number from a list of strings in C# c# - 如何从一个电子表格文档中取出一张工作表并将其添加到另一个电子表格中 - How to take a sheet from one SpreadsheetDocument and add it to another in c# C#如何将一个项目从另一个类添加到List - C# How to add an item to List from another class 如何在C#中有效地从另一个中减去一个巨大的列表 - How to subtract one huge list from another efficiently in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM