当前位置: 移动技术网 > IT编程>开发语言>Java > java中的arrays.sort()代码详解

java中的arrays.sort()代码详解

2019年07月19日  | 移动技术网IT编程  | 我要评论

arrays.sort(t[], comparator < ? super t > c) 方法用于对象数组按用户自定义规则排序.
官方java文档只是简要描述此方法的作用,并未进行详细的介绍,本文将深入解析此方法。

1. 简单示例

sort方法的使用非常的简单明了,下面的例子中,先定义一个比较dog大小的comparator,然后将其实例对象作为参数传给sort方法,通过此示例,你应该能够快速掌握arrays.sort()的使用方法。

import java.util.arrays;
import java.util.comparator;
class dog{
	int size;
	public dog(int s){
		size = s;
	}
}
class dogsizecomparator implements comparator<dog>{
	@override 
	  public int compare(dog o1, dog o2) {
		return o1.size - o2.size;
	}
}
public class arraysort {
	public static void main(string[] args) {
		dog d1 = new dog(2);
		dog d2 = new dog(1);
		dog d3 = new dog(3);
		dog[] dogarray = {d1, d2, d3};
		printdogs(dogarray);
		arrays.sort(dogarray, new dogsizecomparator());
		printdogs(dogarray);
	}
	public static void printdogs(dog[] dogs){
		for (dog d: dogs) 
		      system.out.print(d.size + " " );
		system.out.println();
	}
}

输出为:

2 1 3 
1 2 3 

2.使用策略模式

这是策略模式(strategypattern)的一个完美又简洁的示例,值得一提的是为什么这种场景下适合使用策略模式.

总体来说,策略模式允许在程序执行时选择不同的算法.比如在排序时,传入不同的比较器(comparator),就采用不同的算法.

根据上面的例子,假设你想要根据dog的重量来进行排序,可以像下面这样,创建一个新的比较器来进行排序:

class dog{
	int size;
	int weight;
	public dog(int s, int w){
		size = s;
		weight = w;
	}
}
class dogsizecomparator implements comparator<dog>{
	@override 
	  public int compare(dog o1, dog o2) {
		return o1.size - o2.size;
	}
}
class dogweightcomparator implements comparator<dog>{
	@override 
	  public int compare(dog o1, dog o2) {
		return o1.weight - o2.weight;
	}
}
public class arraysort {
	public static void main(string[] args) {
		dog d1 = new dog(2, 50);
		dog d2 = new dog(1, 30);
		dog d3 = new dog(3, 40);
		dog[] dogarray = {d1, d2, d3};
		printdogs(dogarray);
		arrays.sort(dogarray, new dogsizecomparator());
		printdogs(dogarray);
		arrays.sort(dogarray, new dogweightcomparator());
		printdogs(dogarray);
	}
	public static void printdogs(dog[] dogs){
		for (dog d: dogs) 
		      system.out.print("size="+d.size + " weight=" + d.weight + " ");
		system.out.println();
	}
}

执行结果:

size=2 weight=50 size=1 weight=30 size=3 weight=40 
size=1 weight=30 size=2 weight=50 size=3 weight=40 
size=1 weight=30 size=3 weight=40 size=2 weight=50 

comparator是一个接口,所以sort方法中可以传入任意实现了此接口的类的实例,这就是策略模式的主要思想.

3.为何使用”super”

如果使用“comparator<t>c”那是很简单易懂的,但是sort的第2个参数里面的<?supert>意味着比较器所接受的类型可以是t或者它的超类.为什么是超类呢?答案是:这允许使用同一个比较器对不同的子类对象进行比较.在下面的示例中很明显地演示了这一点:

import java.util.arrays;
import java.util.comparator;
class animal{
	int size;
}
class dog extends animal{
	public dog(int s){
		size = s;
	}
}
class cat extends animal{
	public cat(int s){
		size = s;
	}
}
class animalsizecomparator implements comparator<animal>{
	@override 
	  public int compare(animal o1, animal o2) {
		return o1.size - o2.size;
	}
	//in this way, all sub classes of animal can use this comparator.
}
public class arraysort {
	public static void main(string[] args) {
		dog d1 = new dog(2);
		dog d2 = new dog(1);
		dog d3 = new dog(3);
		dog[] dogarray = {d1, d2, d3};
		printdogs(dogarray);
		arrays.sort(dogarray, new animalsizecomparator());
		printdogs(dogarray);
		system.out.println();
		//when you have an array of cat, same comparator can be used.  
		cat c1 = new cat(2);
		cat c2 = new cat(1);
		cat c3 = new cat(3);
		cat[] catarray = {c1, c2, c3};
		printdogs(catarray);
		arrays.sort(catarray, new animalsizecomparator());
		printdogs(catarray);
	}
	public static void printdogs(animal[] animals){
		for (animal a: animals) 
		      system.out.print("size="+a.size + " ");
		system.out.println();
	}
}

输出结果:

size=2 size=1 size=3 
size=1 size=2 size=3 
size=2 size=1 size=3 
size=1 size=2 size=3 

4. 小结

与arrays.sort()相关的信息总结如下:

通用: super 类
策略设计模式(strategy pattern);
归并排序(merge sort): 时间复杂度 n*log(n);
java.util.collections#sort(list < t > list, comparator < ? super t > c)与arrays.sort 使用类似的思想.

总结

以上就是本文关于java中的arrays.sort()代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他java相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网