[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.
Re: Keep last 10 numbers in List.
How about a Queue(Of Integer)?
vb.net Code:
Dim queue As New Queue(Of Integer)
With queue
.Enqueue(10)
.Enqueue(32)
.Enqueue(3523)
.Enqueue(324)
.Enqueue(124)
.Enqueue(53)
.Enqueue(51)
.Enqueue(5512)
.Enqueue(214)
.Enqueue(98)
End With
If queue.Count >= 10 Then
queue.Dequeue() 'removes the first one that went in which is 10
queue.Enqueue(18) 'the new integer
End If
Everytime the count gets to 10, it dequeues the next first one that went in. (Always index 0)
Re: Keep last 10 numbers in List.
Quote:
Originally Posted by
ForumAccount
How about a Queue(Of Integer)?
vb.net Code:
Dim queue As New Queue(Of Integer)
With queue
.Enqueue(10)
.Enqueue(32)
.Enqueue(3523)
.Enqueue(324)
.Enqueue(124)
.Enqueue(53)
.Enqueue(51)
.Enqueue(5512)
.Enqueue(214)
.Enqueue(98)
End With
If queue.Count >= 10 Then
queue.Dequeue() 'removes the first one that went in which is 10
queue.Enqueue(18) 'the new integer
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.
Re: Keep last 10 numbers in List.
Quote:
Originally Posted by
thegreatone
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.
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!
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:
Dim stack As New Stack(Of Integer)
With stack
.Push(1239)
.Push(32)
.Push(3523)
.Push(324)
.Push(124)
.Push(53)
.Push(51)
.Push(5512)
.Push(214)
.Push(98)
End With
If stack.Count > 9 Then
stack.Pop() 'removes last item to go in, the 98
stack.Push(18) 'the new integer
End If
Works the same way, except it removes the last item that went in when you call .Pop()
Re: [RESOLVED] Keep last 10 numbers in List.
Quote:
Originally Posted by
ForumAccount
I thought you wanted the FIFO system sorry. The FILO equivalent is Stack(Of T):
vb.net Code:
Dim stack As New Stack(Of Integer)
With stack
.Push(1239)
.Push(32)
.Push(3523)
.Push(324)
.Push(124)
.Push(53)
.Push(51)
.Push(5512)
.Push(214)
.Push(98)
End With
If stack.Count > 9 Then
stack.Pop() 'removes last item to go in, the 98
stack.Push(18) 'the new integer
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.
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:
Public Class SizeLimitedQueue(Of T)
Private maxSize As Integer
Private items As Queue(Of T)
Public ReadOnly Property Size() As Integer
Get
Return Me.items.Count
End Get
End Property
Public Sub New(ByVal maxSize As Integer)
Me.maxSize = maxSize
Me.items = New Queue(Of T)(maxSize)
End Sub
Public Sub Add(ByVal item As T)
If Me.items.Count = Me.maxSize Then
Me.RemoveNext()
End If
Me.items.Enqueue(item)
End Sub
Public Function GetNext() As T
Return Me.items.Peek()
End Function
Public Function RemoveNext() As T
Return Me.items.Dequeue()
End Function
Public Function Swap(ByVal item As T) As T
Dim result As T = Me.RemoveNext()
Me.Add(item)
Return result
End Function
End Class
Now you can just do this:
vb.net Code:
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.