zoukankan      html  css  js  c++  java
  • Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class

    转自http://www.anylogic.com/anylogic/help/index.jsp?topic=/com.xj.anylogic.help/html/code/Arrays_Collections.html

    Java offers two types of constructs where you can store multiple values or objects of the same type: arrays and collections (for System Dynamics models AnyLogic also offers HyperArray, also known as "subscripts", – a special type of collection for dynamic variables).

    Array or collection? Arrays are simple constructs with linear storage of fixed size and therefore they can only store a given number of elements. Arrays are built into the core of Java language and the array-related Java syntax is very easy and straightforward, for example the nth element of the array can be obtained as array[n]. Collections are more sophisticated and flexible. First of all, they are resizable: you can add any number of elements to a collection. A collection will automatically handle deletion of an element from any position. There are several types of collections with different internal storage structure (linear, list, hash set, tree, etc.) and you can choose a collection type best matching your problem so that your most frequent operations will be convenient and efficient. Collections are Java classes and syntax for obtaining, e.g., the nth element of acollection of type ArrayList is collection.get(n).

    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.

    Collection, as its javadoc says is "The root interface in the collection hierarchy." This means that every single class implementing Collection in any form is part of the Java Collections Framework.

    The Collections Framework is Java's native implementation of data structure classes (with implementation specific properties) which represent a group of objects which are somehow related to each other and thus can be called a collection.

    Collections is merely an utility method class for doing certain operations, for example adding thread safety to your ArrayList instance by doing this:

    List<MyObj> list = Collections.synchronizedList(new Arraylist<MyObj>());

    The main difference in my opinion is that Collection is base interface which you may use in your code as a type for object (although I wouldn't directly recommend that) while Collections just provides useful operations for handling the collections.

  • 相关阅读:
    审核被拒:包含隐藏功能
    iOS好的个人博客和平台网站
    免费的Git和SVN服务器
    组件化
    三方生产利器
    RSA加密解密和签名验证机制以及其区别和联系
    APP和后台接口设计规范
    树和二叉树2——输出广义表形式(带括号)二叉树
    树和二叉树1——链式二叉树基础
    计算机图形学5——Two-Dimensional Viewing and Clipping(二维线段裁剪算法)
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4258678.html
Copyright © 2011-2022 走看看