我想获取列表计数,但我的列表在字典中。
Dictionary < string, ArrayList > ();
wording["utterance"+x].Count; // this gives me count for items in dictionary.
我想知道的是:
- 我的数组列表中有多少个项目?
- 如何引用列表中的元素?
你当然可以这样做:
ArrayList al = wording["key"];
int count = al.Count;
我很好奇为什么你的初始代码不起作用,除非 Linq 扩展干扰。
不过,我会同意Amicable的建议,即List<T>
而不是ArrayList
。
我的数组列表中有多少个项目?
(wording["utterance"+x] as ArrayList).Count; // gives count of items in ArrayList
如何引用列表中的元素?
wording["Actual Key"][<numeric index of item number>; // wording["utterance"+x][0] in your case for first item in arraylist
我的数组列表中有多少个项目?
您给出的代码应该这样做:
// number of items in the ArrayList at key "utterance" + x
wording["utterance"+x].Count;
如何引用列表中的元素?
您可以通过索引引用它们:
// get the 4th item in the list at key "key"
object myObject = wording["key"][3];
或者您可以迭代它们:
foreach (object item in wording["key"])
DoSomething(item);
总而言之,wording
是一个通过string
键存储ArrayList
的Dictionary
。您可以通过使用该ArrayList
的相应string
键进行索引来检索特定ArrayList
。
wording // evaluates to Dictionary<string, ArrayList>
wording["sometext"] // evaluates to ArrayList
请注意,如果您尚未在该键上放置ArrayList
,则后者将引发异常。