0
List<String> s1;
List<String> s2;

我想将 n 项从 s1 移动到 s2

say s1={"a","b","c"}
s2={"d","e","f"}

将 2 个元素从 s1 移动到 s2 将使

s1={"c"}
s2={"d","e","f","a","b"}

实现这一目标的好方法是什么?

4

2 回答 2

5
var s1 = new List<string>() { "a", "b", "c" };
var s2 = new List<string>() { "d", "e", "f" };
s2.AddRange(s1.Take(2)); 
s1.RemoveRange(0, 2);    
于 2012-10-09T17:10:35.800 回答
1

使用要移动的项目的索引(在本例中为 0):

string item = s1[0];
s1.Remove(item);
s2.Add(item);
于 2012-10-09T17:09:42.773 回答