Results 1 to 8 of 8

Thread: [RESOLVED] Keep last 10 numbers in List.

  1. #1

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Resolved [RESOLVED] Keep last 10 numbers in List.

    Hello, i want to keep only the last 10 numbers in a List.

    The list is declared as:
    Dim LastPlayed As New List(Of Integer)

    I then Add several Items, each time checking LastPlayed.Count is no bigger than 10.
    If it is, i only want to keep the 10 most recent numbers.

    How would i do this?
    It's been driving me nuts.
    Zeegnahtuer?

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Keep last 10 numbers in List.

    How about a Queue(Of Integer)?

    vb.net Code:
    1. Dim queue As New Queue(Of Integer)
    2.         With queue
    3.             .Enqueue(10)
    4.             .Enqueue(32)
    5.             .Enqueue(3523)
    6.             .Enqueue(324)
    7.             .Enqueue(124)
    8.             .Enqueue(53)
    9.             .Enqueue(51)
    10.             .Enqueue(5512)
    11.             .Enqueue(214)
    12.             .Enqueue(98)
    13.         End With
    14.         If queue.Count >= 10 Then
    15.             queue.Dequeue() 'removes the first one that went in which is 10
    16.             queue.Enqueue(18) 'the new integer
    17.         End If

    Everytime the count gets to 10, it dequeues the next first one that went in. (Always index 0)

  3. #3

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: Keep last 10 numbers in List.

    Quote Originally Posted by ForumAccount View Post
    How about a Queue(Of Integer)?

    vb.net Code:
    1. Dim queue As New Queue(Of Integer)
    2.         With queue
    3.             .Enqueue(10)
    4.             .Enqueue(32)
    5.             .Enqueue(3523)
    6.             .Enqueue(324)
    7.             .Enqueue(124)
    8.             .Enqueue(53)
    9.             .Enqueue(51)
    10.             .Enqueue(5512)
    11.             .Enqueue(214)
    12.             .Enqueue(98)
    13.         End With
    14.         If queue.Count >= 10 Then
    15.             queue.Dequeue() 'removes the first one that went in which is 10
    16.             queue.Enqueue(18) 'the new integer
    17.         End If

    Everytime the count gets to 10, it dequeues the next first one that went in. (Always index 0)
    I didn't even know .Net had this feature.

    Fantastic stuff. This is exactly what i am looking for.
    Zeegnahtuer?

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Keep last 10 numbers in List.

    Quote Originally Posted by thegreatone View Post
    I didn't even know .Net had this feature
    Oh man, I don't know how many time's I've said that to myself.

  5. #5

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: [RESOLVED] Keep last 10 numbers in List.

    After all this i realised i needed a FILO system, not FIFO. Eeek!

    Useful thing though is Queue.
    Shame i ended up using the original thing i posted about in the end.

    It's always good to learn new things though!
    Zeegnahtuer?

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [RESOLVED] Keep last 10 numbers in List.

    I thought you wanted the FIFO system sorry. The FILO equivalent is Stack(Of T):

    vb.net Code:
    1. Dim stack As New Stack(Of Integer)
    2.         With stack
    3.             .Push(1239)
    4.             .Push(32)
    5.             .Push(3523)
    6.             .Push(324)
    7.             .Push(124)
    8.             .Push(53)
    9.             .Push(51)
    10.             .Push(5512)
    11.             .Push(214)
    12.             .Push(98)
    13.         End With
    14.         If stack.Count > 9 Then
    15.             stack.Pop() 'removes last item to go in, the 98
    16.             stack.Push(18) 'the new integer
    17.         End If

    Works the same way, except it removes the last item that went in when you call .Pop()

  7. #7

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: [RESOLVED] Keep last 10 numbers in List.

    Quote Originally Posted by ForumAccount View Post
    I thought you wanted the FIFO system sorry. The FILO equivalent is Stack(Of T):

    vb.net Code:
    1. Dim stack As New Stack(Of Integer)
    2.         With stack
    3.             .Push(1239)
    4.             .Push(32)
    5.             .Push(3523)
    6.             .Push(324)
    7.             .Push(124)
    8.             .Push(53)
    9.             .Push(51)
    10.             .Push(5512)
    11.             .Push(214)
    12.             .Push(98)
    13.         End With
    14.         If stack.Count > 9 Then
    15.             stack.Pop() 'removes last item to go in, the 98
    16.             stack.Push(18) 'the new integer
    17.         End If

    Works the same way, except it removes the last item that went in when you call .Pop()
    Jesus, it really does have everything built in these days...

    Thankyou so very much! I Can't give you any more rep, but you deserve it.
    Zeegnahtuer?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Keep last 10 numbers in List.

    The only drawback with using a Queue is that you can't access any item other than the next in the queue. Also, . If that's the functionality you want then that's great, but otherwise you should use a List.

    Also, given that you're using a fully OOP language, why don't you take advantage of it by creating your own class to manage this? If you want FIFO functionality then make use of a Queue:
    vb.net Code:
    1. Public Class SizeLimitedQueue(Of T)
    2.  
    3.     Private maxSize As Integer
    4.     Private items As Queue(Of T)
    5.  
    6.     Public ReadOnly Property Size() As Integer
    7.         Get
    8.             Return Me.items.Count
    9.         End Get
    10.     End Property
    11.  
    12.     Public Sub New(ByVal maxSize As Integer)
    13.         Me.maxSize = maxSize
    14.         Me.items = New Queue(Of T)(maxSize)
    15.     End Sub
    16.  
    17.     Public Sub Add(ByVal item As T)
    18.         If Me.items.Count = Me.maxSize Then
    19.             Me.RemoveNext()
    20.         End If
    21.  
    22.         Me.items.Enqueue(item)
    23.     End Sub
    24.  
    25.     Public Function GetNext() As T
    26.         Return Me.items.Peek()
    27.     End Function
    28.  
    29.     Public Function RemoveNext() As T
    30.         Return Me.items.Dequeue()
    31.     End Function
    32.  
    33.     Public Function Swap(ByVal item As T) As T
    34.         Dim result As T = Me.RemoveNext()
    35.  
    36.         Me.Add(item)
    37.  
    38.         Return result
    39.     End Function
    40.  
    41. End Class
    Now you can just do this:
    vb.net Code:
    1. Dim numbers As New SizeLimitedQueue(Of Integer)(10)
    You can then just call Add and/or Swap as you like without ever having to worry about maintaining the size yourself. If you need random access to the items then you can do something similar with a List. You could also incorporate sorting internally to maintain a high score list or the like.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width