假定一个纯数字的数组,内容时 1,2,4,6,7,9 想要实现向这个数组动态添加值,必须先填补空缺,再添加最大数,比如添加要5个数,分别就得是 3,5,8,10,11。被这个问题完全困扰住了,求解答啊。

解决方案 »

  1.   

    int [] list=new[]{1,2,4,6,7,9};
    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;
    }
      

  2.   

    一个系统的方法,给你。IList<int> sortedList = new SortedList<int>();sortedList.Insert(index,item);随便你怎么插入都是排序的。
      

  3.   

    一个系统的方法,给你。IList<int> sortedList = new SortedList<int>(); sortedList.Insert(index,item);随便你怎么插入都是排序的。 
      

  4.   

    最大值知道,最小值知道先产生一个从最大值 到最小值的连续集合,用这个集合和原始求差集就是当然整个过程,你可以直接用linq,也可以自己用循环搞定
      

  5.   

    换成泛型吧List<int>List<int> list=new List<int>{1,2,4,5,8};
    for(int i=1;i<="需要的上限";i++)
    {
    if(!list.Contains(i))
    {
    list.Add(i);
    }
    }
    可能会比较耗时间
      

  6.   


    楼主还是可以考虑一下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);
                    }
                }
            }
        }
      

  7.   

    执行排序的时机控制好,比每次插入都去找一次可能还要快一些。
    另外,List本质上也是数组,这点可以查源码得知。