Array.Resize ジェネリック メソッド
アセンブリ: mscorlib (mscorlib.dll 内)



このメソッドは、指定されたサイズに基づいて新しい配列用のメモリを確保し、古い配列から新しい配列へと要素をコピーして、古い配列を新しい配列で置き換えます。
array が null 参照 (Visual Basic では Nothing) の場合、指定されたサイズに基づいて新しい配列が作成されます。
newSize が古い配列の Length を超える場合は、新しい配列用にメモリが確保され、古い配列から新しい配列へと、すべての要素がコピーされます。newSize が、古い配列の Length よりも小さい場合は、新しい配列用にメモリが確保され、新しい配列の要素数の上限に達するまで、古い配列から新しい配列へと要素がコピーされます。コピーしきれなかった古い配列の要素は無視されます。newSize が古い配列の Length と等しい場合は、何も実行されません。

次の例では、サイズ変更によって、配列にどのような変化が生じるかを示しています。
Imports System Public Class SamplesArray Public Shared Sub Main() ' Create and initialize a new string array. Dim myArr As String() = {"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(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(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) End Sub 'Main Public Shared Sub PrintIndexAndValues(myArr() As String) Dim i As Integer For i = 0 To myArr.Length - 1 Console.WriteLine(" [{0}] : {1}", i, myArr(i)) Next i Console.WriteLine() End Sub 'PrintIndexAndValues End Class 'SamplesArray '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
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 */
using namespace System; static void PrintIndexAndValues(array<String^>^myArr) { for(int i = 0; i < myArr->Length; i++) { Console::WriteLine(L" [{0}] : {1}", i, myArr[i]); } Console::WriteLine(); } int main() { // Create and initialize a new string array. array<String^>^myArr = {L"The", L"quick", L"brown", L"fox", L"jumps", L"over", L"the", L"lazy", L"dog"}; // Display the values of the array. Console::WriteLine( L"The string array initially contains the following values:"); PrintIndexAndValues(myArr); // Resize the array to a bigger size (five elements larger). Array::Resize(myArr, myArr->Length + 5); // Display the values of the array. Console::WriteLine(L"After resizing to a larger size, "); Console::WriteLine(L"the string array contains the following values:"); PrintIndexAndValues(myArr); // Resize the array to a smaller size (four elements). Array::Resize(myArr, 4); // Display the values of the array. Console::WriteLine(L"After resizing to a smaller size, "); Console::WriteLine(L"the string array contains the following values:"); PrintIndexAndValues(myArr); return 1; } /* 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 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からArray.Resize ジェネリック メソッドを検索する場合は、下記のリンクをクリックしてください。

- Array.Resize ジェネリック メソッドのページへのリンク