9. java.util.Collections
来源:互联网 发布:js怎么隐藏标签 编辑:程序博客网 时间:2024/05/01 15:55
java.util.Collections 是 java.lang.Object的直接子类,它是一个工具类。内部方法都是静态方法,可以直接类名调用。
static <T> void sort(List<T> list, Comparator<? super T> c)Sorts the specified list according to the order induced by the specified comparator.static void reverse(List<?> list)Reverses the order of the elements in the specified list.static void shuffle(List<?> list)Randomly permutes the specified list using a default source of randomness.static void swap(List<?> list, int i, int j)Swaps the elements at the specified positions in the specified list.static <T> void copy(List<? super T> dest, List<? extends T> src)Copies all of the elements from one list into another.static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)Returns the maximum element of the given collection, according to the natural ordering of its elements.static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)Returns the minimum element of the given collection, according to the natural ordering of its elements.static void rotate(List<?> list, int distance)Rotates the elements in the specified list by the specified distance.static <T> boolean replaceAll(List<T> list, T oldVal, T newVal)Replaces all occurrences of one specified value in a list with another.
import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;public class LianXi { public static void main(String[] args) throws Exception { t_1(); } public static void t_1() { int[] i = {3, 1, 4, 1, 5, 9, 2, 5}; List l = new ArrayList(); for (int j = 0; j < i.length; j++) { l.add(i[j]); } for (int j = 0; j < i.length; j++) { System.out.print(l.get(j) + " "); // 3 1 4 1 5 9 2 5 } Collections.sort(l); // 正顺序 System.out.println(); for (int j = 0; j < i.length; j++) { System.out.print(l.get(j) + " "); // 1 1 2 3 4 5 5 9 } Collections.reverse(l); // 反顺序 System.out.println(); for (int j = 0; j < i.length; j++) { System.out.print(l.get(j) + " "); // 9 5 5 4 3 2 1 1 } Collections.shuffle(l); // 随机顺序,每次都不一样 System.out.println(); for (int j = 0; j < i.length; j++) { System.out.print(l.get(j) + " "); // 4 3 5 9 5 1 2 1 } Collections.sort(l); Collections.rotate(l, 0); // 把列表中的元素轮换位置,0不变,正数是把后边的数移动到前边,负数是把前边的数移动到后边 System.out.println(); for (int j = 0; j < i.length; j++) { System.out.print(l.get(j) + " "); // 1 1 2 3 4 5 5 9 } Collections.sort(l); Collections.replaceAll(l, 1, 100); // 把列表中所有的1转变为100 System.out.println(); for (int j = 0; j < i.length; j++) { System.out.print(l.get(j) + " "); // 100 100 2 3 4 5 5 9 } System.out.println(); Collections.sort(l); List ll = new ArrayList(); Collections.addAll(ll, new Integer[l.size()]); // 新建的列表大小默认为0,在复制之前须设定大小,不然报错 Collections.copy(ll, l); for (int j = 0; j < i.length; j++) { System.out.print(ll.get(j) + " "); // 2 3 4 5 5 9 100 100 ll列表中有了相同的元素 } Collections.sort(l); Collections.swap(l, 2, 5); // 交换列表中两个索引的位置,这两个数字位置互换效果一样。 System.out.println(); for (int j = 0; j < i.length; j++) { System.out.print(l.get(j) + " "); // 2 3 9 5 5 4 100 100 } System.out.println(); System.out.println(Collections.min(l)); // 2 System.out.println(Collections.max(l)); // 100 }}
阅读全文
0 0
- 9. java.util.Collections
- 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
- util.Collection与java.util.Collections区别
- Java.util.Collections类包
- 要点Java20 java.util.Collections
- java.util.Collections方法大全
- java.util.Collections空集合
- java.util.Collections 进行排序
- MyBatis框架配置与使用1
- C语言等待一秒,延时函数sleep()
- 【计算方法笔记】python四阶Runge-Kutta法
- 为项目整合Spring Boot Acuator
- poj2505:A multiplication game(Hash)
- 9. java.util.Collections
- 在ROS中使用中文语音交互(百度语音baidu_speech)
- Okhttp2同步,异步 get,post
- Effective TensorFlow Chapter 9: TensorFlow模型原型的设计和利用python ops的高级可视化
- android中getWidth()和getMeasuredWidth()之间的区别
- 数据挖掘学习------------------4-分类方法-7-支持向量机(SVM)
- UE 通用小功能代码集合《一》
- 面试知识点—多线程同步【1】之CountDownLatch
- Python猜字游戏