Collection Interface vs arrays

后端 未结 5 506
庸人自扰
庸人自扰 2020-11-29 02:02

We are learning about the Collection Interface and I was wondering if you all have any good advice for it\'s general use? What can you do with an Collection that you cannot

5条回答
  •  粉色の甜心
    2020-11-29 02:17

    It's basically a question of the desired level of abstraction.

    Most collections can be implemented in terms of arrays, but they provide many more methods on top of it for your convenience. Most collection implementations I know of for instance, can grow and shrink according to demand, or perform other "high-level" operations which basic arrays can't.

    Suppose for instance that you're loading strings from a file. You don't know how many new-line characters the file contains, thus you don't know what size to use when allocating the array. Therefore an ArrayList is a better choice.

提交回复
热议问题