Loop only the last 10 items in arraylist

  • Thread starter Thread starter Curious
  • Start date Start date
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!
 
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
 
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 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!
}
 
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!
}
 
Sorry I got error when I used "Skip":

'System.Collections.ArrayList' does not contain a definition for 'Skip'
 
Sorry I got error when I used "Skip":

'System.Collections.ArrayList' does not contain a definition for 'Skip'
 
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.
 
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.
 
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
 
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
 

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

Back
Top