数组 arr(20,30,20,10,30,30,10,20,20,10)现在要得到arr(10,20,30)   '这是C语言的静态数组表示方法,VB的怎么表示?不会我的写法是sub arr_Sort(byref  array() as integer) 
   dim tmpArr() as integer
   dim i as integer
   dim j as integer
   dim k as integer
   k=0
  for i=0 to ubound(array)-1
    for j=i+1 to ubound(arry)-i
          if array(i)=array(j) then
             k=k+1             '先统计数组中重复数的个数
           end if 
     next
         tmpArr(k)=array(i)     '这样写肯定不对,但我想不到怎么写
  nextend sub
感觉上面的代码写的很丑,但不知怎么改进,望各位指点指点,谢了。

解决方案 »

  1.   

    Public Sub Array_Sort(ByRef Arr() As Integer)
    Dim tmpArr() As Integer
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer    '第一步,复制非重复的项
        ReDim tmpArr(0)
        tmpArr(0) = Arr(0)
        For i = 1 To UBound(Arr)
            For j = 0 To UBound(tmpArr)
              If Arr(i) = tmpArr(j) Then Exit For
            Next j
            If j > UBound(tmpArr) Then
                ReDim Preserve tmpArr(j)
                tmpArr(j) = Arr(i)
            End If
        Next
        
        '第二步,排序
        For i = UBound(tmpArr) To 1 Step -1
            For j = i To 1 Step -1
                If tmpArr(j) < tmpArr(j - 1) Then
                    k = tmpArr(j - 1)
                    tmpArr(j - 1) = tmpArr(j)
                    tmpArr(j) = k
                End If
            Next j
        Next i
        
        On Error Resume Next
        Erase Arr()
        ReDim Arr(UBound(tmpArr))
        For i = 0 To UBound(tmpArr)
            Arr(i) = tmpArr(i)
        Next i
    End Sub
      

  2.   

    Private Sub Command1_Click()
      On Error Resume Next
      Dim i As Integer
      Dim x As New Collection
      Dim y As Variant
      
      Dim Arr() As Variant
      Arr = Array(10, 20, 10, 40, 10, 30)
      ReDim Preserve Arr(5) As Variant
      For i = 0 To UBound(Arr)
       x.Add Arr(i), Str(Arr(i))
       'Debug.Print x.Item(1)
      Next
      For Each y In x
         Debug.Print y
      NextEnd Sub