我的问题是:比如我需要定义一个数组第一个数组长度是8,第二个为4,第三是2,一次类推,我希望第一个数组长度为我输入参数,这个如何定义比较好,另外,其他数组如何定义?

解决方案 »

  1.   

    List<int[]> list = new List<int[]>();
    int maxArrayLength = 8;
    for(int i = 8;i > 0; i = i / 2)
    {
    list.Add(new int[i]);
    }
    int[][] result = = list.ToArray();
      

  2.   

    看看交错数组,
    最好用list;
      

  3.   

    //保存数组的变量 arrs
    Dictionary<int,byte[]> arrs= new Dictionary<int,byte[]>();void Create(int len)
    {
        int i = len;
        while(i>1)
        {
            //建立数组
            byte[] arr = new byte[i];
            //添加到字典变量
            arrs.Add(i,arr);
            i = i / 2;
        }
    }
      

  4.   


     class Program
        {
            static void Main(string[] args)
            {
                int max = 8;
                object ok = arrary(max);
                Console.WriteLine(ok);
            }        /// <summary>
            /// 参数是偶数的数组
            /// </summary>
            /// <param name="n">偶数</param>
            /// <returns></returns>
            public static object arrary(int n)
            {
                List<object> ay = new List<object>();
                ay = AddList(ay, n);
                if ((n / 2) >= 1)
                {
                    for (int index = 0; index < ay.Count;index++ )
                    {
                        ay[index] = arrary(n / 2);
                    }
                }
                return ay.ToArray();
            }        public static List<object> AddList(List<object> o, int index)
            {
                int i=0;
                while (true)
                {
                    o.Add(0);                i++;
                    if (i == index)
                        break;
                }
                return o;
            }