Array.Clear 方法

定义

重载

Clear(Array)

清除数组的内容。

Clear(Array, Int32, Int32)

将数组中的某个范围的元素设置为每个元素类型的默认值。

Clear(Array)

Source:
Array.CoreCLR.cs
Source:
Array.CoreCLR.cs
Source:
Array.CoreCLR.cs

清除数组的内容。

public:
 static void Clear(Array ^ array);
public static void Clear (Array array);
static member Clear : Array -> unit
Public Shared Sub Clear (array As Array)

参数

array
Array

要清除的数组。

例外

array 上声明的默认值为 null

适用于

Clear(Array, Int32, Int32)

Source:
Array.CoreCLR.cs
Source:
Array.CoreCLR.cs
Source:
Array.CoreCLR.cs

将数组中的某个范围的元素设置为每个元素类型的默认值。

public:
 static void Clear(Array ^ array, int index, int length);
public static void Clear (Array array, int index, int length);
static member Clear : Array * int * int -> unit
Public Shared Sub Clear (array As Array, index As Integer, length As Integer)

参数

array
Array

需要清除其元素的数组。

index
Int32

要清除的一系列元素的起始索引。

length
Int32

要清除的元素数。

例外

arraynull

index 小于 array 的下限。

- 或 -

length 小于零。

- 或 -

indexlength 大于 array 的大小。

示例

以下示例使用 Clear 方法重置一维、二维和三维数组中的整数值。

using System;

class Example
{
    public static void Main()
    {
        Console.WriteLine("One dimension (Rank=1):");
        int[] numbers1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        for (int i = 0; i < 9; i++)
        {
            Console.Write("{0} ", numbers1[i]);
        }
        Console.WriteLine();
        Console.WriteLine();

        Console.WriteLine("Array.Clear(numbers1, 2, 5)");
        Array.Clear(numbers1, 2, 5);

        for (int i = 0; i < 9; i++)
        {
            Console.Write("{0} ", numbers1[i]);
        }
        Console.WriteLine();
        Console.WriteLine();

        Console.WriteLine("Two dimensions (Rank=2):");
        int[,] numbers2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.Write("{0} ", numbers2[i, j]);
            }
            Console.WriteLine();
        }

        Console.WriteLine();
        Console.WriteLine("Array.Clear(numbers2, 2, 5)");
        Array.Clear(numbers2, 2, 5);

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.Write("{0} ", numbers2[i, j]);
            }
            Console.WriteLine();
        }

        Console.WriteLine("Three dimensions (Rank=3):");
        int[, ,] numbers3 = {{{1, 2}, {3, 4}},
                             {{5, 6}, {7, 8}},
                             {{9, 10}, {11, 12}}};

        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                for (int k = 0; k < 2; k++)
                {
                    Console.Write("{0} ", numbers3[i, j, k]);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        Console.WriteLine("Array.Clear(numbers3, 2, 5)");
        Array.Clear(numbers3, 2, 5);

        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                for (int k = 0; k < 2; k++)
                {
                    Console.Write("{0} ", numbers3[i, j, k]);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
}
/*  This code example produces the following output:
 *
 * One dimension (Rank=1):
 * 1 2 3 4 5 6 7 8 9
 *
 * Array.Clear(numbers1, 2, 5)
 * 1 2 0 0 0 0 0 8 9
 *
 * Two dimensions (Rank=2):
 * 1 2 3
 * 4 5 6
 * 7 8 9
 *
 * Array.Clear(numbers2, 2, 5)
 * 1 2 0
 * 0 0 0
 * 0 8 9
 *
 * Three dimensions (Rank=3):
 * 1 2
 * 3 4
 *
 * 5 6
 * 7 8
 *
 * Array.Clear(numbers3, 2, 5)
 * 1 2
 * 0 0
 *
 * 0 0
 * 0 8
 */
open System

printfn "One dimension (Rank=1):"
let numbers1 = [| 1..9 |]

for i in numbers1 do
    printf $"{i} " 
printfn "\n\nArray.Clear(numbers1, 2, 5)"

Array.Clear(numbers1, 2, 5)

for i in numbers1 do
    printf $"{i} " 

printfn "\n\nTwo dimensions (Rank=2):"

let numbers2 = array2D [ [ 1; 2; 3 ]; [ 4; 5; 6 ]; [ 7; 8; 9 ] ]

for i = 0 to 2 do
    for j = 0 to 2 do
        printfn $"{numbers2[i, j]} "
    printfn ""

printfn "\nArray.Clear(numbers2, 2, 5)"
Array.Clear(numbers2, 2, 5)

for i = 0 to 2 do
    for j = 0 to 2 do
        printfn $"{numbers2[i, j]} "
    printfn ""

printfn "Three dimensions (Rank=3):"
let numbers3 = Array3D.zeroCreate 2 2 2
numbers3[0, 0, 0] <- 1
numbers3[0, 0, 1] <- 2
numbers3[0, 1, 0] <- 3
numbers3[0, 1, 1] <- 4
numbers3[1, 0, 0] <- 5
numbers3[1, 1, 0] <- 7
numbers3[1, 0, 1] <- 6
numbers3[1, 1, 1] <- 8

for i = 0 to 1 do
    for j = 0 to 1 do
        for k = 0 to 1 do
            printf $"{numbers3[i, j, k]} "
        printfn ""
    printfn ""

printfn "Array.Clear(numbers3, 2, 5)"
Array.Clear(numbers3, 2, 5)

for i = 0 to 1 do
    for j = 0 to 1 do
        for k = 0 to 1 do
            printf $"{numbers3[i, j, k]} "
        printfn ""
    printfn ""

//  This code example produces the following output:
//
// One dimension (Rank=1):
// 1 2 3 4 5 6 7 8 9
//
// Array.Clear(numbers1, 2, 5)
// 1 2 0 0 0 0 0 8 9
//
// Two dimensions (Rank=2):
// 1 2 3
// 4 5 6
// 7 8 9
//
// Array.Clear(numbers2, 2, 5)
// 1 2 0
// 0 0 0
// 0 8 9
//
// Three dimensions (Rank=3):
// 1 2
// 3 4
//
// 5 6
// 7 8
//
// Array.Clear(numbers3, 2, 5)
// 1 2
// 0 0
//
// 0 0
// 0 8
//
Module Example
    Sub Main()
        Console.WriteLine(vbLf & "One dimension (Rank=1):")
        Dim numbers1() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9}

        For i As Integer = 0 To 8
            Console.Write("{0} ", numbers1(i))
        Next 
        Console.WriteLine()

        Console.WriteLine(vbLf & "Array.Clear(numbers1, 2, 5)")
        Array.Clear(numbers1, 2, 5)

        For i As Integer = 0 To 8
            Console.Write("{0} ", numbers1(i))
        Next 
        Console.WriteLine()


        Console.WriteLine(vbLf & "Two dimensions (Rank=2):")
        Dim numbers2(,) As Integer = {{ 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }}

        For i As Integer = 0 To 2
            For j As Integer = 0 To 2
                Console.Write("{0} ", numbers2(i, j))
            Next 
            Console.WriteLine()
        Next 

        Console.WriteLine(vbLf & "Array.Clear(numbers2, 2, 5)")
        Array.Clear(numbers2, 2, 5)

        For i As Integer = 0 To 2
            For j As Integer = 0 To 2
                Console.Write("{0} ", numbers2(i, j))
            Next 
            Console.WriteLine()
        Next 


        Console.WriteLine(vbLf & "Three dimensions (Rank=3):")
        Dim numbers3(,,) As Integer = {{{ 1, 2 }, { 3, 4 }}, _
                                       {{ 5, 6 }, { 7, 8 }}, _
                                       {{ 9, 10 }, { 11, 12 }}}

        For i As Integer = 0 To 1
            For j As Integer = 0 To 1
                For k As Integer = 0 To 1
                    Console.Write("{0} ", numbers3(i, j, k))
                Next 
                Console.WriteLine()
            Next 
            Console.WriteLine()
        Next 
        Console.WriteLine()
        
        Console.WriteLine("Array.Clear(numbers3, 2, 5)")
        Array.Clear(numbers3, 2, 5)

        For i As Integer = 0 To 1
            For j As Integer = 0 To 1
                For k As Integer = 0 To 1
                    Console.Write("{0} ", numbers3(i, j, k))
                Next 
                Console.WriteLine()
            Next 
            Console.WriteLine()
        Next 
    End Sub
End Module
' The example displays the following output:
'       One dimension (Rank=1):
'       1 2 3 4 5 6 7 8 9
'       
'       Array.Clear(numbers1, 2, 5)
'       1 2 0 0 0 0 0 8 9
'       
'       Two dimensions (Rank=2):
'       1 2 3
'       4 5 6
'       7 8 9
'       
'       Array.Clear(numbers2, 2, 5)
'       1 2 0
'       0 0 0
'       0 8 9
'       
'       Three dimensions (Rank=3):
'       1 2
'       3 4
'
'       5 6
'       7 8
'       
'       Array.Clear(numbers3, 2, 5)
'       1 2
'       0 0
'       
'       0 0
'       0 8

以下示例定义包含 TimeZoneTimeTimeZoneInfo 字段和 字段的 DateTimeOffset 结构。 然后, Clear 它调用 方法以清除值两个元素数组中的 TimeZoneTime 一个元素。 方法将清除元素的值设置为 对象的默认值TimeZoneInfo(为 null),并将 对象的DateTimeOffset.MinValue默认值DateTimeOffset设置为 。

using System;

public struct TimeZoneTime
{
   private DateTimeOffset dt;
   private TimeZoneInfo tz;

   public TimeZoneTime(DateTimeOffset dateTime, TimeZoneInfo timeZone)
   {
      dt = dateTime;
      tz = timeZone;
   }

   public DateTimeOffset DateTime
   { get { return dt; } }

   public TimeZoneInfo TimeZone
   { get { return tz; } }
}

public class Example
{
   public static void Main()
   {
      // Declare an array with two elements.
      TimeZoneTime[] timeZoneTimes = { new TimeZoneTime(DateTime.Now, TimeZoneInfo.Local),
                                       new TimeZoneTime(DateTime.Now, TimeZoneInfo.Utc) };
      foreach (var timeZoneTime in timeZoneTimes)
         Console.WriteLine("{0}: {1:G}",
                           timeZoneTime.TimeZone == null ? "<null>" : timeZoneTime.TimeZone.ToString(),
                           timeZoneTime.DateTime);
      Console.WriteLine();

      Array.Clear(timeZoneTimes, 1, 1);
      foreach (var timeZoneTime in timeZoneTimes)
         Console.WriteLine("{0}: {1:G}",
                           timeZoneTime.TimeZone == null ? "<null>" : timeZoneTime.TimeZone.ToString(),
                           timeZoneTime.DateTime);
   }
}
// The example displays the following output:
//       (UTC-08:00) Pacific Time (US & Canada): 1/20/2014 12:11:00 PM
//       UTC: 1/20/2014 12:11:00 PM
//
//       (UTC-08:00) Pacific Time (US & Canada): 1/20/2014 12:11:00 PM
//       <null>: 1/1/0001 12:00:00 AM
open System

[<Struct>]
type TimeZoneTime =
    { DateTime: DateTimeOffset
      TimeZone: TimeZoneInfo }

// Declare an array with two elements.
let timeZoneTimes = 
    [| { DateTime = DateTimeOffset.Now; TimeZone = TimeZoneInfo.Local }
       { DateTime = DateTimeOffset.Now; TimeZone = TimeZoneInfo.Local } |]

for timeZoneTime in timeZoneTimes do
    let tz = if isNull timeZoneTime.TimeZone then "<null>" else string timeZoneTime.TimeZone
    printfn $"{tz}: {timeZoneTime.DateTime:G}"
printfn ""

Array.Clear(timeZoneTimes, 1, 1)
for timeZoneTime in timeZoneTimes do
    let tz = if isNull timeZoneTime.TimeZone then "<null>" else string timeZoneTime.TimeZone
    printfn $"{tz}: {timeZoneTime.DateTime:G}"

// The example displays the following output:
//       (UTC-08:00) Pacific Time (US & Canada): 1/20/2014 12:11:00 PM
//       UTC: 1/20/2014 12:11:00 PM
//
//       (UTC-08:00) Pacific Time (US & Canada): 1/20/2014 12:11:00 PM
//       <null>: 1/1/0001 12:00:00 AM
Public Structure TimeZoneTime
   Private dt As DateTimeOffset
   Private tz As TimeZoneInfo
   
   Public Sub New(dateTime As DateTimeOffset, timeZone As TimeZoneInfo)
      dt = dateTime
      tz = timeZone
   End Sub

   Public ReadOnly Property DateTime As DateTimeOffset
      Get
         Return dt
      End Get
   End Property
   
   Public ReadOnly Property TimeZone As TimeZoneInfo
      Get
         Return tz
      End Get
   End Property 
End Structure

Module Example
   Public Sub Main()
      ' Declare an array with two elements.
      Dim timeZoneTimes() As TimeZoneTime = { New TimeZoneTime(Date.Now, TimeZoneInfo.Local),
                                              New TimeZoneTime(Date.Now, TimeZoneInfo.Utc) }   
      For Each timeZoneTime In timeZoneTimes
         Console.WriteLine("{0}: {1:G}", 
                           If(timeZoneTime.TimeZone Is Nothing, "<null>", timeZoneTime.TimeZone), 
                           timeZoneTime.DateTime)
      Next
      Console.WriteLine()
      
      Array.Clear(timeZoneTimes, 1, 1)
      For Each timeZoneTime In timeZoneTimes
         Console.WriteLine("{0}: {1:G}", 
                           If(timeZoneTime.TimeZone Is Nothing, "<null>", timeZoneTime.TimeZone), 
                           timeZoneTime.DateTime)
      Next
   End Sub
End Module
' The example displays output like the following:
'       (UTC-08:00) Pacific Time (US & Canada): 1/20/2014 12:11:00 PM
'       UTC: 1/20/2014 12:11:00 PM
'       
'       (UTC-08:00) Pacific Time (US & Canada): 1/20/2014 12:11:00 PM
'       <null>: 1/1/0001 12:00:00 AM

注解

此方法将数组中的每个元素重置为元素类型的默认值。 它设置引用类型的元素 (包括 String) 元素到 null,并将值类型的元素设置为下表所示的默认值。

类型
Boolean false
所有整型和浮点数值类型 0(零)
DateTime DateTime.MinValue
其他值类型 类型的字段的默认值

清除的元素的范围在多维数组中按行换行。

此方法仅清除元素的值;它不会删除元素本身。 数组具有固定大小;因此,无法添加或删除元素。

此方法是 O (n) 操作,其中 nlength

适用于