Array.Resize ジェネリック メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Array.Resize ジェネリック メソッドの意味・解説 

Array.Resize ジェネリック メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

配列サイズを、新たに指定したサイズ変更します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Sub Resize(Of
 T) ( _
    ByRef array As T(), _
    newSize As Integer _
)
Dim array As T()
Dim newSize As Integer

Array.Resize(array, newSize)
public static void Resize<T>
 (
    ref T[] array,
    int newSize
)
public:
generic<typename T>
static void Resize (
    array<T>^% array, 
    int newSize
)
J# では、ジェネリックな型およびメソッド使用できますが、新規に宣言することはできません。
JScript では、ジェネリックな型およびメソッド使用できません。

型パラメータ

T

配列要素の型。

パラメータ

array

サイズ変更対象となる、インデックス番号が 0 から始まる 1 次元配列指定したサイズ新し配列作成する場合null 参照 (Visual Basic では Nothing)。

newSize

新し配列サイズ

例外例外
例外種類条件

ArgumentOutOfRangeException

newSize が 0 未満です。

解説解説

このメソッドは、指定されサイズ基づいて新し配列用のメモリ確保し、古い配列から新し配列へと要素コピーして、古い配列新し配列置き換えます

arraynull 参照 (Visual Basic では Nothing) の場合指定されサイズ基づいて新し配列作成されます。

newSize が古い配列Length超える場合は、新し配列用にメモリ確保され、古い配列から新し配列へと、すべての要素コピーされます。newSize が、古い配列Length よりも小さ場合は、新し配列用にメモリ確保され新し配列要素の上限に達するまで、古い配列から新し配列へと要素コピーされます。コピーしきれなかった古い配列要素無視されます。newSize が古い配列Length等し場合は、何も実行されません。

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

使用例使用例

次の例では、サイズ変更によって、配列どのような変化生じるかを示してます。

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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

Array.Resize ジェネリック メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Array.Resize ジェネリック メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS