[英]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.