如果没有这样的号码,如何在列表中间插入一些号码?
在下面的例子中,我试图插入数字4
List<int> list1 = new List<int>(){ 0, 1, 2, 3, 5, 6 };
int must_enter = 4;
if (!list1.Contains(must_enter))
{
list1.Add(must_enter);
}
结果编号将在列表末尾输入,但我希望在3之后(5之前)。
请注意,由于项目的具体细节,我不能使用排序列表,但列表中的所有数字都是保证按升序排列(0,2,6,9,10,。 ..)
编辑:我知道错误,这就是我所做的:
List<int> list0 = new List<int>() { 1, 2, 3, 5, 6 };
int must_enter = 7;
if (!list0.Contains(must_enter))
{
if (must_enter < list0.Max())
{
int result = list0.FindIndex(item => item > must_enter || must_enter > list0.Max());
list0.Insert(result, must_enter);
}
else
{
list0.Add(must_enter);
}
}
edit2:无论如何,由于几个因素,我已经切换到BinarySearch方法。每个人都感谢你的帮助!
答案 0 :(得分:4)
你可以这样做:
int index = list1.BinarySearch(must_enter);
if (index < 0)
list1.Insert(~index, must_enter);
通过这种方式,您可以使用最佳性能对列表进行排序。
答案 1 :(得分:2)
你可以这样做:
list1.Add(must_enter);
然后订购清单:
list1 = list1.OrderBy(n => n).ToList();
结果将是:
0, 1, 2, 3, 4, 5, 6
修改强>
或使用extesion方法:
static class Utility
{
public static void InsertElement(this List<int> list, int n)
{
if(!list.Contains(n))
{
for(int i = 0; i < list.Count; i++)
{
if(list[i] > n)
{
list.Insert(i-1, n);
break;
}
if(i == list.Count - 1)
list.Add(n);
}
}
}
}
然后:
list1.InsertElement(must_enter);
答案 2 :(得分:1)
您正在寻找
list1.Insert(index, must_enter);
在特定索引处而不是在列表末尾插入元素。
您必须首先找到要插入的索引,这可以通过二进制搜索轻松完成。从列表中间的值开始,并将其与要插入的数字进行比较。如果它更大,搜索列表的下半部分,如果更多,搜索列表的上半部分。重复此过程,每次将列表分成两半,直到找到前面的项目小于您要插入的项目的位置,并且后面的项目大于您要插入的项目。 (编辑:当然,如果你的列表总是非常小,那么从一开始就遍历列表找到合适的位置可能不那么麻烦!)
答案 3 :(得分:0)
list1.Insert(4, 4)
List<T>.Insert Method
- 在指定索引处的List中插入一个元素。
快速注释 - 在许多情况下,List类型上的Insert实例方法没有良好的性能。因为对于Insert,list必须调整以下元素。
这是我收到此答案的原始帖子试试看可能会对您有所帮助:Finding best position for element in list
List<int> list = new List<int>{0,2,6,9,10};
for (int i = 0; i < list1.Count; i++)
{
int index = list.BinarySearch(i);
if( i < 0)
{
int insertIndex = ~index;
list.Insert(insertIndex, i);
}
}
只需要一个缺少的元素,因为op需要
int index = list.BinarySearch(4);
if( index < 0)
{
int insertIndex = ~index;
list.Insert(insertIndex, 4);
}
或
List<int> list1 = new List<int>() { 0,2,6,9,10 };
int must_enter = 4;
for (int i = 0; i < list1.Count; i++)
{
if (!list1.Contains(i))
{
list1.Insert(i , i);
}
}
只需要一个元素作为op需要
if (!list1.Contains(4))
{
list1.Insert(4 , 4);
}
答案 4 :(得分:0)
List<int> list1 = new List<int>() { 0, 1, 2, 3, 5, 6 };
int must_enter = 4;
for (int i = 0; i < list1.Count; i++)
{
if (must_enter >= list1[i])
{
list1.Insert(i + 1, must_enter);
}
}
编辑:我喜欢sarwar026,实施得更好。
答案 5 :(得分:0)
List<int> list1 = new List<int>(){ 0, 1, 2, 3, 5, 6 };
int must_enter = 4;
if (!list1.Contains(must_enter))
{
int result = list.FindIndex(item => item > must_enter);
if(result!=-1)
list1.Insert(result, must_enter);
else // must_enter is not found
{
if(must_enter > list.Max()) // must_enter > max value of list
list1.Add(must_enter);
else if(must_enter < list.Min()) // must_enter < min value of list
list1.Insert(0, must_enter);
}
}
首先,找到大于 must_enter (4)的数字的索引,然后将 must_enter 插入该位置
答案 6 :(得分:-1)
if (!list1.Contains(must_enter))
{
SortedSet<int> sorted = new SortedSet<int>( list1 );
sorted.Add( must_enter );
list1 = sorted.ToList();
}