楼主还是可以考虑一下List<T>的,这里面的排序规则可以自己定义。 在执行Insert以后,再做一步List.Sort (Generic Comparison) Comparison可以自己定义,即使多个条件,或者T是结构体也没问题 private static int CompareDinosByLength(string x, string y) { if (x == null) { if (y == null) { // If x is null and y is null, they're // equal. return 0; } else { // If x is null and y is not null, y // is greater. return -1; } } else { // If x is not null... // if (y == null) // ...and y is null, x is greater. { return 1; } else { // ...and y is not null, compare the // lengths of the two strings. // int retval = x.Length.CompareTo(y.Length); if (retval != 0) { // If the strings are not of equal length, // the longer string is greater. // return retval; } else { // If the strings are of equal length, // sort them with ordinary string comparison. // return x.CompareTo(y); } } } }
int toAppend=list.Lenght-list[list.Lenght-1];int [] newlist=new [toAppend+list.Lenght];for(int i=1;i<=newList.Lenght;i++)
{
newlist[i-1]=i;
}
for(int i=1;i<="需要的上限";i++)
{
if(!list.Contains(i))
{
list.Add(i);
}
}
可能会比较耗时间
楼主还是可以考虑一下List<T>的,这里面的排序规则可以自己定义。
在执行Insert以后,再做一步List.Sort (Generic Comparison)
Comparison可以自己定义,即使多个条件,或者T是结构体也没问题 private static int CompareDinosByLength(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo(y.Length); if (retval != 0)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
}
另外,List本质上也是数组,这点可以查源码得知。