Arrays-sort-的用法
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 < 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<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
- 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