Hi,
we have all seen hashtable with a values of string but I want to make a hashtable that
accepts the value as an arraylist,and add items to that array,delete from it,
how can I do that and is it possible?
thank you
Hi,
we have all seen hashtable with a values of string but I want to make a hashtable that
accepts the value as an arraylist,and add items to that array,delete from it,
how can I do that and is it possible?
thank you
That should be possible.
Hashtable table = new Hashtable();
ArrayList list = new ArrayList();
table["list1"] = list;
ArrayList list1 = (ArrayList) table["list1"];
list1.Add("item 1");
bool same = (list == list1);
Console.WriteLine(same);
Hope this helps.
hashtable that accepts the value as an arraylist
do you mean you want to insert objects?System.Collections
has some classes that should help: List<T>
, Dictionary<T1,T2>
where T, T1, T2 can be of every type you want: string, int, long, object, collection, array, etc
Ionut
hi,thanks for replying
you know I have something like the image below,the column that has "s" points to the column that has an arraylist and so on for the other keys,the keys are unique,
I don't know what data structure is the best,
and I haven't worked with List<T>, Dictionary
The Hashtable accepts any object type for both the key and value.
The Dictionary<T1,T2> generic type is just a strongly typed version of the Hashtable
with T1 the type of the key and T2 the type of the value.
The List<T> generic type is just a strongly typed version of the ArrayList with T the type of the value.
Whenever possible it is generaly best to use strong typing as more errors are found at compile time not run-time.
Hope this helps you decide.
Nick
I think the best choose is the hashtable with the arraylist because all my values are string,
but now I'm having problem with retrieving the arraylist from the hashtable
for exmple I have a string that matches with the key of the hashtable and then I want to add some other strings to the arraylist of that key,
how should I do it?
regards
Look at the first reply to your post from sergb again.
I think you probably need to cast the hashtable to the ArrayList like this:
((ArrayList)HashTable["listName"]).Add("New Value String");
It looks like I’m not describing it very well,
I don’t know the listname,I going to access it from my hashtable key
So after I found out that this is the key,I want to access the arraylist of that key,and add stuff to that array,
It doesn’t work right with these codes,I’m mixed up:(
here is the code and the file is something like this
S,A,B
a,b
S>AB
S>a
A>b
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace Context_Free_Grammar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
StreamReader objReader = new StreamReader("c:\\CFG.txt");
////////////////////declaration//////////
string sLine = "";
ArrayList arrText = new ArrayList();
//////////////////////////////
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();
//////////////////////////declaration/////////////////
Hashtable hshTable = new Hashtable();
ArrayList arrV = new ArrayList();
ArrayList arrT = new ArrayList();
//ArrayList Wrkarrlist=new ArrayList();
ArrayList arr = new ArrayList();
string s = "";
string[] sSplit;
////////////////////////////////////
for (int line = 0; line < arrText.Count; line++)
{
if (line == 0)
{
s = arrText[0].ToString();
sSplit = s.Split(',');
for (int i = 0; i < sSplit.Length; i++)
{
arrV.Add(sSplit[i]);
// ArrayList arr = new ArrayList();
hshTable.Add(sSplit[i], "");
}
}
else if (line == 1)
{
s = arrText[1].ToString();
sSplit = s.Split(',');
for (int i = 0; i < sSplit.Length; i++)
{
arrT.Add(sSplit[i]);
}
}
else
{
s = arrText[line].ToString();
sSplit = s.Split('>');
IDictionaryEnumerator en = hshTable.GetEnumerator();
while (en.MoveNext())
{
string str = en.Key.ToString();
if (sSplit[0] == str)
??? arr = (ArrayList)hshTable[str];
????((ArrayList)hshTable["arr"]).Add(sSplit[1]);
}
// }
}
}
}
}
}
so in the hashtable S will point to an arraylist that is going to have AB and a.
thanks to everyone helping
It might help if you posted your code.
Hi nick,
thanks for helping I did post it,
here is it also with notepad.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace Context_Free_Grammar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
StreamReader objReader = new StreamReader("c:\\CFG.txt");
////////////////////declaration//////////
string sLine = "";
ArrayList arrText = new ArrayList();
//////////////////////////////
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();
//////////////////////////declaration/////////////////
Hashtable hshTable = new Hashtable();
ArrayList arrV = new ArrayList();
ArrayList arrT = new ArrayList();
//ArrayList Wrkarrlist=new ArrayList();
ArrayList arr = new ArrayList();
string s = "";
string[] sSplit;
////////////////////////////////////
for (int line = 0; line < arrText.Count; line++)
{
if (line == 0)
{
s = arrText[0].ToString();
sSplit = s.Split(',');
for (int i = 0; i < sSplit.Length; i++)
{
arrV.Add(sSplit[i]);
// ArrayList arr = new ArrayList();
hshTable.Add(sSplit[i], "");
}
}
else if (line == 1)
{
s = arrText[1].ToString();
sSplit = s.Split(',');
for (int i = 0; i < sSplit.Length; i++)
{
arrT.Add(sSplit[i]);
}
}
else
{
s = arrText[line].ToString();
sSplit = s.Split('>');
IDictionaryEnumerator en = hshTable.GetEnumerator();
while (en.MoveNext())
{
string str = en.Key.ToString();
if (sSplit[0] == str)
arr = (ArrayList)hshTable[str];
((ArrayList)hshTable["arr"]).Add(sSplit[1]);
// Wrkarrlist = hshTable["str"].;
// s = hshTable["str"].ToString();
}
// }
}
}
}
}
}
Please see the post above your last. That is the correct method.
Remember you can use variables as your key name, they don't have to be quote marked.
string str = "bob";
hashtable["bob"];
hashtable[str];
Choosing either of the above will give you the same result.
Sorry guys ,I don’t know why I didn’t concentrated very well,
I just was looking for something like tostring and cudn’t find methods like GetType ,GetHashCode,..
string name = hshTable["Author1"].ToString();
it is done by this code:
arr= (ArrayList)hshTable[str];
I don't know how to make this post as solved?!
thanks
another problem that I'm having is that I make the hashtable in a loop,so what I did wrong is that gave the same arraylist to all the keys.
for (int i = 0; i < sSplit.Length; i++)
{
arrV.Add(sSplit[i]);
ArrayList arr = new ArrayList();
hshTable.Add(sSplit[i], arr);
}
so how can I make a different arraylists for every key?:)
Then you'll need to design some decision making to work out which strings go in which array list.
Only you really know the answer to that one ^^
work out which strings go in which array list.
No,at first all arraylist are empty so it's not important which string goes to the arraylist.
the strings here are used to fill the keys of the hashtable and
after that I have to fill the arraylist in another loop,but there i'm just filling one arraylist.(cuz all the keys have the same arraylist)
thanks Ketsuekiame
I ask my question in another way,
how can I make different arraylists(different names)in a loop for my hashtable,
now they all point to one arraylist
S--------->arraylist1
A-------->arraylist1
I want it to be
S------->arraylist1
A------->arraylist2
You should have to use Dictionary.
Take a look at this code snippet.
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
dict.Add("S", new List<string>()
{
"A","B","C","D"
});
dict.Add("T", new List<string>()
{
"P","Q","R"
});
foreach (KeyValuePair<string, List<string>> pair in dict)
{
Console.WriteLine(pair.Key);
foreach (string item in pair.Value)
Console.WriteLine("\t" + item);
}
OR
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
dict.Add("S", new List<string>());
dict.Add("T", new List<string>());
if (dict.ContainsKey("S"))
{
dict["S"].Add("A");
dict["S"].Add("B");
dict["S"].Add("C");
}
if (dict.ContainsKey("T"))
{
dict["T"].Add("P");
dict["T"].Add("Q");
dict["T"].Add("R");
}
foreach (KeyValuePair<string, List<string>> pair in dict)
{
Console.WriteLine(pair.Key);
foreach (string item in pair.Value)
Console.WriteLine("\t" + item);
}
Thanks to adatapost and others everything is going well here,
but the problem is now I'm trying to remove a value from the Dictionary from a special key but it deletes the key also,
how can i sove this one?
hi,
I solved the remove problem but now the problem is when I remove a key or value from the Dictionary in a foreach loop,I get a error that the dictinary has changed Enumerator is not avaliable,....
so how can I remove?!
thank you
Delete a list not a key.
dict["S"].Clear();
Delete a key-value pair.
if (dict.Remove("T"))
{
Console.WriteLine("Del!");
}
Remove all keys.
dict.Clear();
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.