What is the difference between Dictionary and Array?
Solution:-
1.For using Dictionary, we need to create its object using “CreateObject“, method, while Array is declared like normal variables using “Dim“
Example:-
Creating Dictionary Object
Set obj = createobject(“scripting.dictionary”)
‘Declaring array
Dim arr(0)
2.Dictionary is more flexible and is richer in terms of built-in functionality. (It has range of methods and properties like Items, remove, Keys,Exists)
Example:-
‘Creating Dictionary Object
Set obj = createobject(“scripting.dictionary”)
‘Using Add methods to add values to dictionary object
obj.Add “UFT”,“2012”
obj.Add “QTP11”,“2010”
obj.Add “QTP10”,“2009”
3.Dictionaries also work better when you want to locate items by their content rather than their position, so we use item property
Example:-
‘Locating items in Dictonary
MsgBox obj.item(“UFT”)
4.Array uses ReDim to vary the size while we don’t need this in dictionary’s .
‘Declaring array
Dim arr()
‘Resizing Array
ReDim arr(1)
arr(0) = “UFT”
arr(1) = “QTP11”
msgbox arr(0)
5.When you delete a particular item from a dictionary, all the subsequent items automatically shifts up
6.Most important of all is that Array can have index only as numeric (0,1,2..) while we can use keys to identify dictionary items and Keys can be of any data subtype(String,integer), except an array or dictionary.
7.Dictionary can’t be multidimensional like Array’s
For more information on Dictionary object:-
Example Scenario’s
Interview Questions on Array’s?
2 replies on “Dictionary v/s Array?”
A Dictionary can't be multi-dimensional but you can have a separate dictionary object assigned to the value portion of a dictionary. This behaves similarly to a two dimensional array.
Very good!