notes

Storage contents:

  • Arrays can hold basic types and objects
  • An ArrayList can store objects

Space:

  • Array is fixed
  • Arraylists grow dynamically. Each time a new element is added, it checks whether the internal array has enough space. If it does not, it creates a new array with twice the space, adds the elements to the new array, and jettises the old array.

Methods:

Arraylists are more diverse, such as adding all addAll(), removing all removeAll(), returning an iterator(), and so on.

Usage Scenarios:

Frequently delete or move, using LinkedList

The original

Comparison of storage contents:

  • Arrays can contain both primitive types and object types,
  • An ArrayList can only contain object types.

An Array must contain elements of the same type. The same cannot be said for an ArrayList, which can store objects.

Space size comparison:

  • Its space size is fixed, when the space is not enough, it can not be applied again, so it is necessary to determine the appropriate space size in advance.
  • The space of an ArrayList grows dynamically. If it runs out of space, it creates a new array twice the size of the original, copies all the elements into the new array, and then discards the old array. Also, each time a new element is added, the internal array is checked to see if there is enough space. (A bit of trouble).

Comparison of methods: ArrayList, as an enhanced version of Array, is of course more diverse in methods than Array, such as adding all addAll(), removing all removeAll(), returning iterator(), etc.

Applicable scenario: if you want to save some during the whole program is running will exist and the same data, we can put them into a global array, but if we just want to save the data in the form of an array, and operations such as wrong data increase, just convenient we find words, so, we will choose the ArrayList. And it’s also important to know that if we need to move or delete elements a lot, or if we’re dealing with a huge amount of data, then ArrayList is really not a good choice because it’s inefficient, it’s cumbersome to do that with arrays, so, Consider LinkedList.

Link: www.nowcoder.com/questionTer… Source: Niuke.com