次の方法で共有


Array クラス

配列の作成、操作、検索、および並べ替えを行うメソッドを提供します。これにより、共通言語ランタイムのすべての配列の基本クラスとして機能します。

この型のすべてのメンバの一覧については、Array メンバ を参照してください。

System.Object
   System.Array

<Serializable>
MustInherit Public Class Array   Implements ICloneable, IList, ICollection, IEnumerable
[C#]
[Serializable]
public abstract class Array : ICloneable, IList, ICollection,   IEnumerable
[C++]
[Serializable]
public __gc __abstract class Array : public ICloneable, IList,   ICollection, IEnumerable
[JScript]
public
   Serializable
abstract class Array implements ICloneable, IList,   ICollection, IEnumerable

スレッドセーフ

この型の public static (Visual Basic では Shared) メンバは、マルチスレッド操作に対して安全です。インスタンス メンバがスレッド セーフになるかどうかは保証されていません。

この実装は、 Array 用の同期された (スレッド セーフな) ラッパーは提供しませんが、 Array に基づく .NET Framework クラスでは、 SyncRoot プロパティを使用して、独自にコレクションを同期させることができます。

コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

解説

Array クラスは、配列をサポートする言語による各実装の基本クラスです。ただし、システムとコンパイラに限り、 Array クラスから明示的に派生させることができます。開発者は、言語によって提供される配列構造を使用する必要があります。

要素は Array 内の値です。 Array の長さは、その配列に含まれる要素の合計数です Array のランクは、 Array の次元数です。 Array の次元の下限は、 Array のその次元の開始インデックスです。多次元 Array では次元ごとに下限が異なることがあります。

Type オブジェクトは、配列型宣言の情報を提供します。配列型が同じである Array オブジェクトは、同じ Type オブジェクトを共有します。

配列を Array 型にキャストした場合に、結果が配列ではなくオブジェクトになることもあるため、 Type.IsArray および Type.GetElementType は、 Array について予測される結果を返さない場合があります。つまり、 typeof(System.Array).IsArrayfalse を返し、 typeof(System.Array).GetElementType は null 参照 (Visual Basic では Nothing) を返します。

ほとんどのクラスとは異なり、 Array は、遅延バインディングによるアクセスを可能にするために、パブリック コンストラクタではなく CreateInstance メソッドを用意しています。

Array.Copy メソッドでは、同じ型の配列間だけでなく、異なる型の標準配列間でも要素がコピーされます。つまり、自動的に型キャストが処理されます。

CreateInstanceCopyCopyToGetValue および SetValue など、メソッドによっては大容量の配列に対応するために 64 ビット整数をパラメータとして受け取るオーバーロードが用意されています。 LongLength および GetLongLength は、配列の長さを示す 64 ビット整数を返します。

使用例

Array.Copy メソッドを使用して、整数型の配列と Object 型の配列間で要素をコピーする方法を次のコード例で示します。

 
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new integer array and a new Object array.
        Dim myIntArray() As Integer = {1, 2, 3, 4, 5}
        Dim myObjArray() As Object = {26, 27, 28, 29, 30}
        
        ' Prints the initial values of both arrays.
        Console.WriteLine("Initially,")
        Console.Write("integer array:")
        PrintValues(myIntArray)
        Console.Write("Object array: ")
        PrintValues(myObjArray)
        
        ' Copies the first two elements from the integer array to the Object array.
        Array.Copy(myIntArray, myObjArray, 2)
        
        ' Prints the values of the modified arrays.
        Console.WriteLine(ControlChars.NewLine + "After copying the first two" _
           + " elements of the integer array to the Object array,")
        Console.Write("integer array:")
        PrintValues(myIntArray)
        Console.Write("Object array: ")
        PrintValues(myObjArray)
        
        ' Copies the last two elements from the Object array to the integer array.
        Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, _
           myIntArray.GetUpperBound(0) - 1, 2)
        
        ' Prints the values of the modified arrays.
        Console.WriteLine(ControlChars.NewLine + "After copying the last two" _
           + " elements of the Object array to the integer array,")
        Console.Write("integer array:")
        PrintValues(myIntArray)
        Console.Write("Object array: ")
        PrintValues(myObjArray)
    End Sub
    
    Overloads Public Shared Sub PrintValues(myArr() As Object)
        Dim i As Object
        For Each i In  myArr
            Console.Write(ControlChars.Tab + "{0}", i)
        Next i
        Console.WriteLine()
    End Sub    
    
    Overloads Public Shared Sub PrintValues(myArr() As Integer)
        Dim i As Integer
        For Each i In  myArr
            Console.Write(ControlChars.Tab + "{0}", i)
        Next i
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Initially,
' integer array:  1       2       3       4       5
' Object array:   26      27      28      29      30
' 
' After copying the first two elements of the integer array to the Object array,
' integer array:  1       2       3       4       5
' Object array:   1       2       28      29      30
' 
' After copying the last two elements of the Object array to the integer array,
' integer array:  1       2       3       29      30
' Object array:   1       2       28      29      30

[C#] 
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new integer array and a new Object array.
      int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
      Object[] myObjArray = new Object[5] { 26, 27, 28, 29, 30 };

      // Prints the initial values of both arrays.
      Console.WriteLine( "Initially," );
      Console.Write( "integer array:" );
      PrintValues( myIntArray );
      Console.Write( "Object array: " );
      PrintValues( myObjArray );

      // Copies the first two elements from the integer array to the Object array.
      Array.Copy( myIntArray, myObjArray, 2 );

      // Prints the values of the modified arrays.
      Console.WriteLine( "\nAfter copying the first two elements of the integer array to the Object array," );
      Console.Write( "integer array:" );
      PrintValues( myIntArray );
      Console.Write( "Object array: " );
      PrintValues( myObjArray );

      // Copies the last two elements from the Object array to the integer array.
      Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

      // Prints the values of the modified arrays.
      Console.WriteLine( "\nAfter copying the last two elements of the Object array to the integer array," );
      Console.Write( "integer array:" );
      PrintValues( myIntArray );
      Console.Write( "Object array: " );
      PrintValues( myObjArray );
   }


   public static void PrintValues( Object[] myArr )  {
      foreach ( Object i in myArr )  {
         Console.Write( "\t{0}", i );
      }
      Console.WriteLine();
   }

   public static void PrintValues( int[] myArr )  {
      foreach ( int i in myArr )  {
         Console.Write( "\t{0}", i );
      }
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Initially,
integer array:  1       2       3       4       5
Object array:   26      27      28      29      30

After copying the first two elements of the integer array to the Object array,
integer array:  1       2       3       4       5
Object array:   1       2       28      29      30

After copying the last two elements of the Object array to the integer array,
integer array:  1       2       3       29      30
Object array:   1       2       28      29      30
*/

[C++] 
#using <mscorlib.dll>
using namespace System;

void main1();
void main2();

void main(){
    main1();
    
    Console::WriteLine();
    
    main2();
}

void PrintValues( Object* myArr __gc[]);
void PrintValues( int myArr __gc[]);

void main1()  {
    // Creates and initializes a new int array and a new Object array.
    int myIntArray __gc[] = { 1, 2, 3, 4, 5 };
    
    Object* myObjArray __gc[] = { __box(26), __box(27), __box(28), __box(29), __box(30)};    
 
    // Prints the initial values of both arrays.
    Console::WriteLine( "Initially," );
    Console::Write( "int array:   " );
    PrintValues( myIntArray );
    Console::Write( "Object array:" );
    PrintValues( myObjArray );
 
    // Copies the first two elements from the int array to the Object array.
    Array::Copy( myIntArray, myObjArray, 2 );
 
    // Prints the values of the modified arrays.
    Console::WriteLine( "\nAfter copying the first two elements of the int array to the Object array," );
    Console::Write( "int array:   " );
    PrintValues( myIntArray );
    Console::Write( "Object array:" );
    PrintValues( myObjArray );
 
    // Copies the last two elements from the Object array to the int array.
    Array::Copy( myObjArray, myObjArray->GetUpperBound(0) - 1, myIntArray, myIntArray->GetUpperBound(0) - 1, 2 );
 
    // Prints the values of the modified arrays.
    Console::WriteLine( "\nAfter copying the last two elements of the Object array to the int array," );
    Console::Write( "int array:   " );
    PrintValues( myIntArray );
    Console::Write( "Object array:" );
    PrintValues( myObjArray );
}
 
 
void PrintValues( Object* myArr __gc[] )  {
    for(int i = 0; i< myArr->Length; i++){
       Console::Write( "\t{0}", myArr[i] );
    }
    Console::WriteLine();
}
 
void PrintValues( int myArr __gc[])  {
    for(int i = 0; i< myArr->Length; i++) {
       Console::Write("\t{0}", __box( myArr[i]) );
    }
    Console::WriteLine();
}
 /* 
 This code produces the following output.
 
 Initially,
 int array:       1    2    3    4    5
 Object array:    26    27    28    29    30
 After copying the first two elements of the int array to the Object array,
 int array:       1    2    3    4    5
 Object array:    1    2    28    29    30
 After copying the last two elements of the Object array to the int array,
 int array:       1    2    3    29    30
 Object array:    1    2    28    29    30
 */

[JScript] 
public class SamplesArray  {

   public static function Main()  {

      // Creates and initializes a new integer array and a new Object array.
      var myIntArray : int[] = [ 1, 2, 3, 4, 5 ];
      var myObjArray : Object[] = [26, 27, 28, 29, 30 ];

      // Prints the initial values of both arrays.
      Console.WriteLine( "Initially," );
      Console.Write( "integer array:" );
      PrintValues( myIntArray );
      Console.Write( "Object array: " );
      PrintValues( myObjArray );

      // Copies the first two elements from the integer array to the Object array.
      System.Array.Copy( myIntArray, myObjArray, 2 );

      // Prints the values of the modified arrays.
      Console.WriteLine( "\nAfter copying the first two elements of the integer array to the Object array," );
      Console.Write( "integer array:" );
      PrintValues( myIntArray );
      Console.Write( "Object array: " );
      PrintValues( myObjArray );

      // Copies the last two elements from the Object array to the integer array.
      System.Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

      // Prints the values of the modified arrays.
      Console.WriteLine( "\nAfter copying the last two elements of the Object array to the integer array," );
      Console.Write( "integer array:" );
      PrintValues( myIntArray );
      Console.Write( "Object array: " );
      PrintValues( myObjArray );
   }

   public static 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.

Initially,
integer array:  1       2       3       4       5
Object array:   26      27      28      29      30

After copying the first two elements of the integer array to the Object array,
integer array:  1       2       3       4       5
Object array:   1       2       28      29      30

After copying the last two elements of the Object array to the integer array,
integer array:  1       2       3       29      30
Object array:   1       2       28      29      30
*/

Array を作成および初期化し、そのプロパティと要素を表示するコード例を次に示します。

 
Public Class SamplesArray2    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new three-dimensional Array of
        ' type Int32.
        Dim myArr As Array = Array.CreateInstance(GetType(Int32), 2, 3, 4)
        Dim i As Integer
        For i = myArr.GetLowerBound(0) To myArr.GetUpperBound(0)
            Dim j As Integer
            For j = myArr.GetLowerBound(1) To myArr.GetUpperBound(1)
                Dim k As Integer
                For k = myArr.GetLowerBound(2) To myArr.GetUpperBound(2)
                    myArr.SetValue(i * 100 + j * 10 + k, i, j, k)
                Next k
            Next j 
        Next i ' Displays the properties of the Array.
        Console.WriteLine("The Array has {0} dimension(s) and a " _
           + "total of {1} elements.", myArr.Rank, myArr.Length)
        Console.WriteLine(ControlChars.Tab + "Length" + ControlChars.Tab _
           + "Lower" + ControlChars.Tab + "Upper")
        For i = 0 To myArr.Rank - 1
            Console.Write("{0}:" + ControlChars.Tab + "{1}", i, _
               myArr.GetLength(i))
            Console.WriteLine(ControlChars.Tab + "{0}" + ControlChars.Tab _
               + "{1}", myArr.GetLowerBound(i), myArr.GetUpperBound(i))
        Next i
        
        ' Displays the contents of the Array.
        Console.WriteLine("The Array contains the following values:")
        PrintValues(myArr)
    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 Array has 3 dimension(s) and a total of 24 elements.
'     Length    Lower    Upper
' 0:    2    0    1
' 1:    3    0    2
' 2:    4    0    3
' The Array contains the following values:
'     0    1    2    3
'     10    11    12    13
'     20    21    22    23
'     100    101    102    103
'     110    111    112    113
'     120    121    122    123 

[C#] 
public class SamplesArray2{

   public static void Main()  {

      // Creates and initializes a new three-dimensional Array of type Int32.
      Array myArr = Array.CreateInstance( typeof(Int32), 2, 3, 4 );
      for ( int i = myArr.GetLowerBound(0); i <= myArr.GetUpperBound(0); i++ )
         for ( int j = myArr.GetLowerBound(1); j <= myArr.GetUpperBound(1); j++ )
            for ( int k = myArr.GetLowerBound(2); k <= myArr.GetUpperBound(2); k++ )  {
               myArr.SetValue( (i*100)+(j*10)+k, i, j, k );
            }

      // Displays the properties of the Array.
      Console.WriteLine( "The Array has {0} dimension(s) and a total of {1} elements.", myArr.Rank, myArr.Length );
      Console.WriteLine( "\tLength\tLower\tUpper" );
      for ( int i = 0; i < myArr.Rank; i++ )  {
         Console.Write( "{0}:\t{1}", i, myArr.GetLength(i) );
         Console.WriteLine( "\t{0}\t{1}", myArr.GetLowerBound(i), myArr.GetUpperBound(i) );
      }

      // Displays the contents of the Array.
      Console.WriteLine( "The Array contains the following values:" );
      PrintValues( myArr );
   }


   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 Array has 3 dimension(s) and a total of 24 elements.
    Length    Lower    Upper
0:    2    0    1
1:    3    0    2
2:    4    0    3
The Array contains the following values:
    0    1    2    3
    10    11    12    13
    20    21    22    23
    100    101    102    103
    110    111    112    113
    120    121    122    123
*/

[C++] 
void PrintValues( Array* myArr );

void main2()  {
    // Creates and initializes a new three-dimensional Array instance of type Int32.
    Array* myArr = Array::CreateInstance( __typeof(Int32), 2, 3, 4 );

    for ( int i = myArr->GetLowerBound(0); i <= myArr->GetUpperBound(0); i++ )
    {
        for ( int j = myArr->GetLowerBound(1); j <= myArr->GetUpperBound(1); j++ )
        {
            for ( int k = myArr->GetLowerBound(2); k <= myArr->GetUpperBound(2); k++ )
                myArr->SetValue( __box((i*100)+(j*10)+k), i, j, k );
        }
    }
 
    // Displays the properties of the Array.
    Console::WriteLine( "The Array instance has {0} dimension(s) and a total of {1} elements.", __box(myArr->Rank), __box(myArr->Length) );
    Console::WriteLine( "\tLength\tLower\tUpper" );
    for ( int i = 0; i < myArr->Rank; i++ )  {
        Console::Write( "{0}:\t{1}", __box(i), __box(myArr->GetLength(i)) );
        Console::WriteLine( "\t{0}\t{1}", __box(myArr->GetLowerBound(i)), __box(myArr->GetUpperBound(i)) );
    }
    // Displays the contents of the Array.
    Console::WriteLine( "The Array instance contains the following values:" );
    PrintValues( myArr );
}
 
 
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 Array instance has 3 dimension(s) and a total of 24 elements.
     Length    Lower    Upper
 0:    2    0    1
 1:    3    0    2
 2:    4    0    3
 The Array instance contains the following values:
     0    1    2    3
     10    11    12    13
     20    21    22    23
     100    101    102    103
     110    111    112    113
     120    121    122    123
 */

[JScript] 
public class SamplesArray2{

   public static function Main()  {

      // Creates and initializes a new three-dimensional Array of type Int32.
      var myArr : System.Array = System.Array.CreateInstance( Int32, 2, 3, 4 );
      for ( var i : int = myArr.GetLowerBound(0); i <= myArr.GetUpperBound(0); i++ )
         for ( var j : int = myArr.GetLowerBound(1); j <= myArr.GetUpperBound(1); j++ )
            for ( var k : int = myArr.GetLowerBound(2); k <= myArr.GetUpperBound(2); k++ )  {
               myArr.SetValue( Int32((i*100)+(j*10)+k), i, j, k );
            }

      // Displays the properties of the Array.
      Console.WriteLine( "The Array has {0} dimension(s) and a total of {1} elements.", myArr.Rank, myArr.Length );
      Console.WriteLine( "\tLength\tLower\tUpper" );
      for ( var l : int = 0; l < myArr.Rank; l++ )  {
         Console.Write( "{0}:\t{1}", l, myArr.GetLength(l) );
         Console.WriteLine( "\t{0}\t{1}", myArr.GetLowerBound(l), myArr.GetUpperBound(l) );
      }

      // Displays the contents of the Array.
      Console.WriteLine( "The Array contains the following values:" );
      PrintValues( myArr );
   }


   public static 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.

The Array has 3 dimension(s) and a total of 24 elements.
    Length    Lower    Upper
0:    2    0    1
1:    3    0    2
2:    4    0    3
The Array contains the following values:
    0    1    2    3
    10    11    12    13
    20    21    22    23
    100    101    102    103
    110    111    112    113
    120    121    122    123
*/

必要条件

名前空間: System

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

Array メンバ | System 名前空間 | Copy | Object | Type | カルチャを認識しない配列の操作の実行