Java数据结构:工具类Arrays类 、Collections类
代码、知识点来自中国大学MOOC-陈育良《Java核心技术》
整理如下
Arrays:
处理对象是数组
– 排序:对数组排序, sort/parallelSort。
– 查找:从数组中查找一个元素, binarySearch。
– 批量拷贝:从源数组批量复制元素到目标数组, copyOf。
– 批量赋值:对数组进行批量赋值, fill。
– 等价性比较:判定两个数组内容是否相同, equals。
import java.util.Arrays;
import java.util.Random;
public class ArraysTest {
public static void main(String[] args) {
testSort();
testSearch();
testCopy();
testFill();
testEquality();
}
public static void testSort() {//十个随机数的排序
Random r = new Random();
int[] a = new int[10];
for(int i=0;i<a.length;i++) {
a[i] = r.nextInt();
}
System.out.println("===============测试排序================");
System.out.println("排序前");
for(int i=0;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
System.out.println("排序后");
Arrays.sort(a);
for(int i=0;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
}
public static void testSearch() {//数据查找
Random r = new Random();
int[] a = new int[10];
for(int i=0;i<a.length;i++)
{
a[i] = r.nextInt();
}
a[a.length-1] = 10000;
System.out.println("===========测试查找============");
System.out.println("10000 的位置是" + Arrays.binarySearch(a, 10000));
}
public static void testCopy() {//数据拷贝
Random r = new Random();
int[] a = new int[10];
for(int i=0;i<a.length;i++)
{
a[i] = r.nextInt();
}
int[] b = Arrays.copyOf(a, 5);
System.out.println("===========测试拷贝前五个元素============");
System.out.print("源数组:");
for(int i=0;i<a.length;i++)
{
System.out.print(a[i] + ",");
}
System.out.println();
System.out.print("目标数组:");
for(int i=0;i<b.length;i++)
{
System.out.print(b[i] + ",");
}
System.out.println();
}
public static void testFill() {//批量赋值
int[] a = new int[10];
Arrays.fill(a, 100);
Arrays.fill(a, 2, 8, 200);
System.out.println("===========测试批量赋值============");
System.out.print("数组赋值后:");
for(int i=0;i<a.length;i++)
{
System.out.print(a[i] + ",");
}
System.out.println();
}
public static void testEquality() {//比较
int[] a = new int[10];
Arrays.fill(a, 100);
int[] b = new int[10];
Arrays.fill(b, 100);
System.out.println(Arrays.equals(a, b));
b[9] = 200;
System.out.println(Arrays.equals(a, b));
}
}
输出:
===============测试排序================
排序前
-142888888,208558592,-1591295752,982797157,1162159131,-1589667991,-1005790671,-401262119,-421864093,668889952,
排序后
-1591295752,-1589667991,-1005790671,-421864093,-401262119,-142888888,208558592,668889952,982797157,1162159131,
===========测试查找============
10000 的位置是-2
===========测试拷贝前五个元素============
源数组:-1858352171,1155597732,1069774667,-1361049035,2089876626,-2015654389,-833683061,445099456,340670221,1232298482,
目标数组:-1858352171,1155597732,1069774667,-1361049035,2089876626,
===========测试批量赋值============
数组赋值后:100,100,200,200,200,200,200,200,100,100,
true
false
Collections:
处理对象是 Collection及其子类
– 排序:对List进行排序, sort。
– 搜索:从List中搜索元素, binarySearch
– 批量赋值:对List批量赋值, fill。
– 最大、最小:查找集合中最大/小值, max, min
– 反序:将List 反序排列, reverse
import java.util.ArrayList;
import java.util.Collections;
public class CollectionsTest {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(12);
list.add(2);
list.add(19);
// 排序
Collections.sort(list);
// 检索
System.out.println("元素所在的索引值是:" + Collections.binarySearch(list, 12));
//最大最小
System.out.println("最大值:" + Collections.max(list));
System.out.println("最小值:" + Collections.min(list));
Collections.reverse(list); //翻转不需要用到排序
Collections.fill(list, 100); //全部赋值为100
}
}
输出:
元素所在的索引值是:2
最大值:19
最小值:1
对象比较排序
对象实现Comparable接口(需要修改对象类)
compareTo方法
• > 返回1, ==返回0, <返回-1
– Arrays和Collections在进行对象sort时,自动调用该方法
新建Comparator(适用于对象类不可更改的情况)
– compare方法
• > 返回1, ==返回0, <返回-1
– Comparator比较器将作为参数提交给工具类的sort方法
实现Comparable接口
import java.util.Arrays;
public class Person implements Comparable<Person> {//Person继承Comparable接口
String name;
int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public int compareTo(Person another) {
int i = 0;
i = name.compareTo(another.name); // 使用字符串的比较
if (i == 0) {
// 如果名字一样,比较年龄, 返回比较年龄结果
return age - another.age;
} else {
return i; // 名字不一样, 返回比较名字的结果.
}
}
public static void main(String... a) {
Person[] ps = new Person[3];
ps[0] = new Person("Tom", 20);
ps[1] = new Person("Mike", 18);
ps[2] = new Person("Mike", 20);
Arrays.sort(ps);
for (Person p : ps) {
System.out.println(p.getName() + "," + p.getAge());
}
}
}
输出:
Mike,18
Mike,20
Tom,20
新建比较器
public class Person2 {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Person2(String name, int age)
{
this.name = name;
this.age = age;
}
}
import java.util.Arrays;
import java.util.Comparator;
public class Person2Comparator implements Comparator<Person2> {//比较器同样继承Comparator接口
public int compare(Person2 one, Person2 another) {//与实现Comparable接口不同,此处是compare方法
//自己定义的比较方法
int i = 0;
i = one.getName().compareTo(another.getName());
if (i == 0) {
// 如果名字一样,比较年龄,返回比较年龄结果
return one.getAge() - another.getAge();
} else {
return i; // 名字不一样, 返回比较名字的结果.
}
}
public static void main(String[] args) {
Person2[] ps = new Person2[3];
ps[0] = new Person2("Tom", 20);
ps[1] = new Person2("Mike", 18);
ps[2] = new Person2("Mike", 20);
Arrays.sort(ps, new Person2Comparator());//比较器作为参数提交给工具类的sort方法
for (Person2 p : ps) {
System.out.println(p.getName() + "," + p.getAge());
}
}
}
输出:
Mike,18
Mike,20
Tom,20