Hashtable

  • Thread starter Thread starter MAY
  • Start date Start date
M

MAY

hi,

if i have the data :

a 123
1213
3243

b 32
312
342

c 3243
231



how to use hashtable to save these data, and how to retrieve these? Thx.

Regards,

MAY
 
MAY said:
hi,

if i have the data :

a 123
1213
3243

b 32
312
342

c 3243
231



how to use hashtable to save these data, and how to retrieve these? Thx.

The normal way would be to have the mapping for each character to an
ArrayList of ints. For instance:

ArrayList al = new ArrayList();
hash["a"] = al;
al.Add(123);
al.Add(1213);
al.Add(3243);

al = new ArrayList();
hash["b"] = al;
al.Add(32);
al.Add(312);
al.Add(342);

etc

When you fetch from the hash, cast the returned value to ArrayList, and
then you can iterate through the integers.
 
Thx :) i got it

MAY
Jon Skeet said:
MAY said:
hi,

if i have the data :

a 123
1213
3243

b 32
312
342

c 3243
231



how to use hashtable to save these data, and how to retrieve these? Thx.

The normal way would be to have the mapping for each character to an
ArrayList of ints. For instance:

ArrayList al = new ArrayList();
hash["a"] = al;
al.Add(123);
al.Add(1213);
al.Add(3243);

al = new ArrayList();
hash["b"] = al;
al.Add(32);
al.Add(312);
al.Add(342);

etc

When you fetch from the hash, cast the returned value to ArrayList, and
then you can iterate through the integers.
 
Hi Jon,

Can you post a simple code to retreive the data?

Thx again.

MAY


MAY said:
Thx :) i got it

MAY
Jon Skeet said:
MAY said:
hi,

if i have the data :

a 123
1213
3243

b 32
312
342

c 3243
231



how to use hashtable to save these data, and how to retrieve these?
Thx.

The normal way would be to have the mapping for each character to an
ArrayList of ints. For instance:

ArrayList al = new ArrayList();
hash["a"] = al;
al.Add(123);
al.Add(1213);
al.Add(3243);

al = new ArrayList();
hash["b"] = al;
al.Add(32);
al.Add(312);
al.Add(342);

etc

When you fetch from the hash, cast the returned value to ArrayList, and
then you can iterate through the integers.
 
MAY said:
Can you post a simple code to retreive the data?

ArrayList al = (ArrayList) hash["a"];

int x = (int)al[1];

// x is now 1213 from the example you gave
 
// Enumerate
ArrayList al = hash["a"] as ArrayList;
if ( al != null ) {
foreach(int i in al) {
Console.WriteLine(i);
}
}

// Adding an element
public void AddKeyedValue(string key, int value) {
ArrayList al = hash[key] as ArrayList;
if ( al == null ) {
al = new ArrayList();
hash[key] = al;
}
al.Add(value);
}


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

MAY said:
Hi Jon,

Can you post a simple code to retreive the data?

Thx again.

MAY


MAY said:
Thx :) i got it

MAY
Jon Skeet said:
hi,

if i have the data :

a 123
1213
3243

b 32
312
342

c 3243
231



how to use hashtable to save these data, and how to retrieve these? Thx.

The normal way would be to have the mapping for each character to an
ArrayList of ints. For instance:

ArrayList al = new ArrayList();
hash["a"] = al;
al.Add(123);
al.Add(1213);
al.Add(3243);

al = new ArrayList();
hash["b"] = al;
al.Add(32);
al.Add(312);
al.Add(342);

etc

When you fetch from the hash, cast the returned value to ArrayList, and
then you can iterate through the integers.
 
Back
Top