当前位置: 首页 > news >正文

Arrays-sort-的用法

  • 1.集合交换元素

Collections.swap(List<?> list, int i, int j);

源码:

    /*** Swaps the elements at the specified positions in the specified list.* (If the specified positions are equal, invoking this method leaves* the list unchanged.)** @param list The list in which to swap elements.* @param i the index of one element to be swapped.* @param j the index of the other element to be swapped.* @throws IndexOutOfBoundsException if either <tt>i</tt> or <tt>j</tt>*         is out of range (i &lt; 0 || i &gt;= list.size()*         || j &lt; 0 || j &gt;= list.size()).* @since 1.4*/@SuppressWarnings({"rawtypes", "unchecked"})public static void swap(List<?> list, int i, int j) {// instead of using a raw type here, it's possible to capture// the wildcard but it will require a call to a supplementary// private methodfinal List l = list;l.set(i, l.set(j, l.get(i)));}

测试过程如下:

import java.util.ArrayList;
import java.util.Collections;public class SwapDemo {public static void main(String[] args) {ArrayList<Object> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);System.out.println(list);Collections.swap(list, 0, 1);System.out.println(list);}
}

结果如下:

[1, 2, 3]
[2, 1, 3]Process finished with exit code 0
  • 2.Java的Arrays类中有一个sort()方法,该方法是Arrays类的静态方法,在需要对数组进行排序时,非常的好用,但是sort()的参数有好几种

  1. Arrays.sort(int[] a)

这种形式是对一个数组的所有元素进行排序,并且是按从小到大的顺序。

 1 import java.util.Arrays;2 3 public class Main {4     public static void main(String[] args) {5         6         int[] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5};7         Arrays.sort(a);8         for(int i = 0; i < a.length; i ++) {9             System.out.print(a[i] + " ");
10         }
11     }
12 
13 }
// 运行结果如下:// 0 1 2 3 4 5 6 7 8 9 

2、Arrays.sort(int[] a, int fromIndex, int toIndex)

这种形式是对数组部分排序,也就是对数组a的下标从fromIndex到toIndex-1的元素排序,注意:下标为toIndex的元素不参与排序

 1 import java.util.Arrays;2 3 public class Main {4     public static void main(String[] args) {5         6         int[] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5};7         Arrays.sort(a, 0, 3);8         for(int i = 0; i < a.length; i ++) {9             System.out.print(a[i] + " ");
10         }
11     }
12 
13 }
//  运行结果如下:// 7 8 9 2 3 4 1 0 6 5 

3.public static void sort(T[] a,int fromIndex, int toIndex, Comparator<? super T> c)上面有一个拘束,就是排列顺序只能是从小到大,如果我们要从大到小,就要使用这种方式这里牵扯到了Java里面的泛型

 1 package test;2 3 import java.util.Arrays;4 import java.util.Comparator;5 6 public class Main {7     public static void main(String[] args) {8         //注意,要想改变默认的排列顺序,不能使用基本类型(int,double, char)9         //而要使用它们对应的类
10         Integer[] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5};
11         //定义一个自定义类MyComparator的对象
12         Comparator cmp = new MyComparator();
13         Arrays.sort(a, cmp);
14         for(int i = 0; i < a.length; i ++) {
15             System.out.print(a[i] + " ");
16         }
17     }
18 }
19 //Comparator是一个接口,所以这里我们自己定义的类MyComparator要implents该接口
20 //而不是extends Comparator
21 class MyComparator implements Comparator<Integer>{
22     @Override
23     public int compare(Integer o1, Integer o2) {
24         //如果o1小于o2,我们就返回正值,如果o1大于o2我们就返回负值,
25         //这样颠倒一下,就可以实现反向排序了
26         if(o1 < o2) { 
27             return 1;
28         }else if(o1 > o2) {
29             return -1;
30         }else {
31             return 0;
32         }
33     }
34     
35 }
// 运行结果如下:// 9 8 7 6 5 4 3 2 1 0 

Arrays.copyOfRange用法

public class Test {public static void main(String[] args) {int[] array = {0, 1, 2, 3, 4, 5, 6};int[] array2 = Arrays.copyOfRange(array, 2, 4);System.out.println(Arrays.toString(array2));}
}

输出结果:

[2, 3]

Arrays.fill()用法

  • 例如:Arrays.fill(arr,-666) 将arr数组中的所有元素置为-666
http://www.lryc.cn/news/34107.html

相关文章:

  • 华为OD机试真题Java实现【寻找相同子串】真题+解题思路+代码(20222023)
  • 性能指标 确定性能目标 性能场景设计
  • ENVI_Classic:快速入门_菜单栏常见功能的基本介绍
  • 【深度探讨】公共部门在选择区块链平台时要考虑的6个方面
  • 基于阿里云物联网平台设计的实时图传系统_采用MQTT协议传输图像
  • 42-Golang中的单元测试
  • python实现k_means聚类
  • 【批处理脚本】-3.3-exit命令详解
  • 如果读了我2011年求职前端开发的酸爽经历,希望你可以鼓起勇气继续向前
  • PTA:L1-016 查验身份证、L1-017 到底有多二、L1-018 大笨钟(C++)
  • springboot工厂模式解决if_else流程和问题点解决
  • 如何避免缓存击穿?使用GO语言实现sliglefight
  • 【浅学Java】MySQL索引七连炮
  • 扬帆优配|昔日白马股濒临退市,却6天5涨停!ST股突然集体爆发
  • Git 基础(一)—— Git 的安装及其配置
  • 什么是信息安全风险评估?企业如何做?
  • HBase---idea操作Hbase数据库并且映射到Hive
  • 剑指 Offer 61 扑克牌中的顺子
  • Spring 响应式编程-读书笔记
  • CI流水线的理解
  • OpenStack手动分布式部署Nova【Queens版】
  • centos7 oracle19c安装 ORA-01012: not logged on
  • 山东小巨人申报条件
  • 手写中实现并学习ahooks——useRequest
  • [手写OS]动手实现一个OS 之 准备工作以及引导扇区
  • JVM实战OutOfMemoryError异常
  • C++虚函数操作指南
  • Mybatis-Plus分页插件
  • Selenium Webdriver options的实用参数设置
  • 代码随想录算法训练营第七天|454.四数相加II 、 383. 赎金信 、 15. 三数之和 、18. 四数之和