473,472 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Arraylist of Hashtables of arraylist

Hi,

I am creating an arraylist (say masterArrayList) of hashtables, where
each hashtable (say table) is of the format key=string, value =
arraylist of strings (say existing_strings). In a foreach loop I
retreive the corresponding hashtable from the masterArrayList and add
new strings to existing_strings after comparing the hashtable keys.

The problem is that when I add a new value to an existing_strings
arraylist it also add the new value to all other hashtables in the
masterArrayList.

Any idea what could be wrong?

Nov 6 '07 #1
7 2655
"Kamran Shafi" <ka**********@gmail.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
I am creating an arraylist (say masterArrayList) of hashtables, where
each hashtable (say table) is of the format key=string, value =
arraylist of strings (say existing_strings). In a foreach loop I
retreive the corresponding hashtable from the masterArrayList and add
new strings to existing_strings after comparing the hashtable keys.

The problem is that when I add a new value to an existing_strings
arraylist it also add the new value to all other hashtables in the
masterArrayList.

Any idea what could be wrong?
This hapens because you have added a reference to the same hashtable to
all the members of the ArrayList. That is, you have done something like
this:

ArrayList masterArrayList = new ArrayList();
Hashtable ht = new Hashtable();
for (int i=0; i<=n; i++)
{
masterArrayList.Add(ht);
}

You can fix it by doing a "new" for every element:

ArrayList masterArrayList = new ArrayList();
for (int i=0; i<=n; i++)
{
masterArrayList.Add(new Hashtable());
}

Nov 6 '07 #2
On Nov 6, 6:13 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
"Kamran Shafi" <kamran.sh...@gmail.comwrote in message

news:11**********************@k35g2000prh.googlegr oups.com...
I am creating an arraylist (say masterArrayList) of hashtables, where
each hashtable (say table) is of the format key=string, value =
arraylist of strings (say existing_strings). In a foreach loop I
retreive the corresponding hashtable from the masterArrayList and add
new strings to existing_strings after comparing the hashtable keys.
The problem is that when I add a new value to an existing_strings
arraylist it also add the new value to all other hashtables in the
masterArrayList.
Any idea what could be wrong?

This hapens because you have added a reference to the same hashtable to
all the members of the ArrayList. That is, you have done something like
this:

ArrayList masterArrayList = new ArrayList();
Hashtable ht = new Hashtable();
for (int i=0; i<=n; i++)
{
masterArrayList.Add(ht);
}

You can fix it by doing a "new" for every element:

ArrayList masterArrayList = new ArrayList();
for (int i=0; i<=n; i++)
{
masterArrayList.Add(new Hashtable());
}
Thanks for a quick response, here is my code:

ArrayList viafields = getListOfViaAttributes(record);
ArrayList viewfields = getListOfViewAttributes(record);
ArrayList ex_nodes;
Hashtable htable;

int i = 0;
foreach (string field in viafields)
{
if (!(HTableList.Count <= i))
{
htable = null;
htable = (Hashtable)HTableList[i];
//HTableList.Remove(htable);

if (!htable.Contains(field))
htable.Add(field, viewfields);
else
{
ex_nodes = (ArrayList)htable[field];
foreach (string node in viewfields)
{
if (!ex_nodes.Contains(node))
ex_nodes.Add(node);
}
}
//HTableList.Add(htable);
}
else
{
htable = new Hashtable();
htable.Add(field, viewfields);
HTableList.Add(htable);
}
i++;
}

Note that HTableList is a global and I am creating it in another
method as HTableList = new ArrayList();
Also ArrayList viafields and viewfields are created using new in their
respective methods.
Regards
Kamran

Nov 6 '07 #3
On Nov 6, 6:34 pm, Kamran Shafi <kamran.sh...@gmail.comwrote:
On Nov 6, 6:13 pm, "Alberto Poblacion" <earthling-

quitaestoparacontes...@poblacion.orgwrote:
"Kamran Shafi" <kamran.sh...@gmail.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
I am creating an arraylist (say masterArrayList) of hashtables, where
each hashtable (say table) is of the format key=string, value =
arraylist of strings (say existing_strings). In a foreach loop I
retreive the corresponding hashtable from the masterArrayList and add
new strings to existing_strings after comparing the hashtable keys.
The problem is that when I add a new value to an existing_strings
arraylist it also add the new value to all other hashtables in the
masterArrayList.
Any idea what could be wrong?
This hapens because you have added a reference to the same hashtable to
all the members of the ArrayList. That is, you have done something like
this:
ArrayList masterArrayList = new ArrayList();
Hashtable ht = new Hashtable();
for (int i=0; i<=n; i++)
{
masterArrayList.Add(ht);
}
You can fix it by doing a "new" for every element:
ArrayList masterArrayList = new ArrayList();
for (int i=0; i<=n; i++)
{
masterArrayList.Add(new Hashtable());
}

Thanks for a quick response, here is my code:

ArrayList viafields = getListOfViaAttributes(record);
ArrayList viewfields = getListOfViewAttributes(record);
ArrayList ex_nodes;
Hashtable htable;

int i = 0;
foreach (string field in viafields)
{
if (!(HTableList.Count <= i))
{
htable = null;
htable = (Hashtable)HTableList[i];
//HTableList.Remove(htable);

if (!htable.Contains(field))
htable.Add(field, viewfields);
else
{
ex_nodes = (ArrayList)htable[field];
foreach (string node in viewfields)
{
if (!ex_nodes.Contains(node))
ex_nodes.Add(node);
}
}
//HTableList.Add(htable);
}
else
{
htable = new Hashtable();
htable.Add(field, viewfields);
HTableList.Add(htable);
}
i++;
}

Note that HTableList is a global and I am creating it in another
method as HTableList = new ArrayList();
Also ArrayList viafields and viewfields are created using new in their
respective methods.

Regards
Kamran- Hide quoted text -

- Show quoted text -
Thanks for the hint I got it write this time. I was not newing one of
the view arraylists inside the loop.

Nov 6 '07 #4
On 2007-11-05 23:34:05 -0800, Kamran Shafi <ka**********@gmail.comsaid:
[...]
Note that HTableList is a global and I am creating it in another
method as HTableList = new ArrayList();
If I read your problem description correctly, you're not having trouble
with the hash tables per se, but rather with an ArrayList they contain
(though you seem to think there are more than one, Alberto is exactly
right: there's just one ArrayList).
Also ArrayList viafields and viewfields are created using new in their
respective methods.
But they are only created once in the method. So you keep adding the
same "viewfields" instance to all of your hash tables. So when you add
anything to that "viewfields" instance (which is called "ex_nodes"
where new additions are made), all of the hash tables appear to be
updated, because they all have copies of the same reference to the
single "viewfields" ArrayList you created.

I have a suspicion that the whole design is wrong, but that's between
you and your debugger. :) For the moment, I'll recommend two changes:

1) put your "viewfields" variable declaration and initialization
_inside_ the first foreach() loop if you want a new copy of the
ArrayList for each hashtable. This will cause a new "viewfields"
instance to be allocated for each "field" in "viafields", ensuring that
when you add a new "node" to the ArrayList that goes with the current
"field" in the current "htable", it only gets added in a single element
of a single hash table.

It won't fix any other basic design or implementation bugs that exist,
but at least the problem you're asking about will stop happening.

And then...

2) fix that first if() clause so that it reads "i <
HTableList.Count" instead of that ridiculously awful
"!(HTableList.Count <= i)".

Hope that helps.

Pete

Nov 6 '07 #5
On Nov 6, 7:13 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
On 2007-11-05 23:34:05 -0800, Kamran Shafi <kamran.sh...@gmail.comsaid:
[...]
Note that HTableList is a global and I am creating it in another
method as HTableList = new ArrayList();

If I read your problem description correctly, you're not having trouble
with the hash tables per se, but rather with an ArrayList they contain
(though you seem to think there are more than one, Alberto is exactly
right: there's just one ArrayList).
Also ArrayList viafields and viewfields are created using new in their
respective methods.

But they are only created once in the method. So you keep adding the
same "viewfields" instance to all of your hash tables. So when you add
anything to that "viewfields" instance (which is called "ex_nodes"
where new additions are made), all of the hash tables appear to be
updated, because they all have copies of the same reference to the
single "viewfields" ArrayList you created.

I have a suspicion that the whole design is wrong, but that's between
you and your debugger. :) For the moment, I'll recommend two changes:

1) put your "viewfields" variable declaration and initialization
_inside_ the first foreach() loop if you want a new copy of the
ArrayList for each hashtable. This will cause a new "viewfields"
instance to be allocated for each "field" in "viafields", ensuring that
when you add a new "node" to the ArrayList that goes with the current
"field" in the current "htable", it only gets added in a single element
of a single hash table.

It won't fix any other basic design or implementation bugs that exist,
but at least the problem you're asking about will stop happening.

And then...

2) fix that first if() clause so that it reads "i <
HTableList.Count" instead of that ridiculously awful
"!(HTableList.Count <= i)".

Hope that helps.

Pete
Thanks Pete, I finally figured it out as I mentioned in my last mail.
And please forgive my ignorance as this is my first week into C#.
You have made me conscious. Why do you think the design is wrong?

Kam

Nov 6 '07 #6
On Nov 6, 7:13 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
On 2007-11-05 23:34:05 -0800, Kamran Shafi <kamran.sh...@gmail.comsaid:
[...]
Note that HTableList is a global and I am creating it in another
method as HTableList = new ArrayList();

If I read your problem description correctly, you're not having trouble
with the hash tables per se, but rather with an ArrayList they contain
(though you seem to think there are more than one, Alberto is exactly
right: there's just one ArrayList).
Also ArrayList viafields and viewfields are created using new in their
respective methods.

But they are only created once in the method. So you keep adding the
same "viewfields" instance to all of your hash tables. So when you add
anything to that "viewfields" instance (which is called "ex_nodes"
where new additions are made), all of the hash tables appear to be
updated, because they all have copies of the same reference to the
single "viewfields" ArrayList you created.

I have a suspicion that the whole design is wrong, but that's between
you and your debugger. :) For the moment, I'll recommend two changes:

1) put your "viewfields" variable declaration and initialization
_inside_ the first foreach() loop if you want a new copy of the
ArrayList for each hashtable. This will cause a new "viewfields"
instance to be allocated for each "field" in "viafields", ensuring that
when you add a new "node" to the ArrayList that goes with the current
"field" in the current "htable", it only gets added in a single element
of a single hash table.

It won't fix any other basic design or implementation bugs that exist,
but at least the problem you're asking about will stop happening.

And then...

2) fix that first if() clause so that it reads "i <
HTableList.Count" instead of that ridiculously awful
"!(HTableList.Count <= i)".

Hope that helps.

Pete
Thanks Pete. And please forgive my ignorance as this is my first week
into C#.
You have made me conscious. Why do you think the design is wrong?

Kam

Nov 6 '07 #7
What's wrong with the generics? List<in place of ArrayList and
Dictionary<in place of HashTable? I'd think this code would be
significantly more friendly with generics. You could go one step
farther and use the PowerCollections to eliminate one level.

Nov 6 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one...
5
by: Netmonster | last post by:
Hello, Does any one have an example of how to create an ArrayList of objects? i.e. ArrayList of ArrayLists or an ArrayList of Hashtables? Thanks in advance KC
2
by: John Grandy | last post by:
What are good rules of thumb to use when deciding between a NameValueCollection, an ArrayList, or a Hashtable ?
10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
9
by: Chris | last post by:
Wondering the best way of storing my data. This is data pulled and stored in the database but a lot of data manipulation goes on the client side. I never have to remove items but will sometimes...
6
by: AAJ | last post by:
Hi all I have some objects that I add to an ArrayList. Each object is from the same class and has methods etc. Each can be identified by a unique property, in this case PK e.g. PK =1 for...
2
by: Bruce | last post by:
Hi I am having a problem understanding the exact advantages of using an ArrayList over a Hashtable in any situation. In most areas of an application I am working on, lookup needs to be fast. If I...
11
by: Cappamontana | last post by:
Hi, I'm stuck with a problem...in .Net C# I want to sort an ArrayList containing Hashtables. The sort key must be the key of the Hashtable. How can I do this? I've coded some example here... ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.