I am trying to solve a problem which requires me to list the number below 10 that are multiples of 3 and then add them together. I am using an ArrayList as after I have solved this problem I then want to extend the problem and perform bigger calculations.

My line of thinking is to create a loop that does the following :

Dim v as Integer = 1

Dim answer as Integer

Dim answerInt as New ArrayList

Do While v < 10

If v Mod 3 = 0 Then

answer = v / 3

answerInt.add(v)

v += 1

Loop

However I have ran into a problem as I don't know how to return the values from the ArrayList. Is it possible to return each item i.e. 3, 6 and 9 and then add these items together?

You can perform this operation without the ArrayList Object

Dim v As Integer = 1
Dim answer As Integer
Dim result As Integer = 0
Do While v < 10
If v Mod 3 = 0 Then
answer = v / 3
answerInt.Add(v)
result += v
End If
v += 1
Loop
Console.WriteLine(result)

If you need the ArrayList Object , it should be :

Dim v As Integer = 1
Dim answer As Integer
Dim answerInt As New ArrayList
Do While v < 10
If v Mod 3 = 0 Then
answer = v / 3
answerInt.Add(v)
End If
v += 1
Loop
Dim result As Integer = 0
For i As Integer = 0 To answerInt.Count - 1
result += Integer.Parse(answerInt(i))
Next
Console.WriteLine(result)

If you want the collection , you should use the Generic version of the List, you can use the List(Of Integer) Object , it is better of the un/boxing issues

myLabel.Text = "Just \"Mark my post As Answer\" , if it helps you";

## Dan_no7

Member

42 Points

219 Posts

## Reading ArrayList Values

Oct 05, 2008 08:00 AM|Dan_no7|LINK

Hi,

I am trying to solve a problem which requires me to list the number below 10 that are multiples of 3 and then add them together. I am using an ArrayList as after I have solved this problem I then want to extend the problem and perform bigger calculations.

My line of thinking is to create a loop that does the following :

Dim v as Integer = 1

Dim answer as Integer

Dim answerInt as New ArrayList

Do While v < 10

If v Mod 3 = 0 Then

answer = v / 3

answerInt.add(v)

v += 1

Loop

However I have ran into a problem as I don't know how to return the values from the ArrayList. Is it possible to return each item i.e. 3, 6 and 9 and then add these items together?

Thanks for any help.

Daniel

## Dr_no

Member

731 Points

185 Posts

## Re: Reading ArrayList Values

Oct 05, 2008 02:48 PM|Dr_no|LINK

Hi,

You can perform this operation without the ArrayList Object

If you need the ArrayList Object , it should be :

If you want the collection , you should use the Generic version of the List, you can use the List(Of Integer) Object , it is better of the un/boxing issues

## Dan_no7

Member

42 Points

219 Posts

## Re: Reading ArrayList Values

Oct 05, 2008 03:10 PM|Dan_no7|LINK

thanks for your help