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

后端 未结 4 677
长发绾君心
长发绾君心 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 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.

提交回复
热议问题