Java中的列表vs队列vs集合
列表、队列和集合有什么区别?
what is the difference among list, queue and set?
推荐答案
简而言之:
list 是对象的有序列表,其中同一个对象很可能出现多次.例如:[1, 7, 1, 3, 1, 1, 1, 5].谈论列表中的第三个元素"是有道理的.您可以在列表中的任意位置添加元素,在列表中的任意位置更改元素,或从列表中的任意位置移除元素.
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.
queue 也是有序的,但你只会在一端接触元素.所有元素都被插入到队列的结束"并从队列的开始"(或头部)中删除.您可以找出队列中有多少元素,但无法找出第三个"元素是什么.当你到达那里时,你会看到它.
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.
set 没有顺序,不能包含重复项.任何给定的对象要么在集合中,要么不在集合中.{7, 5, 3, 1} 与 {1, 7, 1, 3, 1, 1, 1, 5} 完全相同.你不能再要求第三个"元素甚至第一个"元素,因为它们没有任何特定的顺序.您可以添加或删除元素,并且您可以找出某个元素是否存在(例如,这个集合中有 7 个吗?")
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?")
相关文章