当前位置:网站首页>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);}}
边栏推荐
猜你喜欢
软件测试面试常见问题及答案(发散思维、接口、性能、概念、)
HD Satellite Map Browser
MySQL indexes
第三方免费开放API 获取用户IP 并查询其地理位置
常用命令之思科常用基础配置
Ovie map computer terminal and mobile terminal can not be used, is there any alternative map tool
软件测试外包公司怎么样?有什么好处和坏处?为什么没人去?
Django实现对数据库数据增删改查(二)
How much do you know about the mobile APP testing process specifications and methods?
本体开发日记05-努力理解SWRL(下)
随机推荐
恶意软件查杀工具分享
电脑硬件基础知识科普
本体开发日记01-Jena配置环境变量
Ontology Development Diary 03-Understanding Code
8.Properties属性集合
JMeter参数化4种实现方式
try catch 对性能影响
TypeScript Brief (1)
本体开发日记04-努力理解protege的某个方面
性能测试报告包括哪些内容?模板范文哪里找?看这里
"The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
初窥门径代码起手,Go lang1.18入门精炼教程,由白丁入鸿儒,首次运行golang程序EP01
条件和递归
MySQL event_single event_timed loop event
Consolidation of Questionnaire Questions and Answers
年薪40W测试工程师成长之路,你在哪个阶段?
4.字符流
1.线程简介
2.线程创建
本体开发日记05-努力理解SWRL(中)