Arrays of List collections

T

tshad

Is there a way to set up arrays of list collections?

For example, if I have a class such as:
*********************************
public class ParseItem
{
public enum ParseItemTypes
{
DirectionType,
StreetType,
ApartmentType
}
string mstrTestValue;
string mstrReplaceValue;
ParseItemTypes meType;

public ParseItemTypes ParseType
{
get { return meType; }
set { meType = value; }
}
public string ValueTest
{
get { return mstrTestValue; }
set { mstrTestValue = value; }
}
public new string ToString()
{
return ParseType + " / " + ValueReplace;
}
}
*********************************

I would normally set up my list as:

List<ParseItem> mcolParseItems = new List<ParseItem>();

The problem is that I really need to separate these into 3 lists (one for
each of the enums in the class). This is because the lists are so large it
will take a lot of time each time I do a search on the list. I want to be
able to do ".Find" on only DirectionTypes or StreetTypes.

I thought about setting up 3 different lists but then figured that if I
added another item to the enum, it would be nice to have the program set up
the list automatically based on the enum count. If there are 3 enums items,
it would set an array of 3 lists. If there are 5 enums, it would set up an
array of 5 lists.

I would then access the arrays by enum name.

Can this be done?

Thanks,

Tom
 
T

tshad

Peter Duniho said:
List<T> is just like any other class and references to List<T> instances
can be stored in other collections, including arrays, just as any
reference could be. That is, as long as the collection supports an
appropriate type, the reference can be stored.

That worked well.

Is there a way to initally setting it up using the Enum variable?

At the moment I am setting it up like:

public static List<ParseItem>[] mlstParseItems = new
List<ParseItem>[4];

Then I was setting up each list this way:

mlstParseItems[0] = new List<ParseItem>();
mlstParseItems[1] = new List<ParseItem>();
mlstParseItems[2] = new List<ParseItem>();
mlstParseItems[3] = new List<ParseItem>();

But found that I can do it this way instead:

foreach (ParseItem.ParseItemTypes item in
System.Enum.GetValues(typeof(ParseItem.ParseItemTypes)))
{
mlstParseItems[(int)item] = new List<ParseItem>();
}

Then to add to the list I do:

oParseItem = new ParseItem();
mlstParseItems[(int)ParseItem.ParseItemTypes.StreetType].Add(oParseItem);

Then to access the different lists I can do:

StaticVars.mlstParseItems[(int)ParseItem.ParseItemTypes.DirectionType][1].ValueTest

So I can do all the accessing using the enums or by looping through them.

Is there a way to change the "4" where I set up the array to use some sort
of count of the enums?

public static List<ParseItem>[] mlstParseItems = new
List<ParseItem>[????];

Thanks,

Tom
 
G

Göran Andersson

tshad said:
Is there a way to set up arrays of list collections?

For example, if I have a class such as:
*********************************
public class ParseItem
{
public enum ParseItemTypes
{
DirectionType,
StreetType,
ApartmentType
}

8< snip
}
*********************************

I would normally set up my list as:

List<ParseItem> mcolParseItems = new List<ParseItem>();

The problem is that I really need to separate these into 3 lists (one for
each of the enums in the class). This is because the lists are so large it
will take a lot of time each time I do a search on the list. I want to be
able to do ".Find" on only DirectionTypes or StreetTypes.

I thought about setting up 3 different lists but then figured that if I
added another item to the enum, it would be nice to have the program set up
the list automatically based on the enum count. If there are 3 enums items,
it would set an array of 3 lists. If there are 5 enums, it would set up an
array of 5 lists.

I would then access the arrays by enum name.

Can this be done?

Thanks,

Tom

You can use a dictionary of lists:

Dictionary<ParseItem.ParseItemTypes, List<ParseItem>> lists = new
Dictionary<ParseItem.ParseItemTypes, List<ParseItem>>();

Now you can add an item in a list in the dictionary:

List<ParseItem> list;
if (!lists.TryGetValue(item.ParseType, out list)) {
list = new List<ParseItem>();
lists.Add(list);
}
list.Add(item);

Accessing the dictionary using an enum value gives you a list:

List<ParseItem> streets = lists[ParseItem.ParseItemTypes.StreetType];

If you want to do fast searching, you should consider setting up
dictionaries for the properties that you want to search for. Accessing a
dictionary is an O(1) operation compared to looping though a list which
is an O(n) operation. That means that looping though a list gets slower
the more items there are, while accessing a dictionary doesn't get
slower with more items.
 
T

tshad

Peter Duniho said:
[...]
Is there a way to change the "4" where I set up the array to use some
sort
of count of the enums?

Well, sure. You're already using the Enum.GetValues() method. That
returns an Array, which of course has a Length property indicating how
many members are in it.

Are you saying something like this:

public static List<ParseItem>[] mlstParseItems =
new
List<ParseItem>[System.Enum.GetValues(typeof(ParseItem.ParseItemTypes)).Count];

That doesn't seem to work and intellisense doesn't show Count as a choise.

Thanks,

Tom
 
T

tshad

Peter Duniho said:
Well, sure. You're already using the Enum.GetValues() method. That
returns an Array, which of course has a Length property indicating how
many members are in it.

Are you saying something like this:

public static List<ParseItem>[] mlstParseItems =
new
List<ParseItem>[System.Enum.GetValues(typeof(ParseItem.ParseItemTypes)).Count];

That doesn't seem to work and intellisense doesn't show Count as a
choise.

I never said it would. As is shown in the post you quoted, the property
you're looking for is "Length", not "Count".

You're right you did.

I'm not sure why I was thinking you said "Count".

public static List<ParseItem>[] mlstParseItems = new
List<ParseItem>[System.Enum.GetValues(typeof(ParseItem.ParseItemTypes)).Length];

This works fine, as you said.

Thanks,

Tom
 

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

Similar Threads


Top