英語で読む

次の方法で共有


Array.Resize<T>(T[], Int32) メソッド

定義

1 次元配列の要素数を、指定した新しいサイズに変更します。

C#
public static void Resize<T> (ref T[] array, int newSize);
C#
public static void Resize<T> (ref T[]? array, int newSize);

型パラメーター

T

配列要素の型。

パラメーター

array
T[]

サイズ変更の対象となる、インデックス番号が 0 から始まる 1 次元配列。指定したサイズの新しい配列を作成する場合は null

newSize
Int32

新しい配列のサイズ。

例外

newSize が 0 未満です。

次の例は、サイズ変更が配列に与える影響を示しています。

C#
using System;

public class SamplesArray
{
    public static void Main()  {

        // Create and initialize a new string array.
        String[] myArr = {"The", "quick", "brown", "fox", "jumps",
            "over", "the", "lazy", "dog"};

        // Display the values of the array.
        Console.WriteLine(
            "The string array initially contains the following values:");
        PrintIndexAndValues(myArr);

        // Resize the array to a bigger size (five elements larger).
        Array.Resize(ref myArr, myArr.Length + 5);

        // Display the values of the array.
        Console.WriteLine("After resizing to a larger size, ");
        Console.WriteLine("the string array contains the following values:");
        PrintIndexAndValues(myArr);

        // Resize the array to a smaller size (four elements).
        Array.Resize(ref myArr, 4);

        // Display the values of the array.
        Console.WriteLine("After resizing to a smaller size, ");
        Console.WriteLine("the string array contains the following values:");
        PrintIndexAndValues(myArr);
    }

    public static void PrintIndexAndValues(String[] myArr)  {
        for(int i = 0; i < myArr.Length; i++)
        {
            Console.WriteLine("   [{0}] : {1}", i, myArr[i]);
        }
        Console.WriteLine();
    }
}

/*
This code produces the following output.

The string array initially contains the following values:
   [0] : The
   [1] : quick
   [2] : brown
   [3] : fox
   [4] : jumps
   [5] : over
   [6] : the
   [7] : lazy
   [8] : dog

After resizing to a larger size,
the string array contains the following values:
   [0] : The
   [1] : quick
   [2] : brown
   [3] : fox
   [4] : jumps
   [5] : over
   [6] : the
   [7] : lazy
   [8] : dog
   [9] :
   [10] :
   [11] :
   [12] :
   [13] :

After resizing to a smaller size,
the string array contains the following values:
   [0] : The
   [1] : quick
   [2] : brown
   [3] : fox

*/

注釈

このメソッドは、指定したサイズの新しい配列を割り当て、古い配列から新しい配列に要素をコピーし、古い配列を新しい配列に置き換えます。 array は 1 次元配列である必要があります。

ある場合 array 、このメソッドは null、指定したサイズの新しい配列を作成します。

古い配列よりもLength大きい場合newSizeは、新しい配列が割り当てられ、すべての要素が古い配列から新しい配列にコピーされます。 古い配列よりLength小さい場合newSizeは、新しい配列が割り当てられ、新しい配列が塗りつぶされるまで古い配列から新しい配列に要素がコピーされます。古い配列の残りの要素は無視されます。 古い配列と等しいLength場合newSize、このメソッドは何も行いません。

このメソッドは O(n) 操作です。nnewSize です。

このメソッドは Resize 、1 次元配列のサイズのみを変更します。 このクラスには Array 、多次元配列のサイズを変更するためのメソッドは含まれません。 これを行うには、独自のコードを指定するか、サードパーティライブラリで特別な目的のメソッドを呼び出す必要があります。 次のコードは、 n 次元の配列のサイズを変更するメソッドに対して可能な実装の 1 つを示しています。

C#
using System;

public class Example
{
   public static void Main()
   {
      int[,] arr = new int[10,2];
      for (int n1 = 0; n1 <= arr.GetUpperBound(0); n1++) {
         arr[n1, 0] = n1;
         arr[n1, 1] = n1 * 2;
      }

      // Make a 2-D array larger in the first dimension.
      arr = (int[,]) ResizeArray(arr, new int[] { 12, 2} );
      for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++)
         Console.WriteLine("{0}: {1}, {2}", ctr, arr[ctr, 0], arr[ctr, 1]);
      Console.WriteLine();

      // Make a 2-D array smaller in the first dimension.
      arr = (int[,]) ResizeArray(arr, new int[] { 2, 2} );
      for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++)
         Console.WriteLine("{0}: {1}, {2}", ctr, arr[ctr, 0], arr[ctr, 1]);
   }

   private static Array ResizeArray(Array arr, int[] newSizes)
   {
      if (newSizes.Length != arr.Rank)
         throw new ArgumentException("arr must have the same number of dimensions " +
                                     "as there are elements in newSizes", "newSizes");

      var temp = Array.CreateInstance(arr.GetType().GetElementType(), newSizes);
      int length = arr.Length <= temp.Length ? arr.Length : temp.Length;
      Array.ConstrainedCopy(arr, 0, temp, 0, length);
      return temp;
   }
}
// The example displays the following output:
//       0: 0, 0
//       1: 1, 2
//       2: 2, 4
//       3: 3, 6
//       4: 4, 8
//       5: 5, 10
//       6: 6, 12
//       7: 7, 14
//       8: 8, 16
//       9: 9, 18
//       10: 0, 0
//       11: 0, 0
//
//       0: 0, 0
//       1: 1, 2

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0