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 method
        final 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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值