Multidimension ArrayList

T

tshad

I am trying to create a list of 2 strings. 1st string would be a name and
2nd string would be the value.

Something like:

ArrayList s = new ArrayList();
string[,] s1 = new string[1,2];
s1 = {"City","NewPort Beach"};
s.Add(s1);
s1 = {"State","California"};
s.Add(s1);
s1 = {"Zip","92660"};
s.Add(s1);

Then I would do a foreach on the array into a Datatable (not sure how I
would set up the foreach) but in the loop would be something like:

DataRow dr = ds.Tables["Address"].NewRow();

dr["tagName"] = s2[0];
dr["tagValue"] = s2[1];

ds.Tables["Address"].Rows.InsertAt(dr,0);

I originally was going to just do an array of strings where the 1st index
would be the name, 2nd index would be the value and I would add that to the
DataTable. Then I would get the 3rd index and 4th index and do the same.

I wanted to see if it would be better to use an array list of 2 dimensions
to do the same thing.

Thanks,

Tom
 
B

Ben Voigt [C++ MVP]

tshad said:
I am trying to create a list of 2 strings. 1st string would be a name and
2nd string would be the value.

Something like:

ArrayList s = new ArrayList();
string[,] s1 = new string[1,2];
s1 = {"City","NewPort Beach"};
s.Add(s1);
s1 = {"State","California"};
s.Add(s1);
s1 = {"Zip","92660"};
s.Add(s1);

Then I would do a foreach on the array into a Datatable (not sure how I
would set up the foreach) but in the loop would be something like:

DataRow dr = ds.Tables["Address"].NewRow();

dr["tagName"] = s2[0];
dr["tagValue"] = s2[1];

ds.Tables["Address"].Rows.InsertAt(dr,0);

I originally was going to just do an array of strings where the 1st index
would be the name, 2nd index would be the value and I would add that to
the DataTable. Then I would get the 3rd index and 4th index and do the
same.

I wanted to see if it would be better to use an array list of 2 dimensions
to do the same thing.

Don't use ArrayList anymore, it's obsolete.

I think that List<string[]> should solve your problem.
 
P

Peter Duniho

I am trying to create a list of 2 strings. 1st string would be a name
and
2nd string would be the value.

Something like:

ArrayList s = new ArrayList();
string[,] s1 = new string[1,2];
s1 = {"City","NewPort Beach"};
s.Add(s1);
s1 = {"State","California"};
s.Add(s1);
s1 = {"Zip","92660"};
s.Add(s1);

I haven't bothered to try to compile that, but that doesn't look right to
me. You can't assign a one-dimensional array (like { "City", "Newport
Beach" }) to a two-dimensional array (there's also no point in
initializing "s1" in the declaration, but that's just superfluous, not
uncompilable :) ).
Then I would do a foreach on the array into a Datatable (not sure how I
would set up the foreach) but in the loop would be something like:

DataRow dr = ds.Tables["Address"].NewRow();

dr["tagName"] = s2[0];
dr["tagValue"] = s2[1];

ds.Tables["Address"].Rows.InsertAt(dr,0);

I originally was going to just do an array of strings where the 1st index
would be the name, 2nd index would be the value and I would add that to
the
DataTable. Then I would get the 3rd index and 4th index and do the same.

I wanted to see if it would be better to use an array list of 2
dimensions
to do the same thing.

Do you mean "an array list of 2-dimensional arrays"? There's no such
thing as a 2-dimensional ArrayList.

All that said, it's still unclear to me exactly what you want your final
table to look like. Are you trying to create a new table for each item,
where each row is a pair of "tagName" and "tagValue"? Any particular
reason for doing that, rather than having a more conventional (to me,
anyway) single table containing one row per data item, where you have
pre-defined columns for the "tags" (e.g. columns named "City", "State",
"Zip", etc.).

Ben is correct that it's _possible_ you're looking for something more like
a Dictionary<TKey, TValue>, where the "tagName" is the key, and the
"tagValue" is the value. But that still would create a new dictionary for
each data item, which seems wasteful. A simple array would be more
efficient, if you can simply assign a specific index in each array to a
given column. For example:

enum TagNames
{
City = 0,
State = 1,
Zip = 2, // etc.
}

List<string[]> data = new List<string[]>();

data.Add(new string[] { "Newport Beach", "California", "92660" });

Then later...

foreach (string[] item in data)
{
// initialize "ds" as appropriate, as it appears to be specific to
each "item"

DataRow dr = ds.Tables["Address"].NewRow();

dr["tagName"] = TagNames.City.ToString();
dr["tagValue"] = item[TagNames.City];

ds.Tables["Address"].Rows.InsertAt(dr,0);

dr["tagName"] = TagNames.State.ToString();
dr["tagValue"] = item[TagNames.State];

ds.Tables["Address"].Rows.InsertAt(dr,0);

dr["tagName"] = TagNames.Zip.ToString();
dr["tagValue"] = item[TagNames.Zip];

ds.Tables["Address"].Rows.InsertAt(dr,0);
}

Note above that I'm simply following your previous convention. As I
mentioned, I'm not really clear on why you seem to have a whole new table
for each data item, with individual rows for each value for the item. But
the above preserves that design, inasmuch as I'm able to comprehend it. :)

Using a dictionary instead of a simple array would make more sense if you
have a lot of optional, non-present values for each item. Then rather
than storing a null in the array, the value simply wouldn't even be
present in the dictionary. Possibly not as efficient, depending on usage,
but maybe more readable.

Pete
 
T

tshad

Peter Duniho said:
I am trying to create a list of 2 strings. 1st string would be a name
and
2nd string would be the value.

Something like:

ArrayList s = new ArrayList();
string[,] s1 = new string[1,2];
s1 = {"City","NewPort Beach"};
s.Add(s1);
s1 = {"State","California"};
s.Add(s1);
s1 = {"Zip","92660"};
s.Add(s1);

I haven't bothered to try to compile that, but that doesn't look right to
me. You can't assign a one-dimensional array (like { "City", "Newport
Beach" }) to a two-dimensional array (there's also no point in
initializing "s1" in the declaration, but that's just superfluous, not
uncompilable :) ).
Then I would do a foreach on the array into a Datatable (not sure how I
would set up the foreach) but in the loop would be something like:

DataRow dr = ds.Tables["Address"].NewRow();

dr["tagName"] = s2[0];
dr["tagValue"] = s2[1];

ds.Tables["Address"].Rows.InsertAt(dr,0);

I originally was going to just do an array of strings where the 1st index
would be the name, 2nd index would be the value and I would add that to
the
DataTable. Then I would get the 3rd index and 4th index and do the same.

I wanted to see if it would be better to use an array list of 2
dimensions
to do the same thing.

Do you mean "an array list of 2-dimensional arrays"? There's no such
thing as a 2-dimensional ArrayList.

All that said, it's still unclear to me exactly what you want your final
table to look like. Are you trying to create a new table for each item,
where each row is a pair of "tagName" and "tagValue"? Any particular
reason for doing that, rather than having a more conventional (to me,
anyway) single table containing one row per data item, where you have
pre-defined columns for the "tags" (e.g. columns named "City", "State",
"Zip", etc.).

Ben is correct that it's _possible_ you're looking for something more like
a Dictionary<TKey, TValue>, where the "tagName" is the key, and the
"tagValue" is the value. But that still would create a new dictionary for
each data item, which seems wasteful. A simple array would be more
efficient, if you can simply assign a specific index in each array to a
given column. For example:

enum TagNames
{
City = 0,
State = 1,
Zip = 2, // etc.
}

List<string[]> data = new List<string[]>();

data.Add(new string[] { "Newport Beach", "California", "92660" });

Then later...

foreach (string[] item in data)
{
// initialize "ds" as appropriate, as it appears to be specific to
each "item"

DataRow dr = ds.Tables["Address"].NewRow();

dr["tagName"] = TagNames.City.ToString();
dr["tagValue"] = item[TagNames.City];

ds.Tables["Address"].Rows.InsertAt(dr,0);

dr["tagName"] = TagNames.State.ToString();
dr["tagValue"] = item[TagNames.State];

ds.Tables["Address"].Rows.InsertAt(dr,0);

dr["tagName"] = TagNames.Zip.ToString();
dr["tagValue"] = item[TagNames.Zip];

ds.Tables["Address"].Rows.InsertAt(dr,0);
}

Note above that I'm simply following your previous convention. As I
mentioned, I'm not really clear on why you seem to have a whole new table
for each data item, with individual rows for each value for the item. But
the above preserves that design, inasmuch as I'm able to comprehend it.
:)

What I have is a datatable with about 50 rows. 2 of the fields are
FieldName (tagName) and the value for the tagName.

I need to pick up about 5 or 6 of these rows and pass it to another
routine - whether by string, string array - whatever.

Each of these rows will have a name and a value for that name. I only want
to pass this.

That was why I could just pass the 1st name at index 0 and the value at
index 1, and the 2nd name at index 2 and its value at index 3, the 3rd name
at index 4 and its value at index 5.

I could then do something like:

for (int i = 0; i < tagNames.Count; i += 2)
{
HandlePairs(tagNames, tagNames[i + 1]);
}


I thought about using a string array (string[,]), but you have to know the
size of the array at the start (or resize) it.

I thought about the ArrayList since the size is dynamic but as you said you
can't have a multidimensional array.

I was thinking about having an ArrayList where each item is a string[1,2],
but not sure if I can do that or if too complicated to do that.

I can't use your suggestions with the enums as I don't know what order I am
going to get each pair. I just need to add the pair to the array as I get
them and pass it to the procedure that will read through the array and
handle each pair (name and value) separately.

Thanks,

Tom
 
P

Peter Duniho

Sorry. I still think I'm not really understanding the entire picture
here. So, I'll provide brief comment on what I think I do understand. :)


[...]
I thought about using a string array (string[,]), but you have to know
the
size of the array at the start (or resize) it.

Don't you have the same problem using pairs of items in a single-dimension
array?
I thought about the ArrayList since the size is dynamic but as you said
you
can't have a multidimensional array.

Right. :)
I was thinking about having an ArrayList where each item is a
string[1,2],
but not sure if I can do that or if too complicated to do that.

I wouldn't use ArrayList at all, because List<T> is better. And you can
make a List<string[]> where each string[] is 2 elements long.

That's all I can think of at the moment.

Pete
 

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