如何从数组列表中计算数组的平均值?

时间:2017-11-13 13:33:49

标签: vb.net arraylist average

我在VB.NET中有一个ArrayList。

例如,ArrayList有两个数组:AList(arr,arr)

Dim AList As ArrayList = New ArrayList((1,2,3,4,5,6,7,8,9,10)
                                       (2,3,4,5,6,7,8,9,10,11))

我想要的只是给定数组的平均值,所以:

所以最终的数组将是(1.5,2.5,3.5,4.5,5.5.........10.5)

注意: Arraylist计数和数组长度会变化。

我尝试了以下代码:

For k = 0 To 9 
    Dim sum As Short 
    Dim a As Array 

    For z = 0 To 1 
        sum = sum + AList.Item(a(k)) 
    Next 

    finalarr(k) = sum / 2 
    RichTextBox1.AppendText(finalarr(k)) 
Next

1 个答案:

答案 0 :(得分:0)

Dim i As Integer = 0
Dim count As Inetger = 0
Dim avgList As List(Of Integer) = New List(Of Integer)()

For Each arr As Integer() In AList
    i = 0
    count += 1
    For Each thing as Integer In arr
        i += 1
        If avgList.Count >= i Then
            avgList.Item(i) = ((avgList.Item(i) + thing) / count)
        Else
            avgList.Add(i)
        End If
    Next
Next