Arrays类是集合中的一个工具类,其中所有的方法全部都是静态方法,下面是其在API文档中的描述,如
asList(T... a)
Returns a fixed-size list backed by the specified array.
这个方法的返回值是一个List集合,可以用一个List来接收。
copyOf(int[] original, int newLength)
Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.
这个方法的主要作用是将一个数组复制到另一个数组,而且还有不同的重载方法,但是效果都差不多,只是起始位置和终止位置不同。
sort(int[] a)
Sorts the specified array into ascending numerical order.
这个方法是排序的方法,可以直接对数组按照自然顺序进行排序,如果参数不是基本数据类型,则需要自定义一个比较器,前提是该比较器要实现Comparator接口,那么方法的描述变为:
sort(T[] a, Comparator<? super T> c)
Sorts the specified array of objects according to the order induced by the specified comparator.
当然,最常用的还是这个方法:
static String toString(int[] a)
Returns a string representation of the contents of the specified array.
toString方法可以直接将数组转化为字符串格式,这在打印数组的时候非常方便
下面以代码来说明这几个方法:
import java.util.Arrays;
public class TestArray {
public static void main(String[] args) {
// 创建一个整数数组a
int a[] = { 1, 3, 5, 4, 2 };
// 将整数数组a调用toString()方法打印
System.out.println("a=" + Arrays.toString(a));
// 创建数组b,复制a数组的一部分
int b[] = Arrays.copyOf(a, 3);
System.out.println("b=" + Arrays.toString(b));
// 对a调用sort()方法进行自然排序
Arrays.sort(a);
System.out.println("排序后的a:" + Arrays.toString(a));
}
}
这是运行结果:
a=[1, 3, 5, 4, 2]
b=[1, 3, 5]
sort排序后的a:[1, 2, 3, 4, 5]
说明b成功的复制了a的数组
下面我们来测试一下sort(T[] a, Comparator<? super T> c)方法,这个方法的第一个参数不是普通的基本数据类型,而是一个对象,所以需要用到重写的构造器,下面我们以学生对象为例,来按照学号进行排序,这里的学号是以字符串类型创建的:
import java.util.Arrays;
import java.util.Comparator;
public class TestSort {
public static void main(String[] args) {
// 创建四个学生类对象
Student stu1 = new Student("张三", "2017100", 19);
Student stu2 = new Student("李四", "2017103", 20);
Student stu3 = new Student("王五", "2017101", 20);
Student stu4 = new Student("赵六", "2017102", 21);
// 创建一个数组用来保存学生类对象,并依次赋值
Student[] stus = new Student[4];
stus[0] = stu1;
stus[1] = stu2;
stus[2] = stu3;
stus[3] = stu4;
// 调用Arrays的sort方法对数组内部排序
Arrays.sort(stus, new StudentComparator());
for (Student stu : stus) {
System.out.println(stu.toString());
}
}
/**
* 定义一个内部类,用来比较Student的大小
* @author Administrator
*
*/
static class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if (o1.num.compareTo(o2.num) > 0) {
return 1;
} else if (o1.num.compareTo(o2.num) < 0) {
return -1;
} else {
if (o1.age > o2.age) {
return 1;
} else if (o1.age < o2.age) {
return -1;
}
}
return 0;
}
}
}
/**
* 创建学生类对象,属性包括name, num, age
* @author Administrator
*
*/
class Student {
String name;
String num;
int age;
public Student(String name, String num, int age) {
super();
this.name = name;
this.num = num;
this.age = age;
}
@Override
/**
* 重写toString()方法
*/
public String toString() {
return "Student [name=" + name + ", num=" + num + ", age=" + age + "]";
}
}
下面是运行结果:
Student [name=张三, num=2017100, age=19]
Student [name=王五, num=2017101, age=20]
Student [name=赵六, num=2017102, age=21]
Student [name=李四, num=2017103, age=20]
可以看出这里的排序是按照学号的顺序来的,而之前的添加顺序并不是,说明比较器已经生效,这里的比较器是专门用来比较Student类,这个类是一种自定义类,如果需要比较其他的类,则也要实现相应的Comparator<T>的接口。