Differences between an array and any collection from the java Collection framework?

后端 未结 4 676
长发绾君心
长发绾君心 2021-02-02 16:29

As the title say I am looking into the \"Differences between an array and any collection from the java Collection framework\".

Thought it\'s high level enough to provide

相关标签:
4条回答
  • 2021-02-02 16:47

    There are 5 differences between Array and Collection as given below :

    1. Arrays are fixed in size, whereas some Collections are grow-able in nature.

    2. Arrays store homogeneous data. Collections store both homogeneous as well as heterogeneous data.

    3. In Arrays, there are no underlining data structures, whereas Collections have underlining data structures.

    4. Arrays are recommended for performance, whereas Collections are not.

    5. Arrays use more memory space as compared to Collections.

    0 讨论(0)
  • 2021-02-02 16:51
    1. Arrays are fixed in length where as collections are growable in nature.
    2. Arrays can store homogeneous elements whereas collections can store both.
    3. If you know the size in advance, go for Arrays
    4. Performance point of view, better to go with Arrays
    5. Arrays does not have any ready made methods whereas Collections have ready made methods.
    0 讨论(0)
  • 2021-02-02 17:10

    6 difference between Arrays and Collections are following:

    1. Arrays are fixed in size but Collections are dynamic in size.
    2. With respect to memory arrays are not good to use but with respect to memory Collections are better to use.
    3. With respect to performance its better to use arrays but with respect to performance collection are not good to use.
    4. Arrays can hold only homogeneous elements but collections can hold both homogeneous and heterogeneous elements
    5. Arrays don't have ready made methods but collections have ready made data structures and methods.
    6. Arrays can hold both primitives and wrapper objects but collections can hold only objects.
    0 讨论(0)
  • 2021-02-02 17:11

    They are virtually unreleated, except to say they both store a group of values.

    From a capability perspective, while both can store references to objects:

    • Arrays can store primitives
    • Collections can not store primitives (although they can store the primitive wrapper classes, such as Integer etc)

    One important difference, commonly not understood by programmers new to java, is one of usability and convenience, especially given that Collections automatically expand in size when needed:

    • Arrays - Avoid using them unless you have to
    • Collections - Use them in preference to arrays

    Arrays are ultimately the only way of storing a group of primitives/references in one object, but they are the most basic option. Although arrays may give you some speed advantages, unless you need super-fast code, Collections are preferred because they have so much convenience.

    0 讨论(0)
提交回复
热议问题