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);
}
CodePudding user response:
You can do it via while loop and write something like this:
var count = 3; // example of your user input
var i = 0;
while (i < count)
{
var item = groupOne[0]; // peek the first item
groupOne.Remove(item); // remove it from the first list
groupTwo.Add(item); // add it to the other list
}
CodePudding user response:
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);
}
}
}
}
CodePudding user response:
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);
groupOne.RemoveRange(0,userInput) ;
groupTwo.AddRange(take);
Console.WriteLine("------Group 1-------");
foreach (var group in groupOne)
{
Console.WriteLine(group);
}
Console.WriteLine("------Group 2-------");
foreach (var group in groupTwo)
{
Console.WriteLine(group);
}
output
------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