|
-
Mar 2nd, 2011, 12:56 AM
#1
Thread Starter
Fanatic Member
How to Pick last 1000 elements from a generic collection
Say a collection grow. Once it's size reach 10k, I want to get rid the first 9k. How to do so?
Oh ya I could use removeRange.
Is there another function that will output say, the last 1k of the element, say if I do not want to remove anything? Something like tail or something.
-
Mar 2nd, 2011, 01:22 AM
#2
Re: How to Pick last 1000 elements from a generic collection
Assuming .NET 3.5 or later, you have LINQ at your disposal:
vb.net Code:
myList = myList.Skip(myList.Count - 1000).ToList()
That will skip all but the last 1000 items, then create a new List from the rest.
If you're using an earlier version, or even if you're not, you can use the CopyTo method that every collection has:
vb.net Code:
Dim myArray(999) As String myList.CopyTo(myList.Count - 1000, myArray, 0, 1000) myList = New List(Of String)(myArray)
-
Mar 2nd, 2011, 01:24 AM
#3
Re: How to Pick last 1000 elements from a generic collection
try this:
vb Code:
Public Class Form1
Dim integerList As New List(Of Integer)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = 1 To 10
integerList.Add(x)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tempList As List(Of Integer) = integerList.Skip(8).ToList
For Each i In tempList
MsgBox(i)
Next
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 2nd, 2011, 01:35 AM
#4
Re: How to Pick last 1000 elements from a generic collection
It must be the cold over there that has made your fingers too slow to keep up Paul.
-
Mar 2nd, 2011, 01:41 AM
#5
Re: How to Pick last 1000 elements from a generic collection
yeah that's what it is, cobber
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 2nd, 2011, 02:16 AM
#6
Thread Starter
Fanatic Member
Re: How to Pick last 1000 elements from a generic collection
But then again, I can no longer rate JM because he helped me so much. Maybe if I give rating to everyone it'll work again as usual occasionally.
-
Mar 2nd, 2011, 02:24 AM
#7
Re: How to Pick last 1000 elements from a generic collection
you need to rate 10 other members since last time you rated him...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 2nd, 2011, 02:25 AM
#8
Thread Starter
Fanatic Member
Re: How to Pick last 1000 elements from a generic collection
I know. To be frank, 10 is kind of unfair because again and again JMcilhinney is the one that helped me. The least I can do for now is spell his name correctly. Not Anglo Saxon aren't you JM?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|