Array类的CreateInstarnce()方法可以创建不基于0的数组,例如下面创建一个包含2×3个元素的二维数组,第一维基于1,第二维基于10:int[] lengths = {2,3};
int[] lowerBounds = {1,10};
Array racers = Array.CreateInstance(typeof(Person),lengths,lowerBounds);我对着个方法的参数不是很理解,怎么叫做基于1和基于10?这2个数组形式的参数具体代表什么意思啊?谢谢回答!另:别说msdn上的事,如果能看懂我就不发帖问了,谢谢合作!

解决方案 »

  1.   

    秩下限...你这个问题跟今天的这个帖有点像...
    http://topic.csdn.net/u/20091106/13/e251031d-69ca-4291-9da3-b0c6ca6ab39d.html
    建议你去学学线性代数...这是基础数学问题,MSDN是不会做解释的...
      

  2.   

    int[] lowerBounds = {1,10};你这应该是赋值。
    Array racers = Array.CreateInstance(typeof(Person),lengths,lowerBounds);
    typeof(Person) 为 定义的数组成员类型;
    lengths 为 数组长度;
    lowerBounds 为 最低位数,也可以成为从多少开始维数。
      

  3.   

    比如存放2000年到2009年每个月的GDP产值,用decimal[2000...2009][1...12]这样的两维数组就可以很方便的表示了,内存开销才120元素。第一维基于2000,第二维基于1。反过来,用decimal[2009][12]基于0的两维数组来表示也可以,但存储就很浪费,共2010*13个元素。又比如VB的数组下标就是从1开始的,而不是0。
      

  4.   


    MSDN:
    与大多数类不同,Array 提供 CreateInstance 方法,以便允许后期绑定访问,而不是提供公共构造函数。lengths 和 lowerBounds 数组必须具有相同的元素数。lengths 数组中的元素数必须等于新 Array 中的维数。lengths 数组的每个元素都必须指定新 Array 中的对应维度的长度。lowerBounds 数组中的每个元素都必须指定新 Array 中的对应维度的下限。通常情况下,.NET Framework 类库和许多编程语言不处理非零下限。将引用类型元素初始化为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。将值类型元素初始化为零。此方法的运算复杂度为 O(n),其中 n 是 lengths 中所有值的乘积。
    上面说的明白了没?int[] lengths = {2,3};  
    int[] lowerBounds = {1,10};
    Array racers = Array.CreateInstance(typeof(Person),lengths,lowerBounds);意思是 说明要创建的是2维数组
    第一维数组从1开始最大到4(因为你设置的长度为3)结束,第二维从10开始到13结束(长度为3)
      

  5.   

    发个MSDN的例子给你看 运行看下 结果using System;
    public class SamplesArray  {   public static void Main()  {      // Creates and initializes a multidimensional Array of type String.
          int[] myLengthsArray = new int[2] { 3, 5 };
          int[] myBoundsArray = new int[2] { 2, 3 };
          Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
          for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
             for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ )  {
                int[] myIndicesArray = new int[2] { i, j };
                myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
             }      // Displays the lower bounds and the upper bounds of each dimension.
          Console.WriteLine( "Bounds:\tLower\tUpper" );
          for ( int i = 0; i < myArray.Rank; i++ )
             Console.WriteLine( "{0}:\t{1}\t{2}", i, myArray.GetLowerBound(i), myArray.GetUpperBound(i) );      // Displays the values of the Array.
          Console.WriteLine( "The Array contains the following values:" );
          PrintValues( myArray );
       }
       public static void PrintValues( Array myArr )  {
          System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
          int i = 0;
          int cols = myArr.GetLength( myArr.Rank - 1 );
          while ( myEnumerator.MoveNext() )  {
             if ( i < cols )  {
                i++;
             } else  {
                Console.WriteLine();
                i = 1;
             }
             Console.Write( "\t{0}", myEnumerator.Current );
          }
          Console.WriteLine();
       }
    }
    /* 
    This code produces the following output.Bounds:    Lower    Upper
    0:    2    4
    1:    3    7
    The Array contains the following values:
        23    24    25    26    27
        33    34    35    36    37
        43    44    45    46    47
    */