Java和Collection之间有什么区别? [英] What is the difference between Collection and List in Java?

查看:177
本文介绍了Java和Collection之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Collection和List在Java中有什么区别?

解决方案

首先:a 列表 是一个 集合 。 $ >只是:一个项目的集合。



A 列表您可以添加内容,删除内容,将有关定义的填充序列的信息添加到其中:您可以获取位置 n 的元素,您可以在位置 n 添加元素,您可以删除元素

集合中,您不能这样做:



还有其他专门的集合,例如 设置 这增加了它将不会包含相同元素两次的特征。


What is the difference between Collection and List in Java? When and which I should use?

解决方案

First off: a List is a Collection. It is a specialized Collection, however.

A Collection is just that: a collection of items. You can add stuff, remove stuff, iterate over stuff and query how much stuff is in there.

A List adds the information about a defined sequence of stuff to it: You can get the element at position n, you can add an element at position n, you can remove the element at position n.

In a Collection you can't do that: "the 5th element in this collection" isn't defined, because there is no defined order.

There are other specialized Collections as well, for example a Set which adds the feature that it will never contain the same element twice.

这篇关于Java和Collection之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆