c#list< int>如何在两个值之间插入新值 [英] c# list<int> how to insert a new value in between two values

查看:137
本文介绍了c#list< int>如何在两个值之间插入新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个列表,其中我需要不断添加新值,但是当我这样做时,我需要增加它并将其插入两个值之间.

List<int> initializers = new List <int>();

initializers.Add(1);
initializers.Add(3);

所以初始化器将具有1、3个值.

然后,我将处理一组新的数字.初始化程序将需要具有这些值.

1、5、3、7

如果我处理另一组数字,它应该变成

1、9、5、13、3、11、7、15

我知道如何正确生成插入的新值,我只需要一些帮助将其插入到初始值设定项的现有值之间,而无需添加2或3个以上的循环来移动值的位置.

解决方案

List<int> initializers = new List <int>();

initializers.Add(1);
initializers.Add(3);

int index = initializers.IndexOf(3);
initializers.Insert(index, 2);

给您1,2,3.

so i have a list where i need to add new values constantly but when i do i need to increment it and insert it in between two values.

List<int> initializers = new List <int>();

initializers.Add(1);
initializers.Add(3);

so initializers would have 1, 3 values.

i would then process a new set of numbers. the initializers will need to have the values.

1, 5, 3, 7

and if i process another set of numbers it should become

1, 9, 5, 13, 3, 11, 7, 15

i know how to properly generate the new values inserted, i just need some help on inserting it in between the existing values of the initializers without having to add 2 or 3 more loops to move the values' positions.

解决方案

List<int> initializers = new List <int>();

initializers.Add(1);
initializers.Add(3);

int index = initializers.IndexOf(3);
initializers.Insert(index, 2);

Gives you 1,2,3.

这篇关于c#list&lt; int&gt;如何在两个值之间插入新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆