Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Loop only the last 10 items in arraylist

Reply
 
Old 05-28-2009   #1 (permalink)
Curious


 
 

Loop only the last 10 items in arraylist

Hi,

I have an ArrayList, mHistoryDataList, whose Count is 200. However, I
only want to loop through the last 10 items on mHistoryDataList.

That is, to loop mHistoryDataList, as if the first 190 items didn't
exist. How can I do this in a "foreach" statement?

foreach (Daily daily in hist.HistoryData)
{
// Start from 191th item
}

Thanks!

My System SpecsSystem Spec
Old 05-28-2009   #2 (permalink)
eschneider


 
 

Re: Loop only the last 10 items in arraylist

Well you probably should not use a ArrayList, use a generic collection
instead.

Then one way is to use a regular For loop and just start and end at the last
20 items.


using System.Collections.ObjectModel;

private void Test()
{

Collection<String> items = CreateItems();

for (int t = items.Count - 20; t < items.Count; t++)
{
String item=items[t];
System.Diagnostics.Debug.WriteLine(item);
}


}

private Collection<String> CreateItems()
{
Collection<String> items = new Collection<string>();

for (int t = 0; t < 40; t++)
{
items.Add("Test "+t.ToString());
}
return items;
}

Eric

"Curious" <fir5tsight@xxxxxx> wrote in message
news:f638db24-3f37-4685-b945-b4a9fd40e518@xxxxxx
Quote:

> Hi,
>
> I have an ArrayList, mHistoryDataList, whose Count is 200. However, I
> only want to loop through the last 10 items on mHistoryDataList.
>
> That is, to loop mHistoryDataList, as if the first 190 items didn't
> exist. How can I do this in a "foreach" statement?
>
> foreach (Daily daily in hist.HistoryData)
> {
> // Start from 191th item
> }
>
> Thanks!
My System SpecsSystem Spec
Old 05-28-2009   #3 (permalink)
Curious


 
 

Re: Loop only the last 10 items in arraylist

Thanks for the advice. It works!
My System SpecsSystem Spec
Old 05-29-2009   #4 (permalink)
Thomas Gagne


 
 

Re: Loop only the last 10 items in arraylist

Curious wrote:

foreach (Daiy daily in hist.HistoryData.GetRange(191, 10))
{
// blah
}
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How can I destroy objects like an ArrayList? PowerShell
Remove items from ArrayList .NET General
Get the last item in ArrayList .NET General
Marshalling ArrayList .NET General
Thread-safety: Change property of items in arraylist versus removingitems from the arraylist .NET General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46