转自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.

Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class的更多相关文章

  1. 【转载】#445 - Differences Between an Interface and an Abstract Class

    An interface provides a list of members, without an implementation, that a class can choose to imple ...

  2. Java精选笔记_集合概述(Collection接口、Collections工具类、Arrays工具类)

    集合概述 集合有时又称为容器,简单地说,它是一个对象,能将具有相同性质的多个元素汇聚成一个整体.集合被用于存储.获取.操纵和传输聚合的数据. 使用集合的技巧 看到Array就是数组结构,有角标,查询速 ...

  3. Collection接口和Collections类的简单区别和讲解

    这里仅仅进行一些简单的比较,如果你想要更加详细的信息话,请自己百度. 1.Collection: 是集合类的上层接口.本身是一个Interface,里面包含了一些集合的基本操作. Collection ...

  4. postman接口测试之复制多个接口或collections到某个子文件夹或collections下

    一.痛点 1.postman只支持复制一个请求,或者一个子文件夹,但是不支持复制多个请求,或者整个collections到某个子文件夹或者某个collections下. 2.网上查了好一会儿,没有一个 ...

  5. [19/03/27-星期三] 容器_Iterator(迭代器)之遍历容器元素(List/Set/Map)&Collections工具类

    一.概念 迭代器为我们提供了统一的遍历容器的方式 /* *迭代器遍历 * */ package cn.sxt.collection; import java.security.KeyStore.Ent ...

  6. NET设计规范(二) 命名规范

    http://blog.csdn.net/richnaly/article/details/6280294 第2章       命名规范 2.1.   大小写约定 2.1.1.    标识符的大小写规 ...

  7. 程序猿的量化交易之路(32)--Cointrade之Portfolio组合(19)

    转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top/ Portfolio:组合,代表的是多个 ...

  8. Collection 和 Collections;Array与Arrays的区别

    Collection 和 Collections 的区别. Collection 是个 java.util 下的接口 ,它是各种集合结构的父接口.继承与他的接口主要有 Set  和 List. Col ...

  9. Java集合(三)--Collection、Collections和Arrays

    Collection: Collection是集合类的顶级接口,提供了对集合对象进行基本操作的通用接口方法.Collection接口的意义是为各种具体的集合提供了最大化 的统一操作方式,其直接继承接口 ...

随机推荐

  1. javaweb基础 02--javaweb基础概念

    1.WEB资源 * 静态web资源:指web页面中供人们浏览的数据始终是不变(如 html 页面). * 动态web资源:指web页面中供人们浏览的数据是由程序产生的,不同时间点访问web页面看到的内 ...

  2. Spark学习笔记--Spark在Windows下的环境搭建

    本文主要是讲解Spark在Windows环境是如何搭建的 一.JDK的安装 1.1 下载JDK 首先需要安装JDK,并且将环境变量配置好,如果已经安装了的老司机可以忽略.JDK(全称是JavaTM P ...

  3. Windows平台编译SQLite 3

    由于需要sqlite的x64版本只能自己编译,下载sqlite源代码.sqlite.def.Visual Studio 2013新建一个Visual C++ Empty Project,Configu ...

  4. C# DataView操作DataTable

    1.DataView筛选数据 //假设有一个DataTable数据 DataTable dt = new DataTable(); //DataTable转成DefaultView DataView ...

  5. Android服务开发——WebService

    我在学习Android开发过程中遇到的第一个疑问就是Android客户端是怎么跟服务器数据库进行交互的呢?这个问题是我当初初次接触Android时所困扰我的一个很大的问题,直到几年前的一天,我突然想到 ...

  6. Docker Swarm——集群管理

    前言 之前在总结docker machine的时候,当时对docker理解还不够深入,甚至还不知道 docker machine 与 docker swarm 的区别. 在查阅资料以及官方文档之后,今 ...

  7. thinkphp---部署在IIS8.0服务器上

    最近做了一个项目,使用的是我自己基于thinkphp开发的一套CMS,由于我本地使用的都是apche的环境,即使是线上环境用的也是宝塔面板,但是现在要将thinkphp的系统部署在IIS8.0的环境下 ...

  8. HttpClient 学习整理【转】

    转自 http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html HttpClient 是我最近想研究的东西,以前想过的一些应用没能有很好的 ...

  9. Timer应用之Interval优化

    开发中, 有时有这种场景,使用 Timer 的 Timer_Elapsed 间隔  执行(如:从数据库)获取数据 与 现有 应用服务器中的 静态变量数据(起到缓存的目的)做 对比 ,若有改变,则 更新 ...

  10. HOJ-1005 Fast Food(动态规划)

    Fast Food My Tags (Edit) Source : Unknown Time limit : 3 sec Memory limit : 32 M Submitted : 3777, A ...