0

I want to get count of list but my list is in dictionary.

Dictionary < string,  ArrayList > ();

wording["utterance"+x].Count; // this gives me count for items in dictionary.

What I want to know is:

  • how many items I are there in my ArrayList?
  • how to refer to the elements in the list?
1
  • 1
    wording["utterance"+x].Count; => this gives me count for items in dictionary. if wording is your dictionary this would give you the number of elements in the ArrayList associated with this key. The number of elements (KeyValuePair) in the dictionary itself would be wording.count Commented Nov 26, 2012 at 12:20

3 Answers 3

1

You could of course do:

ArrayList al = wording["key"];
int count = al.Count;

I'm curious why your initial code wouldn't work though, unless Linq extensions are interfering.

I would go with Amicable's suggestion of List<T> over ArrayList though.

Sign up to request clarification or add additional context in comments.

Comments

0

how many items I are there in my ArrayList?

(wording["utterance"+x] as ArrayList).Count; // gives count of items in ArrayList

how to refer to the elements in the list?

wording["Actual Key"][<numeric index of item number>; // wording["utterance"+x][0] in your case for first item in arraylist

Comments

0

how many items I are there in my ArrayList?

The code you gave should do just that:

// number of items in the ArrayList at key "utterance" + x
wording["utterance"+x].Count; 

how to refer to the elements in the list?

You can refer to them by index:

// get the 4th item in the list at key "key"
object myObject = wording["key"][3];

Or you can iterate over them:

foreach (object item in wording["key"])
   DoSomething(item);

To summarize, wording is a Dictionary that stores ArrayLists by a string key. You can retrieve a particular ArrayList by indexing with the appropriate string key for that ArrayList.

wording // evaluates to Dictionary<string, ArrayList>
wording["sometext"] // evaluates to ArrayList

Note that the latter will throw an exception if you have not already placed an ArrayList at that key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.