What is the difference between Array and ArrayList? When will you use Array over ArrayList?

Solution:

The Array and ArrayList classes differ on the following features:

  • Arrays can contain primitive or objects, while an ArrayList can contain only objects.
  • Arrays havefixed size, while an ArrayList is dynamic.
  • An ArrayList provides more methods and features, such as addAll, removeAll, iterator, etc.
  • For a list of primitive data types, the collections use autoboxing to reduce the coding effort. However, this approach makes them slower when working on fixed size primitive data types.
Array ArrayList
Array should not have values of different data types Array List can have values of different data types.
Size of the arrray is defined at the time of declaration Size of the ArrayList can be dynamically changed
You haveto specify the index in order to add data in an array You do not need to specify the index in an ArrayList
Arrays are not type parameterized Arraylists can be type paramaterized.
Arrays can have primitive data types as well as objects Arraylists can have only objects, no primitive data types are allowed
0 answers