Arrays-sort-的用法
创始人
2024-05-31 00:22:43
0
  • 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 i or j*         is out of range (i < 0 || i >= list.size()*         || j < 0 || j >= 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 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 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{
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

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...