473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

arraylist object in hashtable

I'm trying to build a hashtable and a arraylist as object value

I'm not able to retrieve stored object from the hashtable.
Hashtable mp = new Hashtable(); // THE HASHTABLE
ArrayList atemp = new ArrayList(); // THE ARRAY

StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");

atemp.Clear(); // Create an array with just 1 element "5"
atemp.Add("5");

sw.WriteLine("COUNT:" + atemp.Count ); // give's 1 : OK

mp.Add("test",atemp); // ADD the array to the hashtable

if(mp.ContainsKey("test")) // try to retrieve the array in the hashtable,
FOUND
{
atemp.Clear();
atemp=(ArrayList)mp["test"]; // retrieve arraylist from key "test"
sw.WriteLine("COUNT:"+ atemp.Count ); // give's 0 : ?????
}

Nov 17 '05 #1
3 9664


Fred wrote:
I'm trying to build a hashtable and a arraylist as object value

I'm not able to retrieve stored object from the hashtable.
Actually, you are :)


Hashtable mp = new Hashtable(); // THE HASHTABLE
ArrayList atemp = new ArrayList(); // THE ARRAY

StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");

atemp.Clear(); // Create an array with just 1 element "5"
atemp.Add("5");

sw.WriteLine("COUNT:" + atemp.Count ); // give's 1 : OK

mp.Add("test",atemp); // ADD the array to the hashtable
OK you have added the ArrayList to the Hashtable. But atemp is still a
reference to that same ArrayList!

if(mp.ContainsKey("test")) // try to retrieve the array in the hashtable,
FOUND
{
atemp.Clear();
You have just cleared out the ArrayList! atemp and mp["test"] are the
*same* ArrayList. If you change this line to

atemp=null;
atemp=(ArrayList)mp["test"]; // retrieve arraylist from key "test"
sw.WriteLine("COUNT:"+ atemp.Count ); // give's 0 : ?????
}


then you will get the results you want (Of course you don't *need* to
set atempt to null before re-assigning it)

--
Larry Lard
Replies to group please

Nov 17 '05 #2
Fred,

I seems to be surprised your array is cleared but it's perfectly normal.

Since ArrayList is a reference type, when adding it to the hashtable, you
don't create another instance by cloning your original array. You have only
one array, that you cleared.

Fabien

"Fred" <fr**@belbone.be> a écrit dans le message de news:
42***********************@news.skynet.be...
I'm trying to build a hashtable and a arraylist as object value

I'm not able to retrieve stored object from the hashtable.
Hashtable mp = new Hashtable(); // THE HASHTABLE
ArrayList atemp = new ArrayList(); // THE ARRAY

StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");

atemp.Clear(); // Create an array with just 1 element "5"
atemp.Add("5");

sw.WriteLine("COUNT:" + atemp.Count ); // give's 1 : OK

mp.Add("test",atemp); // ADD the array to the hashtable

if(mp.ContainsKey("test")) // try to retrieve the array in the hashtable,
FOUND
{
atemp.Clear();
atemp=(ArrayList)mp["test"]; // retrieve arraylist from key "test"
sw.WriteLine("COUNT:"+ atemp.Count ); // give's 0 : ?????
}

Nov 17 '05 #3
Hi Fred,
because an ArrayList is a reference type not a value type, when you add it
to the hashtable, the hashtable does not store an exact copy of the
ArrayList, it just stores a reference to the original ArrayList, so when you
cleafr the contents out of the array list at the end of your code and then
get the count of the items in the arraylist stored in the hashtable (ie
really the same object) then your result will be zero because you removed all
the items from the list:

i.e. in your code:
Hashtable ht = new Hashtable();
ArrayList atemp = new ArrayList();
ArrayList atemp2;

//remove all the items from the ArrayList and add one item
atemp.Clear();
atemp.Add("5");

atemp.Count -> should equal one

//add a REFERENCE to the arraylist to the hashtable, NOT a copy
//of the arraylist - if you want a copy then you need to Clone the object
//to produce a copy of the object
mp.Add("test",atemp);

if(mp.ContainsKey("test")) // try to retrieve the array in the hashtable,
FOUND
{
//Here you are removing al lthe items form the arraylist, this is the same
//object referenced inside the hashtable
atemp.Clear();
//atemp.Count should equal zero at this point

//retrieve the object reference form the hashtable
atemp2=(ArrayList)mp["test"];

//atemp and atemp2 point to the same instance of an object
//so atemp2 will also have a count of zero.

}
Hope that helps
Mark R Dawson

"Fred" wrote:
I'm trying to build a hashtable and a arraylist as object value

I'm not able to retrieve stored object from the hashtable.
Hashtable mp = new Hashtable(); // THE HASHTABLE
ArrayList atemp = new ArrayList(); // THE ARRAY

StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");

atemp.Clear(); // Create an array with just 1 element "5"
atemp.Add("5");

sw.WriteLine("COUNT:" + atemp.Count ); // give's 1 : OK

mp.Add("test",atemp); // ADD the array to the hashtable

if(mp.ContainsKey("test")) // try to retrieve the array in the hashtable,
FOUND
{
atemp.Clear();
atemp=(ArrayList)mp["test"]; // retrieve arraylist from key "test"
sw.WriteLine("COUNT:"+ atemp.Count ); // give's 0 : ?????
}

Nov 17 '05 #4

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...
2
by: D. Shane Fowlkes | last post by:
I've been reading up on Arrays in ASP.NET. I'm going to create an two dimensional array of some type to contain 5 columns but a variable amount of rows. I read up on the ArrayList function and...
5
by: Adda | last post by:
In a Parent mdi form I have a datagrid. I select a record from the grid and then invoke a childmdi form. I add the childmdi to an arraylist to keep track of it. If a user has selected multiple...
3
by: Sam | last post by:
Hi Everyone, I have a stucture below stored in an arraylist and I want to check user's input (point x,y) to make sure there is no duplicate point x,y entered (string label can be duplicated). Is...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
10
by: chrisben | last post by:
Hi, Here is the scenario. I have a list of IDs and there are multiple threads trying to add/remove/read from this list. I can do in C# 1. create Hashtable hList = Hashtable.Synchronized(new...
5
by: blisspikle | last post by:
I figure that someone good at dotnet can look at this and give me a clue on how to easily organize this code? If there is a unique identifier like "Publisher" with a bunch of "Book" that are...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.