在第 n 个项目之后轻松更改列表

发布于 2024-12-04 06:53:33 字数 341 浏览 0 评论 0 原文

我得到了一个清单: var x = new List(){"a","b","c"}

我正在寻找一种非常简单的方法来更改 a 之后的所有项目 例如:

var x = new List<string>(){"a","b","c"}
var y = new List<string>(){"d","e","f"}
x.addAfterFirst(y);

result x= "a","d","e","f"

我知道'x.Skip(1)'可以返回信息。我需要设置它。

I got a list:
var x = new List<string>(){"a","b","c"}

I am looking for an pretty easy way to change all items after a
example:

var x = new List<string>(){"a","b","c"}
var y = new List<string>(){"d","e","f"}
x.addAfterFirst(y);

result x= "a","d","e","f"

I know that' x.Skip(1)' can return me the info. I need to set it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮倦 2024-12-11 06:53:33

您可以使用采用扩展方法来采用第一个n 来自 x 的项目,并使用 Concat扩展方法

List<string> x = new List<string> { "a", "b", "c" };
List<string> y = new List<string> { "d", "e", "f" };

int n = 1;

List<string> result = x.Take(n).Concat(y).ToList();
// result == { "a", "d", "e", "f" }

如果你想就地修改x而不是创建新列表,可以使用RemoveRange 方法 用于删除前 n 个项目之后的所有项目,以及 AddRange 方法y 附加到 x

List<string> x = new List<string> { "a", "b", "c" };
List<string> y = new List<string> { "d", "e", "f" };

int n = 1;

x.RemoveRange(n, x.Count - n);
x.AddRange(y);
// x == { "a", "d", "e", "f" }

You can use the Take Extension Method to take the first n items from x and concat them with y using the Concat Extension Method:

List<string> x = new List<string> { "a", "b", "c" };
List<string> y = new List<string> { "d", "e", "f" };

int n = 1;

List<string> result = x.Take(n).Concat(y).ToList();
// result == { "a", "d", "e", "f" }

If you want to modify x in-place instead of creating a new list, you can use the RemoveRange Method to remove all items after the first n items, and the AddRange Method to append y to x:

List<string> x = new List<string> { "a", "b", "c" };
List<string> y = new List<string> { "d", "e", "f" };

int n = 1;

x.RemoveRange(n, x.Count - n);
x.AddRange(y);
// x == { "a", "d", "e", "f" }
剪不断理还乱 2024-12-11 06:53:33

请使用 InsertRange

var x = new list<string>(){"a","b","c"}
var y = new list<string>(){"d","e","f"}
x.InsertRange(2,y);

立即执行编辑任务

如果您想删除元素,

var x = new list<string>(){"a","b","c"};
int xlength = x.Count() - 1;
var y = new list<string>(){"d","e","f"};
int ylength = y.Count() - 1;
x.InsertRange(2,y);
x.RemoveRang( 2 + ylength, xlength- 2);

Make use of InsertRange will do you task

var x = new list<string>(){"a","b","c"}
var y = new list<string>(){"d","e","f"}
x.InsertRange(2,y);

Edit

now if you want to remove element

var x = new list<string>(){"a","b","c"};
int xlength = x.Count() - 1;
var y = new list<string>(){"d","e","f"};
int ylength = y.Count() - 1;
x.InsertRange(2,y);
x.RemoveRang( 2 + ylength, xlength- 2);
陌路终见情 2024-12-11 06:53:33

您的结果与完整描述不符

您是否希望插入或替换

您是否需要修改现有集合或者您可以接受新集合吗?

所有示例均使用以下初始化

var insertIndex=1;
var x = new List<string>(){"a","b","c"};
var y = new List<string>(){"d","e","f"}; 

新集合 替换

var result=x.Take(insertIndex).Concat(y).ToList();

新集合 插入

var result=x.Take(insertIndex).Concat(y).Concat(x.Skip(insertIndex)).ToList();

修改集合 替换

x.RemoveRange(insertIndex,x.Count-insertIndex);
x.AddRange(y);

修改集合 插入

x.InsertRange(insertIndex,y);

Your result doesn't match the full description

Are you wishing to insert or replace

Are you needing to modify the existing collection or could you accept a new collection?

All examples use the following initialization

var insertIndex=1;
var x = new List<string>(){"a","b","c"};
var y = new List<string>(){"d","e","f"}; 

New Collection Replace

var result=x.Take(insertIndex).Concat(y).ToList();

New Collection Insert

var result=x.Take(insertIndex).Concat(y).Concat(x.Skip(insertIndex)).ToList();

Modify Collection Replace

x.RemoveRange(insertIndex,x.Count-insertIndex);
x.AddRange(y);

Modify Collection Insert

x.InsertRange(insertIndex,y);
半城柳色半声笛 2024-12-11 06:53:33

非LINQ扩展方式:

int i;
for (i=0;i < y.Count;i++) 
{
     if (i+1 < x.Count)
         x[i+1] = y[i];
     else
         x.Add(y[i]); 
 }
 //If you dont want trailing elements to remain in x
 for (;i < x.Count;i++)
     x.RemoveAt(i);

Non-LINQ extention way:

int i;
for (i=0;i < y.Count;i++) 
{
     if (i+1 < x.Count)
         x[i+1] = y[i];
     else
         x.Add(y[i]); 
 }
 //If you dont want trailing elements to remain in x
 for (;i < x.Count;i++)
     x.RemoveAt(i);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文