[VB6][VB.NET][C#.NET] 利用記憶體複製(CopyMemory),插入陣列元素

  • 23294
  • 0
  • C#
  • 2008-11-16

摘要:[VB6][VB.NET][C#.NET] 利用記憶體複製(CopyMemory),插入陣列元素

在.Net Framework還沒有問世前,陣列的操作對設計師來說,複雜度還真不小,回味一下利用CopyMemory來進行對陣列的插入,有關CopyMemory的用法,請詳見http://www.google.com.tw/search?q=CopyMemory&sourceid=navclient-ff&ie=UTF-8&rlz=1B3GGGL_zh-TWTW300TW300

VB6:法一,CopyMemory

Private Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) As Boolean
Dim arr()

Private Sub Command1_Click()
    ReDim Preserve arr(UBound(arr) + 1)
    Debug.Print UBound(arr)
    Dim i As Long, j As Long
    i = CLng(Text2.Text)
2:
    If Val(i) > UBound(arr) - 1 Then
        MsgBox "您輸入的位置數值過大", , "信息提示"
        GoTo 2
    Else
        CopyMemory arr(i + 1), arr(i), (UBound(arr) - i + 2) * 16
        arr(i) = CLng(Text3.Text)
        Text4.Text = ""
        For j = LBound(arr) To UBound(arr)
            If j <> 0 Then
                Text4.Text = Text4.Text & ","
            End If
            Text4.Text = Text4.Text & arr(j)
        Next j
    End If
End Sub


Private Sub Form_Load()
    Dim j As Long
    arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
    Text1.Text = ""
    For j = LBound(arr) To UBound(arr)
        If j <> 0 Then
            Text1.Text = Text1.Text & ","
        End If
        Text1.Text = Text1.Text & arr(j)
    Next j
End Sub

執行結果如下:

 法二:土法煉鋼

Private Sub Command2_Click()
    ReDim Preserve arr(UBound(arr) + 1)
    Debug.Print UBound(arr)
    Dim i As Long, j As Long, m As Long, n As Long
    i = CLng(Text2.Text)
    n = CLng(Text3.Text)
2:
    If Val(i) > UBound(arr) - 1 Then
        MsgBox "您輸入的位置數值過大", , "信息提示"
        GoTo 2
    Else
        For j = i To UBound(arr) '插入新數值
            m = arr(j)
            arr(j) = n
            n = m
        Next j

        Text4.Text = ""
        For j = LBound(arr) To UBound(arr)
            If j <> 0 Then
                Text4.Text = Text4.Text & ","
            End If
            Text4.Text = Text4.Text & arr(j)
        Next j
    End If
End Sub

執行結果如下:

範例下載VB6插入陣列.rar

VB.Net:用CopyMemory似乎不能像VB6一樣的把資料做偏移。別怕我們還有System.Collections.ArrayList.Insert可以用來做陣列的插入,當然還有更多更方便的功能,提供給設計師使用,因為太簡單了,所以懶的寫,只列出資料來源以便爾後資料查詢。

.NET Framework 類別庫:

宣告成陣列的變數就能使用System.Array類別: System.Array.IList.Insert
動態陣列: System.Collections.ArrayList.Insert

以下為比較表:

比較項目

Array

ArrayList

初始資料型態

依使用者決定

一開始都是Object

元素資料型態

所有元素都是同一種

任何Object可接受的型態

空間配置

使用Redim改變

自行動態配置

支援移除元素

支援插入元素

記憶體空間

使用時機

已知資料型態及元素數量

未知資料型態及元素數量

根據jeff377前輩所指用List<T>效能比較好,所以不才的後學,也依jeff377前輩所指的MSDN試了一下,不知醬的寫法是不是jeff377前輩所講的方法

若有錯誤請前輩指教,感恩!

VB.NET 2005:

    Dim ary() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.ListBox1.Items.Clear()
        Me.ListBox2.Items.Clear()
        For Each i As Integer In ary
            Me.ListBox1.Items.Add(i)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' 一整數陣列
        Me.ListBox1.Items.Clear()
        Me.ListBox2.Items.Clear()
        Dim inarr As Integer = CInt(TextBox1.Text)
        Dim inst As Integer = CInt(TextBox2.Text)
        ary = InstArray(ary, inarr, inst)
        For Each i As Integer In ary '
            Me.ListBox1.Items.Add(i)
            Me.ListBox2.Items.Add(i)
        Next
    End Sub

    Private Function InstArray(ByRef Ary() As Integer, ByVal insarr As Integer, ByVal inst As Integer) As Integer()
        ' 使用 List 泛型類別  
        Dim lst As New List(Of Integer)
        For Each i As Integer In Ary
            lst.Add(i)
        Next
        lst.Insert(insarr, inst)
        Return lst.ToArray ' 將 List 的元素複製到新的陣列並回傳  
    End Function

執行結果如下圖:

 

 範例下載:VB.NET泛型插入陣列元素.rar

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo