I have an arrayList with key which are shown below:
1
2
3
1 2
1 3
3 1
2 3
1 2 3
2 3 1
I have another sortesList with key and value which shown below:
1:4
2:4
3:4
1 2:3
1 3:3
2 3:4
1 2 3:4
if the key in arrayList which is same with the key in sortedlist,
then will come out the value.
my expected output is :
1:4
2:4
3:4
1 2:3
1 3:3
2 3:4
1 2 3:4
2 3 1:4
how i compare this 2 keys and display it in another new sortedList?
Hope someone can help me. my coding are shown below:
ArrayList arryKey = new ArrayList();
double v;
foreach (String key1 in sortList2.Keys)
{
arryKey.Add(key1);
foreach (String key in arryKey)
{
if (sortList.ContainsKey(key))
{
sortList.TryGetValue(key1, out v);
richTextBox5.AppendText(key1 + " " + v +"\n");
}
}
}
Loading
VulpesPosted Mar 30, 2011, 1:02 PM
y sPosted Mar 30, 2011, 1:32 PM
Once again, I am very grateful to you.
y sPosted Mar 30, 2011, 12:34 PM
VulpesPosted Mar 30, 2011, 12:26 PM
y sPosted Mar 30, 2011, 12:15 PM
string AssR = output.Replace("->", " ");
richTextBox4.AppendText(AssR);
String[] split = (output).Split(new string[] { "\r\n\t", "->" }, StringSplitOptions.RemoveEmptyEntries);
string[] split2 = new string[split.Length];
for (int i = 0; i < split.Length; i++)
{
split2[i] = split[i];
richTextBox4.AppendText(split2[i].ToString() + "\r\n");
}
foreach (String sort2 in richTextBox4.Lines)
{
if (!(sortList2.ContainsKey(sort2)))
{
sortList2.Add(sort2, 1);
}
}
ArrayList arryKey = new ArrayList();
foreach (string key in arryKey)
{
if (sortList.ContainsKey(key))
{
if (!sortList2.ContainsKey(key)) sortList2.Add(key, sortList[key]);
}
}
string text ="";
foreach (string key in sortList2.Keys)
{
text += String.Format("{0}:{1}\r\n", key, sortList2[key]);
}
richTextBox5.Clear();
richTextBox5.AppendText(text);
may i know that My code have something error?
VulpesPosted Mar 30, 2011, 12:00 PM
Are you sure you've copied the code correctly and haven't inadvertently altered something?
y sPosted Mar 30, 2011, 11:48 AM
1 2:1
2 1:1
1:1
2:1
1 3:1
2 3:1
1 2 3:1
3 1:1
3 2:1
1 3 2:1
2 3 1:1
3:1
for example 1 2 in arrayList is same key with 1 2 in sortedLIst. therefore the value should be 3.
e.g. 1 2:3
how i get and display the value?
VulpesPosted Mar 30, 2011, 11:37 AM
Is there some other value you want to display?
y sPosted Mar 30, 2011, 11:32 AM
may i know how to get and display the value?
VulpesPosted Mar 30, 2011, 10:42 AM
richTextBox5.AppendText(text);
VulpesPosted Mar 30, 2011, 10:12 AM
y sPosted Mar 30, 2011, 10:00 AM
1 2
1 3
2 1
2 3
1 2 3
3 1
3 2
1 3 2
2 3 1
1
2
1
3
2
1
2
3
1 2
3
3
1
3
2
1 3
2
2 3
1
but in my sortedList just has
1: 4
2: 4
1 2: 3
3: 4
1 3: 3
2 3: 3
1 2 3: 2
if the keys in the arrayList are same with sortedList key, then will come out the value.
i had try ur code, the error is show that the same keys has been added.
ArrayList arryKey = new ArrayList();
string text = "";
foreach (String key1 in sortList2.Keys)
{
arryKey.Add(key1);
foreach (string key in arryKey)
{
if (sortList.ContainsKey(key))
{
sortList2.Add(key, sortList[key]);
text += String.Format("{0}:{1}\r\n", key, sortList2[key]);
}
}
richTextBox5.AppendText(text);
}
Suthish NairPosted Mar 30, 2011, 9:46 AM
VulpesPosted Mar 30, 2011, 9:38 AM
I don't understand the error you're getting unless you're using different data to that shown above and trying to add the same key more than once to a SortedList which isn't allowed.
y sPosted Mar 30, 2011, 9:31 AM
VulpesPosted Mar 30, 2011, 8:30 AM
string[] strings =
{
"1",
"2",
"3",
"1 2",
"1 3",
"3 1",
"2 3",
"1 2 3",
"2 3 1"
};
ArrayList arryKey = new ArrayList(strings);
SortedList
sortList.Add("1", 4);
sortList.Add("2", 4);
sortList.Add("3", 4);
sortList.Add("1 2", 3);
sortList.Add("1 3", 3);
sortList.Add("2 3", 4);
sortList.Add("1 2 3", 4);
sortList.Add("2 3 1", 4);
SortedList
string text = "";
foreach(string key in arryKey)
{
if(sortList.ContainsKey(key))
{
sortList2.Add(key, sortList[key]);
text += String.Format("{0}:{1}\r\n", key, sortList2[key]);
}
}
richTextBox5.AppendText(text);