5 Replies - 27999 Views - Last Post: 23 June 2012 - 09:10 PM Rate Topic: -----

#1 mattcash83   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 23-June 12

For Each loop in arraylists

Posted 23 June 2012 - 06:27 PM

OK, so I am creating a bit of code that will allow me to randomly pick one "card" from a "deck", delete that "card" from the deck, and repeat. I found in all of my research that vb.net has the arraylist that is supposed to be very simple in these situations. However, when I try to populate them with a For Each statement, I only get odd numbers in the array list. What am I missing?

    Dim a As New ArrayList

        For i As Integer = 1 To 52
            a.Add(i)
            i = i + 1
        Next

        For Each number As Integer In a
            Debug.Print(number)
        Next



The debug.Print shows me that I only got these numbers to populate:
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51

Is This A Good Question/Topic? 0
  • +

Replies To: For Each loop in arraylists

#2 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: For Each loop in arraylists

Posted 23 June 2012 - 06:51 PM

    For i As Integer = 1 To 52
        a.Add(i)
        i = i + 1
    Next

look carefully,whats wrong here?
Was This Post Helpful? 0
  • +
  • -

#3 mattcash83   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 23-June 12

Re: For Each loop in arraylists

Posted 23 June 2012 - 07:04 PM

Makes me wish I had not posted this now... I think I will quit staring at the screen for now. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: For Each loop in arraylists

Posted 23 June 2012 - 07:14 PM

can happen to anyone,... And I recommend you to use List (OF Integer) rather than ArrayList.
Was This Post Helpful? 0
  • +
  • -

#5 mattcash83   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 23-June 12

Re: For Each loop in arraylists

Posted 23 June 2012 - 08:40 PM

sela007

Thanks. You mentioned using List(OfInteger), I am unfamiliar with this. While I start to research this, could you explain why you suggested it over the arraylist?
Was This Post Helpful? 0
  • +
  • -

#6 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: For Each loop in arraylists

Posted 23 June 2012 - 09:10 PM

It's all about performance and memory. Read this ArrayList and List Collection Types
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1