API学习Collections
来源:互联网 发布:《网络基础知识》文档 编辑:程序博客网 时间:2024/05/16 03:25
package com.wonders.week01.collection;import java.util.ArrayList;import java.util.Collections;import java.util.List;/** * JDK1.7 * Collections * @author liyongyong * */public class CollectionsTest { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); List<Integer> newList = new ArrayList<Integer>(); list.add(1); list.add(4); list.add(8); list.add(2); list.add(5); list.add(3); newList.addAll(list); System.out.print("排序前的集合元素顺序:"); for(Integer integer:list){ System.out.print(integer + " "); } System.out.println(); //对集合进行排序,默认是升序 Collections.sort(list); for(Integer integer:list){ System.out.print(integer + "--"); } System.out.println(); //利用二分查找法查找集合中是否有该元素,如果存在,则返回该元素的下标,如果不存在则返回-1 int index = Collections.binarySearch(list, 5); System.out.print("元素5在集合中的索引是:" + index); System.out.println(); //对数组进行逆序 Collections.reverse(list); System.out.print("逆序结果:"); for(Integer integer:list){ System.out.print(integer + " "); } System.out.println(); //随机排列指定的列表,使用默认的随机性源 Collections.shuffle(list); System.out.print("随机排列结果:"); for(Integer integer:list){ System.out.print(integer + " "); } System.out.println(); //交换集合(数组)中指定位置的指定元素 Collections.swap(list, 0, list.size()-1); System.out.print("交换第一个元素与最后一个元素的排列结果:"); for(Integer integer:list){ System.out.print(integer + " "); }// System.out.println(); //用指定的值替换掉集合中所有的元素// Collections.fill(list, 1);// System.out.print("替换以后的结果:");// for(Integer integer:list){// System.out.print(integer + " ");// } System.out.println(); //将一个集合的内容copy到另外一个集合 //第一个参数是目标集合,第二个参数是源集合 Collections.copy(newList, list); System.out.print("集合复制以后的结果:"); for(Integer integer:newList){ System.out.print(integer + " "); } System.out.println(); //返回集合中的最小值 Integer min =Collections.min(list); System.out.println("最小值是: " + min); //返回集合中的最大值 Integer max = Collections.max(list); System.out.println("最大值 :" + max); //按照指定的距离对于集合中的元素进行旋转 Collections.rotate(list, 2); System.out.print("旋转以后的结果:"); for(Integer integer:list){ System.out.print(integer + " "); } System.out.println(); //用新的值替换掉原来集合中所有的指定值 //第一个参数是集合名字,第二个参数是集合中的原有值,第三个参数是新值 Collections.replaceAll(list, 1, 99); System.out.print("旋转以后的结果:"); for(Integer integer:list){ System.out.print(integer + " "); } System.out.println(); }}
阅读全文
1 0
- API学习Collections
- java API 学习1 Collections
- Collections API
- ES6学习——集合(Collections):Map API
- ES6学习——集合(Collections):Set API
- API解读:Collections
- 关于 Java Collections API
- Java9新API--Collections、Streams
- java.util.Collections学习
- collections的学习知识
- collections的学习知识
- java.util.Collections学习
- c#学习之collections
- System.Collections 学习中。。。
- Java中Collections学习
- java Collections学习
- IOS学习笔记---collections
- Collections.sort();用法学习
- POJ1330Nearest Common Ancestors
- PHP生成缩略图
- QT问题系列(2)
- Beaglebone Black上使用Codesys开发EtherCAT(四)
- 斐波那契数列数列的三种时间复杂度的实现方法
- API学习Collections
- HDU2586How far away? LCA
- 数据库数据按指定格式导出到Excel
- 大学生活随笔
- <iframe>引用页面传值获取不到的问题
- FormData表单字段数据对象
- PHP 数组和字符串互相转换实现方法
- linux——文本处理——正则表达式
- 2的幂次方表示