Loop only the last 10 items in arraylist

C

Curious

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!
 
E

eschneider

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
 
E

eschneider

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
 
F

Family Tree Mike

Curious said:
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!


foreach (Daily daily in hist.HistoryData.Skip(190))
{
// Starts at the 191st entry!
}
 
F

Family Tree Mike

Curious said:
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!


foreach (Daily daily in hist.HistoryData.Skip(190))
{
// Starts at the 191st entry!
}
 
C

Curious

Sorry I got error when I used "Skip":

'System.Collections.ArrayList' does not contain a definition for 'Skip'
 
C

Curious

Sorry I got error when I used "Skip":

'System.Collections.ArrayList' does not contain a definition for 'Skip'
 
F

Family Tree Mike

Curious said:
Sorry I got error when I used "Skip":

'System.Collections.ArrayList' does not contain a definition for 'Skip'


Ooops! I hardly ever use ArrayLists, but thought it allowed Skip. Generic
List<> allows it.
 
F

Family Tree Mike

Curious said:
Sorry I got error when I used "Skip":

'System.Collections.ArrayList' does not contain a definition for 'Skip'


Ooops! I hardly ever use ArrayLists, but thought it allowed Skip. Generic
List<> allows it.
 
M

Matt Evans

Sorry I got error when I used "Skip":

'System.Collections.ArrayList' does not contain a definition for 'Skip'

Skip is a linq extension and can't be used with arraylist on its own.
However you could do something like:

var q = from Daily d in hist.HistoryData
select d;

foreach(Daily d in q.Skip(190))
{
... do stuff
}

(ensure you import the linq namespace if you want to use it)

As the other posters have mentioned, it's better to use generic
collections, as they are strongly typed, and you can use skip on them
(without converting to an IEnumerable)

Matt
 
M

Matt Evans

Sorry I got error when I used "Skip":

'System.Collections.ArrayList' does not contain a definition for 'Skip'

Skip is a linq extension and can't be used with arraylist on its own.
However you could do something like:

var q = from Daily d in hist.HistoryData
select d;

foreach(Daily d in q.Skip(190))
{
... do stuff
}

(ensure you import the linq namespace if you want to use it)

As the other posters have mentioned, it's better to use generic
collections, as they are strongly typed, and you can use skip on them
(without converting to an IEnumerable)

Matt
 
T

Thomas Gagne

Curious wrote:

foreach (Daiy daily in hist.HistoryData.GetRange(191, 10))
{
// blah
}
 
T

Thomas Gagne

Curious wrote:

foreach (Daiy daily in hist.HistoryData.GetRange(191, 10))
{
// blah
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top