Array.Clear メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
オーバーロード
| 名前 | 説明 |
|---|---|
| Clear(Array) |
配列の内容をクリアします。 |
| Clear(Array, Int32, Int32) |
配列内の要素の範囲を各要素型の既定値に設定します。 |
Clear(Array)
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.CoreCLR.cs
- ソース:
- Array.CoreCLR.cs
- ソース:
- 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)
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.CoreCLR.cs
- ソース:
- Array.CoreCLR.cs
- ソース:
- 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
クリアする要素の数。
例外
array は nullです。
index が arrayの下限より小さい。
-又は-
length が 0 未満です。
-又は-
indexとlengthの合計がarrayのサイズを超えています。
例
次の例では、 Clear メソッドを使用して、1 次元、2 次元、および 3 次元配列の整数値をリセットします。
using System;
class Example
{
public static void RunIt()
{
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("\nThree dimensions (Rank=3):");
int[,,] numbers3 = {{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}},
{{9, 10}, {11, 12}}};
for (int i = 0; i < 3; 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 < 3; 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
*
* 9 10
* 11 12
*
* Array.Clear(numbers3, 2, 5)
* 1 2
* 0 0
*
* 0 0
* 0 8
*
* 9 10
* 11 12
*/
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
numbers3[2, 0, 0] <- 9
numbers3[2, 1, 0] <- 10
numbers3[2, 0, 1] <- 11
numbers3[2, 1, 1] <- 12
for i = 0 to 2 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
//
// 9 10
// 11 12
//
// Array.Clear(numbers3, 2, 5)
// 1 2
// 0 0
//
// 0 0
// 0 8
//
// 9 10
// 11 12
//
Module Example
Sub RunIt()
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 2
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("Array.Clear(numbers3, 2, 5)")
Array.Clear(numbers3, 2, 5)
For i As Integer = 0 To 2
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
'
' 9 10
' 11 12
'
' Array.Clear(numbers3, 2, 5)
' 1 2
' 0 0
'
' 0 0
' 0 8
'
' 9 10
' 11 12
次の例では、TimeZoneTime フィールドとTimeZoneInfo フィールドを含むDateTimeOffset構造を定義します。 次に、 Clear メソッドを呼び出して、 TimeZoneTime 値の 2 要素配列内の 1 つの要素をクリアします。 このメソッドは、クリアされた要素の値を TimeZoneInfo オブジェクトの既定値 ( null) と、 DateTimeOffset オブジェクトの既定値 ( DateTimeOffset.MinValue) に設定します。
using System;
public struct TimeZoneTime
{
public TimeZoneTime(DateTimeOffset dateTime, TimeZoneInfo timeZone)
{
DateTime = dateTime;
TimeZone = timeZone;
}
public DateTimeOffset DateTime { get; }
public TimeZoneInfo TimeZone { get; }
}
public class Example1
{
public static void RunIt()
{
// Declare an array with two elements.
TimeZoneTime[] timeZoneTimes = {
new(DateTime.Now, TimeZoneInfo.Local),
new(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 Example1
Public Sub RunIt()
' 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)$ 演算です。ここで、$n$ は length。