Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array/list vs Dictionary (why we have them at first place)

To me they are both same and that is why i am wondering why we have dictionary data structure when we can do everything with arrays/list? What is so fancy in dictionaries?

like image 672
itsaboutcode Avatar asked Dec 06 '22 02:12

itsaboutcode


1 Answers

Arraylists just store a set of objects (that can be accessed randomly). Dictionaries store pairs of objects. This makes array/lists more suitable when you have a group of objects in a set (prime numbers, colors, students, etc.). Dictionaries are better suited for showing relationships between a pair of objects.

Why do we need dictionaries? lets say you have some data you need to convert from one form to another, like roman numeral characters to their values. Without dictionaries, you'd have to hack this association together with two arrays, where you first find the position the key is in the first list and access that position in the second. This is terribly error prone and inefficient, and dictionaries provide a more direct approach.

like image 136
Gordon Gustafson Avatar answered Dec 18 '22 11:12

Gordon Gustafson