java的collections集合
时间:2007-05-28 yycnet.yeah.net yyc译
下面这张表格(表一)总结了用一个集合能做的所有事情(亦可对Set和List做同样的事情,尽管List还提供了一些额外的功能)。Map不是从Collection继承的,所以要单独对待。boolean add(Object) *保证集合内包含了自变量。如果它没有添加自变量,就返回false(假)boolean addAll(Collection) *添加自变量内的所有元素。如果没有添加元素,则返回true(真)void clear() *删除集合内的所有元素boolean contains(Object) 若集合包含自变量,就返回“真”boolean containsAll(Collection) 若集合包含了自变量内的所有元素,就返回“真”boolean isEmpty() 若集合内没有元素,就返回“真”Iterator iterator() 返回一个反复器,以用它遍历集合的各元素boolean remove(Object) *如自变量在集合里,就删除那个元素的一个实例。如果已进行了删除,就返回“真”boolean removeAll(Collection) *删除自变量里的所有元素。如果已进行了任何删除,就返回“真”boolean retainAll(Collection) *只保留包含在一个自变量里的元素(一个理论的“交集”)。如果已进行了任何改变,就返回“真”int size() 返回集合内的元素数量Object[] toArray() 返回包含了集合内所有元素的一个数组
Boolean add(Object)
|
*Ensures that the Collection contains the argument. Returns false if it doesn’t add the argument.
|
Boolean addAll(Collection)
|
*Adds all the elements in the argument. Returns true if any elements were added.
|
void clear()
|
*Removes all the elements in the Collection.
|
Boolean contains(Object)
|
True if the Collection contains the argument.
|
Boolean containsAll(Collection)
|
True if the Collection contains all the elements in the argument.
|
Boolean isEmpty()
|
True if the Collection has no elements.
|
Iterator iterator()
|
Returns an Iterator that you can use to move through the elements in the Collection.
|
Boolean remove(Object)
|
*If the argument is in the Collection, one instance of that element is removed. Returns true if a removal occurred.
|
Boolean removeAll(Collection)
|
*Removes all the elements that are contained in the argument. Returns true if any removals occurred.
|
Boolean retainAll(Collection)
|
*Retains only elements that are contained in the argument (an “intersection” from set theory). Returns true if any changes occurred.
|
int size()
|
Returns the number of elements in the Collection.
|
Object[] toArray()
|
Returns an array containing all the elements in the Collection.
|
Object[] toArray(Object[] a)
|
Returns an array containing all the elements in the Collection, whose type is that of the array a rather than plain Object (you must cast the array to the right type).
|
|
*This is an “optional” method, which means it might not be implemented by a particular Collection. If not, that method throws an UnsupportedOperationException. Exceptions will be covered in Chapter 9.
|
|