Question how to sum the list value of array list

solutionsdxb

Member
Joined
Mar 18, 2009
Messages
8
Programming Experience
1-3
Hi,

i have created an array list and added value using

arraylist.add(value)

now i want to sum all the value inside the arraylist how its possible.

Dim sublist As New ArrayList()

sublist.Add(1)
sublist.Add(0)
sublist.Add(0)
sublist.Add(0)
sublist.Add(0)
sublist.Add(0)
sublist.Add(0)
sublist.Add(0)
sublist.Add(1)

how can get the sum of those added value.

with regards
 
You can add the value of the items together like this:
VB.NET:
sublist.Add(1) ' item(0)
sublist.Add(0) ' item(1)
sublist.Add(0) ' item(2)
sublist.Add(0) ' item(3)
sublist.Add(0) ' item(4)
sublist.Add(0) ' item(5)
sublist.Add(0) ' item(6)
sublist.Add(0)  'item(7)
sublist.Add(1)  'item(8)

Dim total as integer = 0

total = sublist.item(0) + sublist.item(1) + sublist.item(2) + sublist.item(3) + _
          sublist.item(4) + sublist.item(5) + sublist.item(6) + sublist.item(7) + _
          sublist.item(8)
Or, for less typing, you can put the last part into a loop:

VB.NET:
Dim total As Integer = 0

        Dim i As Integer = 0
        For i = 0 To sublist.Count - 1
            total = total + sublist(i)
            i += 1
        Next


Does this help?
 
Thanks for your reply buddy , but i had found the solution before your post but its really great and helpful for at least other users also in the forums
 
Back
Top