Array.CreateInstance Array.CreateInstance Array.CreateInstance Array.CreateInstance Method

定义

初始化 Array 类的新实例。Initializes a new instance of the Array class.

重载

CreateInstance(Type, Int32) CreateInstance(Type, Int32) CreateInstance(Type, Int32) CreateInstance(Type, Int32)

创建使用从零开始的索引、具有指定 Type 和长度的一维 ArrayCreates a one-dimensional Array of the specified Type and length, with zero-based indexing.

CreateInstance(Type, Int32[]) CreateInstance(Type, Int32[]) CreateInstance(Type, Int32[]) CreateInstance(Type, Int32[])

创建索引从零开始、具有指定 Type 和维长的多维 ArrayCreates a multidimensional Array of the specified Type and dimension lengths, with zero-based indexing. 维的长度在一个 32 位整数数组中指定。The dimension lengths are specified in an array of 32-bit integers.

CreateInstance(Type, Int64[]) CreateInstance(Type, Int64[]) CreateInstance(Type, Int64[]) CreateInstance(Type, Int64[])

创建索引从零开始、具有指定 Type 和维长的多维 ArrayCreates a multidimensional Array of the specified Type and dimension lengths, with zero-based indexing. 维度的长度在一个 64 位整数数组中指定。The dimension lengths are specified in an array of 64-bit integers.

CreateInstance(Type, Int32, Int32) CreateInstance(Type, Int32, Int32) CreateInstance(Type, Int32, Int32) CreateInstance(Type, Int32, Int32)

创建使用从零开始的索引、具有指定 Array 和维长的二维 TypeCreates a two-dimensional Array of the specified Type and dimension lengths, with zero-based indexing.

CreateInstance(Type, Int32[], Int32[]) CreateInstance(Type, Int32[], Int32[]) CreateInstance(Type, Int32[], Int32[]) CreateInstance(Type, Int32[], Int32[])

创建具有指定下限、指定 Array 和维长的多维 TypeCreates a multidimensional Array of the specified Type and dimension lengths, with the specified lower bounds.

CreateInstance(Type, Int32, Int32, Int32) CreateInstance(Type, Int32, Int32, Int32) CreateInstance(Type, Int32, Int32, Int32) CreateInstance(Type, Int32, Int32, Int32)

创建使用从零开始的索引、具有指定 Type 和维长的三维 ArrayCreates a three-dimensional Array of the specified Type and dimension lengths, with zero-based indexing.

CreateInstance(Type, Int32) CreateInstance(Type, Int32) CreateInstance(Type, Int32) CreateInstance(Type, Int32)

创建使用从零开始的索引、具有指定 Type 和长度的一维 ArrayCreates a one-dimensional Array of the specified Type and length, with zero-based indexing.

public:
 static Array ^ CreateInstance(Type ^ elementType, int length);
public static Array CreateInstance (Type elementType, int length);
static member CreateInstance : Type * int -> Array
Public Shared Function CreateInstance (elementType As Type, length As Integer) As Array

参数

elementType
Type Type Type Type

要创建的 ArrayTypeThe Type of the Array to create.

length
Int32 Int32 Int32 Int32

要创建的 Array 的大小。The size of the Array to create.

返回

使用从零开始的索引、具有指定 Type 和长度的新的一维 ArrayA new one-dimensional Array of the specified Type with the specified length, using zero-based indexing.

异常

elementType 不是有效的 TypeelementType is not a valid Type.

不支持 elementTypeelementType is not supported. 例如,不支持 VoidFor example, Void is not supported.

-or- elementType 为开放式泛型类型。elementType is an open generic type.

示例

下面的代码示例演示如何创建和初始化一维 ArrayThe following code example shows how to create and initialize a one-dimensional Array.

using namespace System;
void PrintValues( Array^ myArr );
void main()
{
   // Creates and initializes a one-dimensional Array instance of type Int32.
   Array^ my1DArray = Array::CreateInstance( Int32::typeid, 5 );
   for ( int i = my1DArray->GetLowerBound( 0 ); i <= my1DArray->GetUpperBound( 0 ); i++ )
      my1DArray->SetValue( i + 1, i );

   // Displays the values of the Array.
   Console::WriteLine(  "The one-dimensional Array instance contains the following values:" );
   PrintValues( my1DArray );
}

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.
 
 The one-dimensional Array instance contains the following values:
     1    2    3    4    5
 */
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a one-dimensional Array of type Int32.
      Array my1DArray=Array.CreateInstance( typeof(Int32), 5 );
      for ( int i = my1DArray.GetLowerBound(0); i <= my1DArray.GetUpperBound(0); i++ )
         my1DArray.SetValue( i+1, i );

      // Displays the values of the Array.
      Console.WriteLine( "The one-dimensional Array contains the following values:" );
      PrintValues( my1DArray );
   }


   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.

The one-dimensional Array contains the following values:
    1    2    3    4    5
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a one-dimensional Array of type Int32.
        Dim my1DArray As Array = Array.CreateInstance(GetType(Int32), 5)
        Dim i As Integer
        For i = my1DArray.GetLowerBound(0) To my1DArray.GetUpperBound(0)
            my1DArray.SetValue(i + 1, i)
        Next i 
        ' Displays the values of the Array.
        Console.WriteLine("The one-dimensional Array contains the " _
           + "following values:")
        PrintValues(my1DArray)
        
    End Sub
    
    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The one-dimensional Array contains the following values:
'     1    2    3    4    5 

注解

与大多数类不同,Array 提供 @no__t 1 方法,而不是公共构造函数,以允许进行后期绑定访问。Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

引用类型的元素初始化为 nullReference-type elements are initialized to null. 值类型元素初始化为零。Value-type elements are initialized to zero.

此方法的运算复杂度为 O (n),其中 n lengthThis method is an O(n) operation, where n is length.

CreateInstance(Type, Int32[]) CreateInstance(Type, Int32[]) CreateInstance(Type, Int32[]) CreateInstance(Type, Int32[])

创建索引从零开始、具有指定 Type 和维长的多维 ArrayCreates a multidimensional Array of the specified Type and dimension lengths, with zero-based indexing. 维的长度在一个 32 位整数数组中指定。The dimension lengths are specified in an array of 32-bit integers.

public:
 static Array ^ CreateInstance(Type ^ elementType, ... cli::array <int> ^ lengths);
public static Array CreateInstance (Type elementType, params int[] lengths);
static member CreateInstance : Type * int[] -> Array
Public Shared Function CreateInstance (elementType As Type, ParamArray lengths As Integer()) As Array

参数

elementType
Type Type Type Type

要创建的 ArrayTypeThe Type of the Array to create.

lengths
Int32[]

一个 32 位整数数组,它表示要创建的 Array 中每个维度的大小。An array of 32-bit integers that represent the size of each dimension of the Array to create.

返回

指定 Type 的新多维 Array,该数组每个维具有指定长度,且使用从零开始的索引。A new multidimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

异常

elementTypenullelementType is null.

- 或 --or- lengthsnulllengths is null.

elementType 不是有效的 TypeelementType is not a valid Type.

-or- lengths 数组包含的元素少于一个。The lengths array contains less than one element.

不支持 elementTypeelementType is not supported. 例如,不支持 VoidFor example, Void is not supported.

-or- elementType 为开放式泛型类型。elementType is an open generic type.

lengths 中的任何值都小于零。Any value in lengths is less than zero.

示例

下面的代码示例演示如何创建和初始化多维 @no__t。The following code example shows how to create and initialize a multidimensional Array.

using namespace System;
void PrintValues( Array^ myArr );
void main()
{
   // Creates and initializes a multidimensional Array instance of type String.
   array<int>^myLengthsArray = {2,3,4,5};
   Array^ my4DArray = Array::CreateInstance( String::typeid, myLengthsArray );
   for ( int i = my4DArray->GetLowerBound( 0 ); i <= my4DArray->GetUpperBound( 0 ); i++ )
      for ( int j = my4DArray->GetLowerBound( 1 ); j <= my4DArray->GetUpperBound( 1 ); j++ )
         for ( int k = my4DArray->GetLowerBound( 2 ); k <= my4DArray->GetUpperBound( 2 ); k++ )
            for ( int l = my4DArray->GetLowerBound( 3 ); l <= my4DArray->GetUpperBound( 3 ); l++ )
            {
               array<int>^myIndicesArray = {i,j,k,l};
               my4DArray->SetValue( String::Concat( Convert::ToString( i ), j, k, l ), myIndicesArray );

            }

   // Displays the values of the Array.
   Console::WriteLine(  "The four-dimensional Array instance contains the following values:" );
   PrintValues( my4DArray );
}

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.
 
 The four-dimensional Array instance contains the following values:
     0000    0001    0002    0003    0004
     0010    0011    0012    0013    0014
     0020    0021    0022    0023    0024
     0030    0031    0032    0033    0034
     0100    0101    0102    0103    0104
     0110    0111    0112    0113    0114
     0120    0121    0122    0123    0124
     0130    0131    0132    0133    0134
     0200    0201    0202    0203    0204
     0210    0211    0212    0213    0214
     0220    0221    0222    0223    0224
     0230    0231    0232    0233    0234
     1000    1001    1002    1003    1004
     1010    1011    1012    1013    1014
     1020    1021    1022    1023    1024
     1030    1031    1032    1033    1034
     1100    1101    1102    1103    1104
     1110    1111    1112    1113    1114
     1120    1121    1122    1123    1124
     1130    1131    1132    1133    1134
     1200    1201    1202    1203    1204
     1210    1211    1212    1213    1214
     1220    1221    1222    1223    1224
     1230    1231    1232    1233    1234
 */
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a multidimensional Array of type String.
      int[] myLengthsArray = new int[4] { 2, 3, 4, 5 };
      Array my4DArray=Array.CreateInstance( typeof(String), myLengthsArray );
      for ( int i = my4DArray.GetLowerBound(0); i <= my4DArray.GetUpperBound(0); i++ )
         for ( int j = my4DArray.GetLowerBound(1); j <= my4DArray.GetUpperBound(1); j++ )
            for ( int k = my4DArray.GetLowerBound(2); k <= my4DArray.GetUpperBound(2); k++ )
               for ( int l = my4DArray.GetLowerBound(3); l <= my4DArray.GetUpperBound(3); l++ )  {
                  int[] myIndicesArray = new int[4] { i, j, k, l };
                  my4DArray.SetValue( Convert.ToString(i) + j + k + l, myIndicesArray );
               }

      // Displays the values of the Array.
      Console.WriteLine( "The four-dimensional Array contains the following values:" );
      PrintValues( my4DArray );
   }


   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.

The four-dimensional Array contains the following values:
    0000    0001    0002    0003    0004
    0010    0011    0012    0013    0014
    0020    0021    0022    0023    0024
    0030    0031    0032    0033    0034
    0100    0101    0102    0103    0104
    0110    0111    0112    0113    0114
    0120    0121    0122    0123    0124
    0130    0131    0132    0133    0134
    0200    0201    0202    0203    0204
    0210    0211    0212    0213    0214
    0220    0221    0222    0223    0224
    0230    0231    0232    0233    0234
    1000    1001    1002    1003    1004
    1010    1011    1012    1013    1014
    1020    1021    1022    1023    1024
    1030    1031    1032    1033    1034
    1100    1101    1102    1103    1104
    1110    1111    1112    1113    1114
    1120    1121    1122    1123    1124
    1130    1131    1132    1133    1134
    1200    1201    1202    1203    1204
    1210    1211    1212    1213    1214
    1220    1221    1222    1223    1224
    1230    1231    1232    1233    1234
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a multidimensional Array of type String.
        Dim myLengthsArray() As Integer = {2, 3, 4, 5}
        Dim my4DArray As Array = Array.CreateInstance(GetType(String), myLengthsArray)
        Dim i, j, k, l As Integer
        Dim myIndicesArray() As Integer
        For i = my4DArray.GetLowerBound(0) To my4DArray.GetUpperBound(0)
            For j = my4DArray.GetLowerBound(1) To my4DArray.GetUpperBound(1)
                For k = my4DArray.GetLowerBound(2) To my4DArray.GetUpperBound(2)
                    For l = my4DArray.GetLowerBound(3) To my4DArray.GetUpperBound(3)
                        myIndicesArray = New Integer() {i, j, k, l}
                        my4DArray.SetValue(Convert.ToString(i) + j.ToString() _
                           + k.ToString() + l.ToString(), myIndicesArray)
                    Next l
                Next k 
            Next j
        Next i

        ' Displays the values of the Array.
        Console.WriteLine("The four-dimensional Array contains the following values:")
        PrintValues(my4DArray)
    End Sub
    
    
    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The four-dimensional Array contains the following values:
'     0000    0001    0002    0003    0004
'     0010    0011    0012    0013    0014
'     0020    0021    0022    0023    0024
'     0030    0031    0032    0033    0034
'     0100    0101    0102    0103    0104
'     0110    0111    0112    0113    0114
'     0120    0121    0122    0123    0124
'     0130    0131    0132    0133    0134
'     0200    0201    0202    0203    0204
'     0210    0211    0212    0213    0214
'     0220    0221    0222    0223    0224
'     0230    0231    0232    0233    0234
'     1000    1001    1002    1003    1004
'     1010    1011    1012    1013    1014
'     1020    1021    1022    1023    1024
'     1030    1031    1032    1033    1034
'     1100    1101    1102    1103    1104
 '    1110    1111    1112    1113    1114
'     1120    1121    1122    1123    1124
'     1130    1131    1132    1133    1134
'     1200    1201    1202    1203    1204
'     1210    1211    1212    1213    1214
'     1220    1221    1222    1223    1224
'     1230    1231    1232    1233    1234

注解

与大多数类不同,Array 提供 @no__t 1 方法,而不是公共构造函数,以允许进行后期绑定访问。Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

@No__t 数组中的元素数必须等于新 @no__t 中的维数。The number of elements in the lengths array must equal the number of dimensions in the new Array. @No__t-0 数组的每个元素都必须在新的 @no__t 中指定相应维度的长度。Each element of the lengths array must specify the length of the corresponding dimension in the new Array.

引用类型的元素初始化为 nullReference-type elements are initialized to null. 值类型元素初始化为零。Value-type elements are initialized to zero.

此方法的运算复杂度为 O (n),其中 nlengths 中所有值的乘积。This method is an O(n) operation, where n is the product of all values in lengths.

CreateInstance(Type, Int64[]) CreateInstance(Type, Int64[]) CreateInstance(Type, Int64[]) CreateInstance(Type, Int64[])

创建索引从零开始、具有指定 Type 和维长的多维 ArrayCreates a multidimensional Array of the specified Type and dimension lengths, with zero-based indexing. 维度的长度在一个 64 位整数数组中指定。The dimension lengths are specified in an array of 64-bit integers.

public:
 static Array ^ CreateInstance(Type ^ elementType, ... cli::array <long> ^ lengths);
public static Array CreateInstance (Type elementType, params long[] lengths);
static member CreateInstance : Type * int64[] -> Array
Public Shared Function CreateInstance (elementType As Type, ParamArray lengths As Long()) As Array

参数

elementType
Type Type Type Type

要创建的 ArrayTypeThe Type of the Array to create.

lengths
Int64[]

一个 64 位整数数组,它表示要创建的 Array 中每个维度的大小。An array of 64-bit integers that represent the size of each dimension of the Array to create. 数组中的每个整数都必须介于零和 MaxValue 之间,(包括零和 System.Int32.MaxValue)。Each integer in the array must be between zero and MaxValue, inclusive.

返回

指定 Type 的新多维 Array,该数组每个维具有指定长度,且使用从零开始的索引。A new multidimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

异常

elementTypenullelementType is null.

-or- lengthsnulllengths is null.

elementType 不是有效的 TypeelementType is not a valid Type.

- 或 --or- lengths 数组包含的元素少于一个。The lengths array contains less than one element.

不支持 elementTypeelementType is not supported. 例如,不支持 VoidFor example, Void is not supported.

-or- elementType 为开放式泛型类型。elementType is an open generic type.

lengths 中的任何值小于零或大于 MaxValueAny value in lengths is less than zero or greater than MaxValue.

示例

下面的代码示例演示如何创建和初始化多维 @no__t。The following code example shows how to create and initialize a multidimensional Array.

using namespace System;
void PrintValues( Array^ myArr );
void main()
{
   // Creates and initializes a multidimensional Array instance of type String.
   array<int>^myLengthsArray = {2,3,4,5};
   Array^ my4DArray = Array::CreateInstance( String::typeid, myLengthsArray );
   for ( int i = my4DArray->GetLowerBound( 0 ); i <= my4DArray->GetUpperBound( 0 ); i++ )
      for ( int j = my4DArray->GetLowerBound( 1 ); j <= my4DArray->GetUpperBound( 1 ); j++ )
         for ( int k = my4DArray->GetLowerBound( 2 ); k <= my4DArray->GetUpperBound( 2 ); k++ )
            for ( int l = my4DArray->GetLowerBound( 3 ); l <= my4DArray->GetUpperBound( 3 ); l++ )
            {
               array<int>^myIndicesArray = {i,j,k,l};
               my4DArray->SetValue( String::Concat( Convert::ToString( i ), j, k, l ), myIndicesArray );

            }

   // Displays the values of the Array.
   Console::WriteLine(  "The four-dimensional Array instance contains the following values:" );
   PrintValues( my4DArray );
}

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.
 
 The four-dimensional Array instance contains the following values:
     0000    0001    0002    0003    0004
     0010    0011    0012    0013    0014
     0020    0021    0022    0023    0024
     0030    0031    0032    0033    0034
     0100    0101    0102    0103    0104
     0110    0111    0112    0113    0114
     0120    0121    0122    0123    0124
     0130    0131    0132    0133    0134
     0200    0201    0202    0203    0204
     0210    0211    0212    0213    0214
     0220    0221    0222    0223    0224
     0230    0231    0232    0233    0234
     1000    1001    1002    1003    1004
     1010    1011    1012    1013    1014
     1020    1021    1022    1023    1024
     1030    1031    1032    1033    1034
     1100    1101    1102    1103    1104
     1110    1111    1112    1113    1114
     1120    1121    1122    1123    1124
     1130    1131    1132    1133    1134
     1200    1201    1202    1203    1204
     1210    1211    1212    1213    1214
     1220    1221    1222    1223    1224
     1230    1231    1232    1233    1234
 */
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a multidimensional Array of type String.
      int[] myLengthsArray = new int[4] { 2, 3, 4, 5 };
      Array my4DArray=Array.CreateInstance( typeof(String), myLengthsArray );
      for ( int i = my4DArray.GetLowerBound(0); i <= my4DArray.GetUpperBound(0); i++ )
         for ( int j = my4DArray.GetLowerBound(1); j <= my4DArray.GetUpperBound(1); j++ )
            for ( int k = my4DArray.GetLowerBound(2); k <= my4DArray.GetUpperBound(2); k++ )
               for ( int l = my4DArray.GetLowerBound(3); l <= my4DArray.GetUpperBound(3); l++ )  {
                  int[] myIndicesArray = new int[4] { i, j, k, l };
                  my4DArray.SetValue( Convert.ToString(i) + j + k + l, myIndicesArray );
               }

      // Displays the values of the Array.
      Console.WriteLine( "The four-dimensional Array contains the following values:" );
      PrintValues( my4DArray );
   }


   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.

The four-dimensional Array contains the following values:
    0000    0001    0002    0003    0004
    0010    0011    0012    0013    0014
    0020    0021    0022    0023    0024
    0030    0031    0032    0033    0034
    0100    0101    0102    0103    0104
    0110    0111    0112    0113    0114
    0120    0121    0122    0123    0124
    0130    0131    0132    0133    0134
    0200    0201    0202    0203    0204
    0210    0211    0212    0213    0214
    0220    0221    0222    0223    0224
    0230    0231    0232    0233    0234
    1000    1001    1002    1003    1004
    1010    1011    1012    1013    1014
    1020    1021    1022    1023    1024
    1030    1031    1032    1033    1034
    1100    1101    1102    1103    1104
    1110    1111    1112    1113    1114
    1120    1121    1122    1123    1124
    1130    1131    1132    1133    1134
    1200    1201    1202    1203    1204
    1210    1211    1212    1213    1214
    1220    1221    1222    1223    1224
    1230    1231    1232    1233    1234
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a multidimensional Array of type String.
        Dim myLengthsArray() As Integer = {2, 3, 4, 5}
        Dim my4DArray As Array = Array.CreateInstance(GetType(String), myLengthsArray)
        Dim i, j, k, l As Integer
        Dim myIndicesArray() As Integer
        For i = my4DArray.GetLowerBound(0) To my4DArray.GetUpperBound(0)
            For j = my4DArray.GetLowerBound(1) To my4DArray.GetUpperBound(1)
                For k = my4DArray.GetLowerBound(2) To my4DArray.GetUpperBound(2)
                    For l = my4DArray.GetLowerBound(3) To my4DArray.GetUpperBound(3)
                        myIndicesArray = New Integer() {i, j, k, l}
                        my4DArray.SetValue(Convert.ToString(i) + j.ToString() _
                           + k.ToString() + l.ToString(), myIndicesArray)
                    Next l
                Next k 
            Next j
        Next i

        ' Displays the values of the Array.
        Console.WriteLine("The four-dimensional Array contains the following values:")
        PrintValues(my4DArray)
    End Sub
    
    
    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The four-dimensional Array contains the following values:
'     0000    0001    0002    0003    0004
'     0010    0011    0012    0013    0014
'     0020    0021    0022    0023    0024
'     0030    0031    0032    0033    0034
'     0100    0101    0102    0103    0104
'     0110    0111    0112    0113    0114
'     0120    0121    0122    0123    0124
'     0130    0131    0132    0133    0134
'     0200    0201    0202    0203    0204
'     0210    0211    0212    0213    0214
'     0220    0221    0222    0223    0224
'     0230    0231    0232    0233    0234
'     1000    1001    1002    1003    1004
'     1010    1011    1012    1013    1014
'     1020    1021    1022    1023    1024
'     1030    1031    1032    1033    1034
'     1100    1101    1102    1103    1104
 '    1110    1111    1112    1113    1114
'     1120    1121    1122    1123    1124
'     1130    1131    1132    1133    1134
'     1200    1201    1202    1203    1204
'     1210    1211    1212    1213    1214
'     1220    1221    1222    1223    1224
'     1230    1231    1232    1233    1234

注解

与大多数类不同,Array 提供 @no__t 1 方法,而不是公共构造函数,以允许进行后期绑定访问。Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

@No__t 数组中的元素数必须等于新 @no__t 中的维数。The number of elements in the lengths array must equal the number of dimensions in the new Array. @No__t-0 数组的每个元素都必须在新的 @no__t 中指定相应维度的长度。Each element of the lengths array must specify the length of the corresponding dimension in the new Array.

引用类型的元素初始化为 nullReference-type elements are initialized to null. 值类型元素初始化为零。Value-type elements are initialized to zero.

此方法的运算复杂度为 O (n),其中 nlengths 中所有值的乘积。This method is an O(n) operation, where n is the product of all values in lengths.

CreateInstance(Type, Int32, Int32) CreateInstance(Type, Int32, Int32) CreateInstance(Type, Int32, Int32) CreateInstance(Type, Int32, Int32)

创建使用从零开始的索引、具有指定 Array 和维长的二维 TypeCreates a two-dimensional Array of the specified Type and dimension lengths, with zero-based indexing.

public:
 static Array ^ CreateInstance(Type ^ elementType, int length1, int length2);
public static Array CreateInstance (Type elementType, int length1, int length2);
static member CreateInstance : Type * int * int -> Array
Public Shared Function CreateInstance (elementType As Type, length1 As Integer, length2 As Integer) As Array

参数

elementType
Type Type Type Type

要创建的 ArrayTypeThe Type of the Array to create.

length1
Int32 Int32 Int32 Int32

要创建的 Array 的第一维的大小。The size of the first dimension of the Array to create.

length2
Int32 Int32 Int32 Int32

要创建的 Array 的第二维的大小。The size of the second dimension of the Array to create.

返回

使用从零开始的索引、具有指定 Type 的新的二维 Array,其每个维度都为指定的长度。A new two-dimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

异常

elementType 不是有效的 TypeelementType is not a valid Type.

不支持 elementTypeelementType is not supported. 例如,不支持 VoidFor example, Void is not supported.

-or- elementType 为开放式泛型类型。elementType is an open generic type.

length1 小于零。length1 is less than zero.

-or- length2 小于零。length2 is less than zero.

示例

下面的代码示例演示如何创建和初始化二维 @no__t。The following code example shows how to create and initialize a two-dimensional Array.

using namespace System;
void PrintValues( Array^ myArr );
void main()
{
   // Creates and initializes a two-dimensional Array instance of type String.
   Array^ my2DArray = Array::CreateInstance( String::typeid, 2, 3 );
   for ( int i = my2DArray->GetLowerBound( 0 ); i <= my2DArray->GetUpperBound( 0 ); i++ )
      for ( int j = my2DArray->GetLowerBound( 1 ); j <= my2DArray->GetUpperBound( 1 ); j++ )
         my2DArray->SetValue( String::Concat( "abc", i, j ), i, j );
   
   // Displays the values of the Array.
   Console::WriteLine(  "The two-dimensional Array instance contains the following values:" );
   PrintValues( my2DArray );
}

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.
 
 The two-dimensional Array instance contains the following values:
     abc00    abc01    abc02
     abc10    abc11    abc12
 */
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a two-dimensional Array of type String.
      Array my2DArray=Array.CreateInstance( typeof(String), 2, 3 );
      for ( int i = my2DArray.GetLowerBound(0); i <= my2DArray.GetUpperBound(0); i++ )
         for ( int j = my2DArray.GetLowerBound(1); j <= my2DArray.GetUpperBound(1); j++ )
            my2DArray.SetValue( "abc" + i + j, i, j );

      // Displays the values of the Array.
      Console.WriteLine( "The two-dimensional Array contains the following values:" );
      PrintValues( my2DArray );
   }


   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.

The two-dimensional Array contains the following values:
    abc00    abc01    abc02
    abc10    abc11    abc12
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a two-dimensional Array of type String.
        Dim my2DArray As Array = Array.CreateInstance(GetType(String), 2, 3)
        Dim i, j As Integer        
        For i = my2DArray.GetLowerBound(0) To my2DArray.GetUpperBound(0)
            For j = my2DArray.GetLowerBound(1) To my2DArray.GetUpperBound(1)
                my2DArray.SetValue("abc" + i.ToString() + j.ToString(), i, j)
            Next j 
        Next i

        ' Displays the values of the Array.
        Console.WriteLine("The two-dimensional Array contains the " _
           + "following values:")
        PrintValues(my2DArray)
    End Sub   
    
    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The two-dimensional Array contains the following values:
'     abc00    abc01    abc02
'     abc10    abc11    abc12 

注解

与大多数类不同,Array 提供 @no__t 1 方法,而不是公共构造函数,以允许进行后期绑定访问。Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

引用类型的元素初始化为 nullReference-type elements are initialized to null. 值类型元素初始化为零。Value-type elements are initialized to zero.

此方法的运算复杂度为 O (n),其中 nlength1 和 @no__t 3 的乘积。This method is an O(n) operation, where n is the product of length1 and length2.

CreateInstance(Type, Int32[], Int32[]) CreateInstance(Type, Int32[], Int32[]) CreateInstance(Type, Int32[], Int32[]) CreateInstance(Type, Int32[], Int32[])

创建具有指定下限、指定 Array 和维长的多维 TypeCreates a multidimensional Array of the specified Type and dimension lengths, with the specified lower bounds.

public:
 static Array ^ CreateInstance(Type ^ elementType, cli::array <int> ^ lengths, cli::array <int> ^ lowerBounds);
public static Array CreateInstance (Type elementType, int[] lengths, int[] lowerBounds);
static member CreateInstance : Type * int[] * int[] -> Array
Public Shared Function CreateInstance (elementType As Type, lengths As Integer(), lowerBounds As Integer()) As Array

参数

elementType
Type Type Type Type

要创建的 ArrayTypeThe Type of the Array to create.

lengths
Int32[]

一维数组,它包含要创建的 Array 的每个维度的大小。A one-dimensional array that contains the size of each dimension of the Array to create.

lowerBounds
Int32[]

一维数组,它包含要创建的 Array 的每个维度的下限(起始索引)。A one-dimensional array that contains the lower bound (starting index) of each dimension of the Array to create.

返回

具有指定 Type 的新的多维 Array,其每个维度都具有指定长度和下限。A new multidimensional Array of the specified Type with the specified length and lower bound for each dimension.

异常

elementTypenullelementType is null.

-or- lengthsnulllengths is null.

- 或 --or- lowerBoundsnulllowerBounds is null.

elementType 不是有效的 TypeelementType is not a valid Type.

- 或 --or- lengths 数组包含的元素少于一个。The lengths array contains less than one element.

-or- lengthslowerBounds 数组包含的元素数不同。The lengths and lowerBounds arrays do not contain the same number of elements.

不支持 elementTypeelementType is not supported. 例如,不支持 VoidFor example, Void is not supported.

-or- elementType 为开放式泛型类型。elementType is an open generic type.

lengths 中的任何值都小于零。Any value in lengths is less than zero.

- 或 --or- lowerBounds 中的任意一个值都很大,因此,维的下限和长度的和大于 MaxValueAny value in lowerBounds is very large, such that the sum of a dimension's lower bound and length is greater than MaxValue.

示例

下面的代码示例演示如何创建和初始化具有指定下限的多维 @no__t 0。The following code example shows how to create and initialize a multidimensional Array with specified lower bounds.

using namespace System;
void PrintValues( Array^ myArr );
void main()
{
   // Creates and initializes a multidimensional Array instance of type String.
   array<int>^myLengthsArray = {3,5};
   array<int>^myBoundsArray = {2,3};
   Array^ myArray = Array::CreateInstance( String::typeid, myLengthsArray, myBoundsArray );
   for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
      for ( int j = myArray->GetLowerBound( 1 ); j <= myArray->GetUpperBound( 1 ); j++ )
      {
         array<int>^myIndicesArray = {i,j};
         myArray->SetValue( String::Concat( 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 instance contains the following values:" );
   PrintValues( myArray );
}

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 instance contains the following values:
     23    24    25    26    27
     33    34    35    36    37
     43    44    45    46    47
 */
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
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a multidimensional Array of type String.
        Dim myLengthsArray() As Integer = {3, 5}
        Dim myBoundsArray() As Integer = {2, 3}
        Dim myArray As Array = Array.CreateInstance(GetType(String), _
           myLengthsArray, myBoundsArray)
        Dim i, j As Integer
        Dim myIndicesArray() As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            For j = myArray.GetLowerBound(1) To myArray.GetUpperBound(1)
                myIndicesArray = New Integer() {i, j}
                myArray.SetValue(i.ToString() + j.ToString(), myIndicesArray)
            Next j
        Next i
        
        ' Displays the lower bounds and the upper bounds of each dimension.
        Console.WriteLine("Bounds:" + ControlChars.Tab + "Lower" _
           + ControlChars.Tab + "Upper")
        For i = 0 To myArray.Rank - 1
            Console.WriteLine("{0}:" + ControlChars.Tab + "{1}" _
               + ControlChars.Tab + "{2}", i, myArray.GetLowerBound(i), _
               myArray.GetUpperBound(i))
        Next i
        
        ' Displays the values of the Array.
        Console.WriteLine("The Array contains the following values:")
        PrintValues(myArray)
    End Sub    
    
    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' 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 

注解

与大多数类不同,Array 提供 @no__t 1 方法,而不是公共构造函数,以允许进行后期绑定访问。Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

@No__t 0 和 @no__t 1 数组必须具有相同数量的元素。The lengths and lowerBounds arrays must have the same number of elements. @No__t 数组中的元素数必须等于新 @no__t 中的维数。The number of elements in the lengths array must equal the number of dimensions in the new Array.

@No__t-0 数组的每个元素都必须在新的 @no__t 中指定相应维度的长度。Each element of the lengths array must specify the length of the corresponding dimension in the new Array.

@No__t 数组的每个元素都必须在新的 @no__t 中指定相应维度的下限。Each element of the lowerBounds array must specify the lower bound of the corresponding dimension in the new Array. 通常,.NET Framework 类库和许多编程语言不处理非零的下限。Generally, the .NET Framework class library and many programming languages do not handle nonzero lower bounds.

引用类型的元素初始化为 nullReference-type elements are initialized to null. 值类型元素初始化为零。Value-type elements are initialized to zero.

此方法的运算复杂度为 O (n),其中 nlengths 中所有值的乘积。This method is an O(n) operation, where n is the product of all values in lengths.

CreateInstance(Type, Int32, Int32, Int32) CreateInstance(Type, Int32, Int32, Int32) CreateInstance(Type, Int32, Int32, Int32) CreateInstance(Type, Int32, Int32, Int32)

创建使用从零开始的索引、具有指定 Type 和维长的三维 ArrayCreates a three-dimensional Array of the specified Type and dimension lengths, with zero-based indexing.

public:
 static Array ^ CreateInstance(Type ^ elementType, int length1, int length2, int length3);
public static Array CreateInstance (Type elementType, int length1, int length2, int length3);
static member CreateInstance : Type * int * int * int -> Array
Public Shared Function CreateInstance (elementType As Type, length1 As Integer, length2 As Integer, length3 As Integer) As Array

参数

elementType
Type Type Type Type

要创建的 ArrayTypeThe Type of the Array to create.

length1
Int32 Int32 Int32 Int32

要创建的 Array 的第一维的大小。The size of the first dimension of the Array to create.

length2
Int32 Int32 Int32 Int32

要创建的 Array 的第二维的大小。The size of the second dimension of the Array to create.

length3
Int32 Int32 Int32 Int32

要创建的 Array 的第三维的大小。The size of the third dimension of the Array to create.

返回

每个维具有指定长度、使用从零开始的索引的指定 Type 的新三维 ArrayA new three-dimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

异常

elementType 不是有效的 TypeelementType is not a valid Type.

不支持 elementTypeelementType is not supported. 例如,不支持 VoidFor example, Void is not supported.

-or- elementType 为开放式泛型类型。elementType is an open generic type.

length1 小于零。length1 is less than zero.

-or- length2 小于零。length2 is less than zero.

- 或 --or- length3 小于零。length3 is less than zero.

示例

下面的代码示例演示如何创建和初始化三维 @no__t。The following code example shows how to create and initialize a three-dimensional Array.

using namespace System;
void PrintValues( Array^ myArr );
void main()
{
   // Creates and initializes a three-dimensional Array instance of type Object.
   Array^ my3DArray = Array::CreateInstance( Object::typeid, 2, 3, 4 );
   for ( int i = my3DArray->GetLowerBound( 0 ); i <= my3DArray->GetUpperBound( 0 ); i++ )
      for ( int j = my3DArray->GetLowerBound( 1 ); j <= my3DArray->GetUpperBound( 1 ); j++ )
         for ( int k = my3DArray->GetLowerBound( 2 ); k <= my3DArray->GetUpperBound( 2 ); k++ )
            my3DArray->SetValue( String::Concat( "abc", i, j, k ), i, j, k );
   
   // Displays the values of the Array.
   Console::WriteLine(  "The three-dimensional Array instance contains the following values:" );
   PrintValues( my3DArray );
}

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.
 
 The three-dimensional Array instance contains the following values:
     abc000    abc001    abc002    abc003
     abc010    abc011    abc012    abc013
     abc020    abc021    abc022    abc023
     abc100    abc101    abc102    abc103
     abc110    abc111    abc112    abc113
     abc120    abc121    abc122    abc123
 */
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a three-dimensional Array of type Object.
      Array my3DArray=Array.CreateInstance( typeof(Object), 2, 3, 4 );
      for ( int i = my3DArray.GetLowerBound(0); i <= my3DArray.GetUpperBound(0); i++ )
         for ( int j = my3DArray.GetLowerBound(1); j <= my3DArray.GetUpperBound(1); j++ )
            for ( int k = my3DArray.GetLowerBound(2); k <= my3DArray.GetUpperBound(2); k++ )
               my3DArray.SetValue( "abc" + i + j + k, i, j, k );

      // Displays the values of the Array.
      Console.WriteLine( "The three-dimensional Array contains the following values:" );
      PrintValues( my3DArray );
   }


   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.

The three-dimensional Array contains the following values:
    abc000    abc001    abc002    abc003
    abc010    abc011    abc012    abc013
    abc020    abc021    abc022    abc023
    abc100    abc101    abc102    abc103
    abc110    abc111    abc112    abc113
    abc120    abc121    abc122    abc123
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a three-dimensional Array of type Object.
        Dim my3DArray As Array = Array.CreateInstance(GetType(Object), 2, 3, 4)
        Dim i As Integer
        For i = my3DArray.GetLowerBound(0) To my3DArray.GetUpperBound(0)
            Dim j As Integer
            For j = my3DArray.GetLowerBound(1) To my3DArray.GetUpperBound(1)
                Dim k As Integer
                For k = my3DArray.GetLowerBound(2) To my3DArray.GetUpperBound(2)
                    my3DArray.SetValue("abc" + i.ToString() _
                       + j.ToString() + k.ToString(), i, j, k)
                Next k 
            Next j
        Next i

        ' Displays the values of the Array.
        Console.WriteLine("The three-dimensional Array contains the " _
           + "following values:")
        PrintValues(my3DArray)
    End Sub
    
    
    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The three-dimensional Array contains the following values:
'     abc000    abc001    abc002    abc003
'     abc010    abc011    abc012    abc013
'     abc020    abc021    abc022    abc023
'     abc100    abc101    abc102    abc103
'     abc110    abc111    abc112    abc113
'     abc120    abc121    abc122    abc123 

注解

与大多数类不同,Array 提供 @no__t 1 方法,而不是公共构造函数,以允许进行后期绑定访问。Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

引用类型的元素初始化为 nullReference-type elements are initialized to null. 值类型元素初始化为零。Value-type elements are initialized to zero.

此方法的运算复杂度为 O (n),其中 nlength1length2 和 @no__t 的乘积。This method is an O(n) operation, where n is the product of length1, length2, and length3.

适用于