java.util.Collections
来源:互联网 发布:淘宝样式重置 编辑:程序博客网 时间:2024/05/01 11:55
public class Collectionsextends Object
The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.
如果为此类的方法所提供的 collection 或类对象为 null,则这些方法都将抛出 NullPointerException。
The documentation for the polymorphic algorithms contained in this class generally includes a brief description of the implementation. Such descriptions(说明) should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute(替代) other algorithms, so long as the specification itself is adhered to(遵守). (For example, the algorithm used by sort does not have to be a mergesort(归并排序), but it does have to be stable.)
此类中所含多态算法的文档通常包括对实现 的简短描述。应该将这类描述视为实现注意事项,而不是规范 的一部分。实现者应该可以随意使用其他算法替代,只要遵循规范本身即可。(例如,sort 使用的算法不一定是合并排序算法,但它必须是稳定的。)
The "destructive(破坏的)" algorithms contained in this class, that is, the algorithms that modify the collection on which they operate, are specified to throw UnsupportedOperationException if the collection does not support the appropriate mutation(变种) primitive(s), such as the set method. These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection. For example, invoking the sort method on an unmodifiable list that is already sorted may or may not throw UnsupportedOperationException.
此类中包含的“破坏性”算法,即可修改其所操作的 collection 的算法,该算法被指定在 collection 不支持适当的可变基元(比如 set 方法)时抛出UnsupportedOperationException。如果调用不会对 collection 产生任何影响,那么这些算法可能(但不要求)抛出此异常。例如,在已经排序的、不可修改列表上调用 sort 方法可能会(也可能不会)抛出 UnsupportedOperationException。
This class is a member of the Java Collections Framework.
Since:
- 1.2
- See Also:
Collection
,Set
,List
,Map
nCopies
public static <T> List<T> nCopies(int n,T o)
Returns an immutable(不可变) list consisting of n copies of the specified object. The newly allocated(分配) data object is tiny(小) (it contains a single reference to the data object). This method is useful in combination with the List.addAll method to grow lists. The returned list is serializable.Parameters:
n - the number of elements in the returned list.
o - the element to appear repeatedly in the returned list.Returns:
an immutable list consisting of n copies of the specified object.Throws:
IllegalArgumentException - if n < 0See Also:
List.addAll(Collection), List.addAll(int, Collection)java代码:
package Collection;import java.util.Collections;import java.util.List;public class TestCollections {public static void main(String[] args) {//生成的是java.util.Collections$CopiesList对象//这个私有类继承自AbstractList,没有实现add方法List list = Collections.nCopies(3, new CopyObj());System.out.println(list.getClass().getName());//list.add(new CopyObj());//报UnsupportedOperationExceptionfor(int i = 0;i < list.size(); i++){System.out.print(list.get(i) + " ");}}}class CopyObj{}
fill
public static <T> void fill(List<? super T> list,T obj)
Replaces all of the elements of the specified list with the specified element.
This method runs in linear(线性) time.Parameters:
list - the list to be filled with the specified element.
obj - The element with which to fill the specified list.Throws:
UnsupportedOperationException - if the specified list or its list-iterator does not support the set operation.java代码:
package Collection;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class TestCollectionsFill {public static void main(String[] args) {List list = new ArrayList();list.add("1");list.add("2");list.add("3");Collections.fill(list, new CopyObj());System.out.println(list.getClass().getName());list.add(new CopyObj());for(int i = 0;i < list.size(); i++){System.out.print(list.get(i) + " ");}}}
11
- java.util.Collections;Collections .synchronizedMap
- java.util.Collections学习
- java.util.Collections类
- java.util.Collections学习
- java.util.Collections使用说明
- java.util.Collections
- java.util.Collections 类
- java.util.Collections
- java.util.Collections.reverse()
- Java.util.Collections 类
- java.util.Collections
- 9. java.util.Collections
- util.Collection与java.util.Collections区别
- Java.util.Collections类包
- 要点Java20 java.util.Collections
- java.util.Collections方法大全
- java.util.Collections空集合
- java.util.Collections 进行排序
- iOS开发之UITextField常用属性设置
- Google Map Android v2开发,安装运行Google Map Android v2 示例程序
- xmu 1423.整点坐标
- 从Hadoop框架与MapReduce模式中谈海量数据处理(含淘宝技术架构)
- H.264编码常用参数
- java.util.Collections
- poj1005——I Think I Need a Houseboat
- uva_10167_Birthday Cake( 窮舉+卡精度 )
- Android自定义类库详解
- MapReduce技术的初步了解与学习
- hdu2055An easy problem
- 性能优化之二
- C++ 友元的作用
- 建立DICOM数据集,并将其保持到文件的简单示例代码