Java 中的列表、队列和集合

发布于 2024-09-27 17:54:19 字数 21 浏览 0 评论 0原文

列表、队列和集合有什么区别?

what is the difference among list, queue and set?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2024-10-04 17:54:19

简而言之:

列表是对象的有序列表,其中同一对象很可能出现多次。例如:[1,7,1,3,1,1,1,5]。谈论列表中的“第三个元素”是有意义的。您可以在列表中的任意位置添加元素、更改列表中的任意位置的元素或从列表中的任意位置删除元素。

队列也是有序的,但您只能接触一端的元素。所有元素都插入到队列的“末尾”并从队列的“开头”(或头部)删除。您可以找出队列中有多少元素,但无法找出“第三”元素是什么。当你到达那里时你就会看到它。

集合未排序且不能包含重复项。任何给定的对象要么在集合中,要么不在集合中。 {7, 5, 3, 1} 与 {1, 7, 1, 3, 1, 1, 1, 5} 完全相同。您同样不能要求“第三个”元素甚至“第一个”元素,因为它们没有任何特定的顺序。您可以添加或删除元素,并且可以查明某个元素是否存在(例如,“这个集合中有 7 个吗?”)

In brief:

A list is an ordered list of objects, where the same object may well appear more than once. For example: [1, 7, 1, 3, 1, 1, 1, 5]. It makes sense to talk about the "third element" in a list. You can add an element anywhere in the list, change an element anywhere in the list, or remove an element from any position in the list.

A queue is also ordered, but you'll only ever touch elements at one end. All elements get inserted at the "end" and removed from the "beginning" (or head) of the queue. You can find out how many elements are in the queue, but you can't find out what, say, the "third" element is. You'll see it when you get there.

A set is not ordered and cannot contain duplicates. Any given object either is or isn't in the set. {7, 5, 3, 1} is the exact same set as {1, 7, 1, 3, 1, 1, 1, 5}. You again can't ask for the "third" element or even the "first" element, since they are not in any particular order. You can add or remove elements, and you can find out if a certain element exists (e.g., "is 7 in this set?")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文