共用方式為


Array.Resize<T> Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Changes the number of elements of an array to the specified new size.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Sub Resize(Of T) ( _
    ByRef array As T(), _
    newSize As Integer _
)
public static void Resize<T>(
    ref T[] array,
    int newSize
)

Type Parameters

  • T
    The type of the elements of the array.

Parameters

  • array
    Type: array<T[]%
    The one-dimensional, zero-based array to resize, or nulla null reference (Nothing in Visual Basic) to create a new array with the specified size.

Exceptions

Exception Condition
ArgumentOutOfRangeException

newSize is less than zero.

Remarks

This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.

If array is nulla null reference (Nothing in Visual Basic), this method creates a new array with the specified size.

If newSize is greater than the Length of the old array, a new array is allocated and all the elements are copied from the old array to the new one. If newSize is less than the Length of the old array, a new array is allocated and elements are copied from the old array to the new one until the new one is filled; the rest of the elements in the old array are ignored. If newSize is equal to the Length of the old array, this method does nothing.

This method is an O(n) operation, where n is newSize.

Examples

The following example shows how resizing affects the array.


Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' 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.
      outputBlock.Text &= _
          "The string array initially contains the following values:" & vbCrLf
      PrintIndexAndValues(outputBlock, myArr)

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

      ' Display the values of the array.
      outputBlock.Text &= String.Format("After resizing to a larger size, ") & vbCrLf
      outputBlock.Text &= "the string array contains the following values:" & vbCrLf
      PrintIndexAndValues(outputBlock, myArr)

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

      ' Display the values of the array.
      outputBlock.Text &= String.Format("After resizing to a smaller size, ") & vbCrLf
      outputBlock.Text &= "the string array contains the following values:" & vbCrLf
      PrintIndexAndValues(outputBlock, myArr)

   End Sub 'Main

   Public Shared Sub PrintIndexAndValues(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal myArr() As String)
      Dim i As Integer
      For i = 0 To myArr.Length - 1
         outputBlock.Text &= String.Format("   [{0}] : {1}", i, myArr(i)) & vbCrLf
      Next i
      outputBlock.Text &= vbCrLf
   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 Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

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

      // Display the values of the array.
      outputBlock.Text +=
          "The string array initially contains the following values:" + "\n";
      PrintIndexAndValues(outputBlock, myArr);

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

      // Display the values of the array.
      outputBlock.Text += String.Format("After resizing to a larger size, ") + "\n";
      outputBlock.Text += "the string array contains the following values:" + "\n";
      PrintIndexAndValues(outputBlock, myArr);

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

      // Display the values of the array.
      outputBlock.Text += String.Format("After resizing to a smaller size, ") + "\n";
      outputBlock.Text += "the string array contains the following values:" + "\n";
      PrintIndexAndValues(outputBlock, myArr);
   }

   public static void PrintIndexAndValues(System.Windows.Controls.TextBlock outputBlock, String[] myArr)
   {
      for (int i = 0; i < myArr.Length; i++)
      {
         outputBlock.Text += String.Format("   [{0}] : {1}", i, myArr[i]) + "\n";
      }
      outputBlock.Text += "\n";
   }
}

/* 
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

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference