假设我有一个动态数组m_strArray(),视它为一个列表,内容分别为字符串A、B、C、D、E、F、G、H、I、N共十个元素。现在我想实现向上移动、向下移动和删除的操作。比如上移操作,把G移到F而F移到G。下移操作,把C移到D而D移到C。删除就是将数组中指定的元素移除。
我也想出了实现以上功能的一些方法,不外乎逐个循环判断,但总觉得效率不够高。请问大家有没有更好的方法呀?
我也想出了实现以上功能的一些方法,不外乎逐个循环判断,但总觉得效率不够高。请问大家有没有更好的方法呀?
用Collection的话操作简单点,但是读取相对于数组来说慢一些。
所以您要是觉得数组麻烦,可以改用Collection
现在要交换这些仓库的货物,如果不用指针的话,就要把这些仓库的货物进行实际的交换
而是用指针就相当于只交换仓库的标识牌
变通的办法是建立一个索引数组,索引数组里每一个元素指向一个对应的字符串数组的索引,只需要针对这个索引数组来增加减少或移动就能达到你要的效果了, 并且因为只有基本运算不设计API或内存复制之类的东西,程序也很容易实现,并且效率非常高. 这应该才是VB的标准解决方法.
CopyMemory m_clsItems(i), m_clsItems(i + 1), 4 * (m_lngColCount - (i + 1))
ZeroMemory m_clsItems(m_lngColCount - 1), 4
m_lngColCount = m_lngColCount - 1
Dim m_strArray() As String'程序初始化
Private Sub Form_Load()
Erase m_strArray
Command1.Caption = "数据初始化"
Command2.Caption = "向上"
Command3.Caption = "向下"
Command4.Caption = "删除"
End Sub'数据初始化
Private Sub Command1_Click()
m_strArray = Split("A、B、C、D、E、F、G、H、I、N", "、")
PrintArray
End Sub'数据向上移动
Private Sub Command2_Click()
Dim MoveID As Long
Dim ArrayCount As Long
Dim X As Long
Dim tmpStr As String
'校验数据
ArrayCount = -1
On Error Resume Next
ArrayCount = UBound(m_strArray)
If Err.Number < 0 Then
Err.Clear
MsgBox "请您先初始化数据后再操作。"
Exit Sub
End If
If ArrayCount < 0 Then
MsgBox "请您先初始化数据后再操作。"
Exit Sub
End If
If List1.ListCount <= 0 Then Exit Sub
MoveID = List1.ListIndex
If MoveID = 0 Then Exit Sub
If MoveID > ArrayCount Then Exit Sub
'开始处理过程
tmpStr = m_strArray(MoveID)
m_strArray(MoveID) = m_strArray(MoveID - 1)
m_strArray(MoveID - 1) = tmpStr
PrintArray
List1.ListIndex = MoveID - 1
End Sub'数据向下移动
Private Sub Command3_Click()
Dim MoveID As Long
Dim ArrayCount As Long
Dim X As Long
Dim tmpStr As String
'校验数据
ArrayCount = -1
On Error Resume Next
ArrayCount = UBound(m_strArray)
If Err.Number < 0 Then
Err.Clear
MsgBox "请您先初始化数据后再操作。"
Exit Sub
End If
If ArrayCount < 0 Then
MsgBox "请您先初始化数据后再操作。"
Exit Sub
End If
If List1.ListCount <= 0 Then Exit Sub
MoveID = List1.ListIndex
If MoveID >= ArrayCount Then Exit Sub
'开始处理过程
tmpStr = m_strArray(MoveID)
m_strArray(MoveID) = m_strArray(MoveID + 1)
m_strArray(MoveID + 1) = tmpStr
PrintArray
List1.ListIndex = MoveID + 1
End Sub'数据删除
Private Sub Command4_Click()
Dim MoveID As Long
Dim ArrayCount As Long
Dim X As Long
Dim tmpStr As String
'校验数据
ArrayCount = -1
On Error Resume Next
ArrayCount = UBound(m_strArray)
If Err.Number < 0 Then
Err.Clear
MsgBox "请您先初始化数据后再操作。"
Exit Sub
End If
If ArrayCount < 0 Then
MsgBox "请您先初始化数据后再操作。"
Exit Sub
End If
If List1.ListCount <= 0 Then Exit Sub
MoveID = List1.ListIndex
If MoveID < 0 Then Exit Sub
If MoveID > ArrayCount Then Exit Sub
'开始处理过程
If MoveID = ArrayCount Then
If ArrayCount = 0 Then
'只有最后第一数据了,而且还是删除
Erase m_strArray
MoveID = 0
Else
'只是删除最后一条数据
ReDim Preserve m_strArray(ArrayCount - 1)
MoveID = ArrayCount - 1
End If
Else
'删除普通数据
ArrayCount = ArrayCount - 1
For X = MoveID To ArrayCount
m_strArray(X) = m_strArray(X + 1)
Next X
ReDim Preserve m_strArray(ArrayCount)
If MoveID > ArrayCount Then MoveID = ArrayCount
End If
PrintArray
List1.ListIndex = MoveID
End Sub
'刷新数据显示
Private Sub PrintArray()
Dim X As Long
Dim ArrayCount As Long
List1.Clear
'校验数据
ArrayCount = -1
On Error Resume Next
ArrayCount = UBound(m_strArray)
If Err.Number < 0 Then
Err.Clear
Exit Sub
End If
If ArrayCount < 0 Then
Exit Sub
End If '开始处理过程
For X = LBound(m_strArray) To UBound(m_strArray)
List1.AddItem m_strArray(X)
Next X
End Sub