What is a main difference between an Array and a Dictionary?

Technology CommunityCategory: ArraysWhat is a main difference between an Array and a Dictionary?
VietMX Staff asked 3 years ago

Arrays and dictionaries both store collections of data, but differ by how they are accessed. Arrays provide random access of a sequential set of data. Dictionaries (or associative arrays) provide a map from a set of keys to a set of values.

  • Arrays store a set of objects (that can be accessed randomly)
  • Dictionaries store pairs of objects
  • Items in an array are accessed by position (index) (often a number) and hence have an order.
  • Items in a dictionary are accessed by key and are unordered.

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.