当前位置:网站首页>7.Collections tool class
7.Collections tool class
2022-08-09 09:32:00 【come here my bear】
Collections tool class
- Concept: Collection tool class, which defines common collection methods other than access
- Method:
- public static void reverse(List> list) reverses the order of elements in the collection
- public static void shuffle(List> list) Randomly reset the order of collection elements (shuffle)
- public static void sort(List list) sort in ascending order (the element type must implement the Comparable interface)
- binarySearch Binary Search
- copy copy
package com.jhkj.map;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;/*** Use of Collections tool class*/public class Demo4 {public static void main(String[] args) {ArrayList list = new ArrayList<>();list.add(1);list.add(8);list.add(5);list.add(9);list.add(78);System.out.println("Number of elements: " + list.size());System.out.println(list.toString());// sort sort default ascending orderSystem.out.println("-----------------");System.out.println("before sorting" + list.toString());Collections.sort(list);System.out.println("After sorting" + list.toString());// binarySearch binary searchint i = Collections.binarySearch(list, 5);System.out.println(i);// copy copyArrayList dest = new ArrayList<>();for (int k = 0; k < list.size(); k++) {dest.add(0);}Collections.copy(dest,list);System.out.println(dest.toString());// reverse reverseCollections.reverse(list);System.out.println("After inversion: " + list);// shuffleCollections.shuffle(list);System.out.println("After shuffling: " + list);// Supplement: convert list to arraySystem.out.println("---------list into array------");Integer[] array = list.toArray(new Integer[0]);System.out.println(array.length);System.out.println(Arrays.toString(array));// convert array to collectionSystem.out.println("----------The array is converted into a collection-------");String[] names = {"Zhang San", "Li Si", "Wang Wu"};// The collection is a restricted collection and cannot be added or removedList asList = Arrays.asList(names);System.out.println(asList);// When converting an array of basic types into a collection, it needs to be modified to a wrapper class// int[]Integer[] nums = {100,200,300,400};// iny[]List list1 = Arrays.asList(nums);System.out.println(list1);}}
边栏推荐
猜你喜欢
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(4.Building An OWL Ontology)
如何用数组实现环形队列
WAVE SUMMIT 2022深度学习开发者峰会
The div simulates the textarea text box, the height of the input text is adaptive, and the word count and limit are implemented
Web请求原理
2048小游戏成品源码
第三方免费开放API 获取用户IP 并查询其地理位置
软件测试的流程规范有哪些?具体要怎么做?
本体开发日记05-努力理解SWRL(上)
JMeter初探五-配置元件与参数化
随机推荐
MySQL Leak Detection and Filling (2) Sorting and Retrieval, Filtering Data, Fuzzy Query, Regular Expression
7.Collections工具类
Rights management model, ACL, RBAC and ABAC (steps)
5.Set接口与实现类
学习栈的心得和总结(数组实现)
div模拟textarea文本框,输入文字高度自适应,且实现字数统计和限制
GBase数据库产生迁移工具假死的原因是什么?
接口测试主要测试哪方面?需要哪些技能?要怎么学习?
JS-常用方法整理
Jfinal loading configuration file principle
Max Flow P
.equals ==
如何用数组实现环形队列
功能自动化测试实施的原则以及方法有哪些?
Domestic with Google earth software, see the download 19th level high-resolution satellite images so easy!
迭代
QT sets the icon of the exe executable
本体开发日记03-排错进行时
What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
MySQL Checking and Filling Leaks (5) Unfamiliar Knowledge Points