當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


VB.NET Array Clear()用法及代碼示例

Clear() 方法用於清除指定索引中指定數組中元素的值。

用法:

Sub Clear(ByVal arr() as object, ByVal index as integer, Byval length as integer) 

參數:

  • Arr:它是指定的數組。
  • Index:指定索引以清除項目的值。
  • Length:它指定必須清除多少個元素。

返回值:它不返回任何值。

程序/源代碼:

下麵給出了演示 Array 類的 Clear() 方法的源代碼。給定的程序已成功編譯並執行。

'VB.NET program to demonstrate the Clear() 
'method of Array class.

Imports System

Module Module1
    Sub Main()
        Dim arr() As Integer = {10, 20, 30, 40, 50}

        'Reset item in the array from index 2 to 3 by 0.
        Array.Clear(arr, 2, 2)

        Console.WriteLine("Array elements:")
        For i = 0 To arr.Length - 1 Step 1
            Console.Write(arr(i) & " ")
        Next
        Console.WriteLine()
    End Sub
End Module

輸出:

Array elements:
10 20 0 0 50
Press any key to continue . . .

說明:

在上麵的程序中,我們創建了一個包含 Main() 函數的模塊 Module1。 Main() 函數是程序的入口點。

在 Main() 函數中,我們創建了一個包含 5 個元素的數組 arr。然後我們使用 Array 類的 Clear() 方法清除索引 2 到 3 中元素的值,然後在控製台屏幕上打印更新後的數組。





相關用法


注:本文由純淨天空篩選整理自 VB.Net program to demonstrate the Clear() method of Array class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。