Type と次元の長さを指定して、下限を持つ多次元の Array を作成します。
Overloads Public Shared Function CreateInstance( _
ByVal elementType As Type, _ ByVal lengths() As Integer, _ ByVal lowerBounds() As Integer _) As Array
[C#]
public static Array CreateInstance(TypeelementType,int[] lengths,int[] lowerBounds);
[C++]
public: static Array* CreateInstance(Type* elementType,intlengths __gc[],intlowerBounds __gc[]);
[JScript]
public static function CreateInstance(
elementType : Type,lengths : int[],lowerBounds : int[]) : Array;
パラメータ
- elementType
作成する Array の Type 。 - lengths
作成する Array の各次元のサイズを格納する 1 次元配列。 - lowerBounds
作成する Array の各次元の下限 (開始インデックス) を格納する 1 次元配列。
戻り値
次元ごとに指定した長さおよび下限を持つ、指定した Type の新しい多次元の Array 。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | elementType が null 参照 (Visual Basic では Nothing) です。
または lengths が null 参照 (Nothing) です。 または lowerBounds が null 参照 (Nothing) です。 |
ArgumentException | elementType が有効な Type ではありません。
または lengths 配列に含まれる要素が 1 つ未満です。 または lengths 配列と lowerBounds 配列の要素数が異なります。 |
NotSupportedException | elementType はサポートされていません。 |
ArgumentOutOfRangeException | lengths のいずれかの値が 0 未満です。
または lowerbounds のいずれかの値が非常に大きく、次元の下限と長さの合計が Int32.MaxValue を超えています。 |
解説
ほとんどのクラスとは異なり、 Array は、遅延バインディングによるアクセスを可能にするために、パブリック コンストラクタではなく CreateInstance メソッドを用意しています。
lengths 配列と lowerBounds 配列は同じ要素数である必要があります。 lengths 配列内の要素数が、新しい Array 内の次元数と等しい必要があります。
lengths 配列の各要素は、新しい Array 内で対応する次元の長さを指定する必要があります。
lowerBounds 配列の各要素は、新しい Array 内で対応する次元の下限を指定する必要があります。通常、.NET Framework クラス ライブラリと多くのプログラミング言語は、0 以外の下限を処理しません。
参照型の要素は、 null 参照 (Visual Basic では Nothing) に初期化されます。値型の要素は 0 に初期化されます。
使用例
指定した下限を使用して多次元 Array を作成および初期化する方法を次のコード例に示します。
Imports System
Imports Microsoft.VisualBasic
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
[C#]
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
*/
[C++]
#using <mscorlib.dll>
using namespace System;
void PrintValues( Array* myArr );
void main() {
// Creates and initializes a multidimensional Array instance of type String.
int myLengthsArray __gc[] = { 3, 5 };
int myBoundsArray __gc[] = { 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 __gc[] = { i, j };
myArray->SetValue( String::Concat(Convert::ToString(i), __box(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}", __box(i), __box(myArray->GetLowerBound(i)), __box(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
*/
[JScript]
import System;
// Creates and initializes a multidimensional Array of type String.
var myLengthsArray : int[] = [ 3, 5 ];
var myBoundsArray : int[] = [ 2, 3 ];
var myArray : System.Array = System.Array.CreateInstance( System.String, myLengthsArray, myBoundsArray );
for ( var i : int = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
for ( var j : int = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
var myIndicesArray : int[] = [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 ( var k : int = 0; k < myArray.Rank; k++ )
Console.WriteLine( "{0}:\t{1}\t{2}", k, myArray.GetLowerBound(k), myArray.GetUpperBound(k) );
// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintValues( myArray );
function PrintValues( myArr : System.Array) {
var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator();
var i : int = 0;
var cols : int = 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
*/
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard
参照
Array クラス | Array メンバ | System 名前空間 | Array.CreateInstance オーバーロードの一覧