当前位置:网站首页>Use cases of the arrays class
Use cases of the arrays class
2022-04-23 14:19:00 【White horse is not a horse·】
Catalog
Arrays class
Arrays Class is often used to manipulate array objects , There are two common functions , One is sorting , One is to transform into character string .
1. Convert to string output
One dimensional array output :Arrays.toString(data)
// When the array is one-dimensional :Arrays.toString(data)
int[] data=new int[]{
2,3,4,5};
System.out.println("data="+Arrays.toString(data));
//data=[2, 3, 4, 5]
Multidimensional array output :Arrays.deepToString(data)
// When the array is one-dimensional :Arrays.toString(data)
int[]] data=new int[][]{
{
2,3,4,5},{
2,3,4},{
4,3,1}};
System.out.println("data="+Arrays.deepToString(data));
//data=[[2, 3, 4, 5], [2, 3, 4], [4, 3, 1]]
2. Sort
Take an integer array as an example , Sort
2.1 One dimensional array sort ( The default is ascending ):Arrays.sort(data)
int[] data=new int[]{
2,5,4,1};
Arrays.sort(data);
System.out.println("data="+Arrays.toString(data));
//data=[1, 2, 4, 5]
2.2 One dimensional array sort ( Custom sort ):Arrays.sort(data)
- If you need to customize the order of elements in the array , Need to be right sort Comparator Comparator Class compare Method , among Comparator Is an anonymous inner class .
// The comparator needs to be calibrated Comparator Class compare Method
Integer[] data=new Integer[]{
2,5,4,1};
Arrays.sort(data,new Comparator<Integer>(){
@Override
public int compare(Integer a,Integer b){
return b-a;
}
});
System.out.println("data="+Arrays.toString(data));
// have access to lambda Expression shortens anonymous inner classes
Integer[] data=new Integer[]{
2,5,4,1};
Arrays.sort(data,(a,b)-> b-a);
System.out.println("data="+Arrays.toString(data));
2.3 Multi dimensional array sorting ( Custom sort ):Arrays.sort(data)
// First, the array is arranged in ascending order according to the first element of the array ,
// If equal , Then arrange in reverse order according to the second element
int[][] data=new int[][]{
{
3,1,2},{
3,2,3},{
1,2,3}};
Arrays.sort(data,(a,b)->{
if(a[0]==b[0]) return a[1]-b[1];
return b[0]-a[0];
});
System.out.println("data="+Arrays.deepToString(data));
//data=[[3, 1, 2], [3, 2, 3], [1, 2, 3]]
3. Array to set Arrays.asList()( The easiest way )
String[] string={
"2","3","a","b"};
List<String> list=new Arraylist<>(Arrays.asList(string));
list.add("3");
System.out.println("list="+lsit);
Be careful :
- Arrays.asList(string) It's the method of the object , The passed in must be an object array , Not the basic type , Therefore, it is necessary to use packaging .
- Arrays.asList(string), The object is Arrays An inner class , Not at all List aggregate , Therefore, there is no function to modify and add , We need to turn him into ArrayList aggregate .
4. ( Supplementary knowledge ) Anonymous inner class ( Inner class )
4.1 What is an inner class :
1) Classes defined in classes
4.2 The role of inner classes :
1) You can access the data within the scope of this class definition , Include private Decorated private data .
2) Can solve Java The defect of single inheritance .
4.3 What is an anonymous inner class :
1) There is no intrinsic name of a class , Defined in method , Block of code .
2) effect : Easy to create subclass objects , The ultimate goal is to simplify coding
3) The format is as follows :
Animal a=new Animal(){
public void run(){
};
5. ( Supplementary knowledge )Lambda expression
- The simplified function is the anonymous inner class of the interface , The main purpose is to simplify the code .
- In the face of Arrays.sort For custom sorting Lambda expression .
版权声明
本文为[White horse is not a horse·]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231407525534.html
边栏推荐
- 金融行业云迁移实践 平安金融云整合HyperMotion云迁移解决方案,为金融行业客户提供迁移服务
- 01-nio basic ByteBuffer and filechannel
- 修改Firebase Emulators的默认侦听IP
- 统信UOS PHP7.2.3升级至PHP7.2.24
- dp-能量项链
- squid代理
- Redis数据库讲解(一)
- Web page, adaptive, proportional scaling
- On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication
- RecyclerView细节研究-RecyclerView点击错位问题的探讨与修复
猜你喜欢
On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication
RecyclerView进阶使用-实现仿支付宝菜单编辑页面拖拽功能
线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口
Pass in external parameters to the main function in clion
sar命令详解
Recyclerview advanced use (I) - simple implementation of sideslip deletion
Operation instructions of star boundary text automatic translator
Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface
云迁移的六大场景
剑指offer刷题(1)--面向华为
随机推荐
星界边境文本自动翻译机使用说明
关于NodeJS中JSON5的相关配置和使用
预览CSV文件
Use the executors class to quickly create a thread pool
百度笔试2022.4.12+编程题目:简单整数问题
线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口
GFS分布式文件系统(理论)
常见存储类型和FTP主被动模式解析
elk安装
Pass in external parameters to the main function in clion
星界边境Starbound创意工坊订阅的mod的存放路径
Research on recyclerview details - Discussion and repair of recyclerview click dislocation
一些小小小小记录~
mysql锁数据库锁
Uni app message push
关于云容灾,你需要知道这些
第四届“传智杯”全国大学生IT技能大赛(决赛B组) 题解
Algorithem_ReverseLinkedList
Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
About the configuration and use of json5 in nodejs