Java中的数组排序
Java中的数组排序,一般是利用Arrays.sort(),这个方法是经过优化的快速排序。在Arrays种有多中形式的重载,在这里就不一一列举了。
数组排序的种类:
1.非降序排序, 非升序排序(就排序后数组元素排列的结果而言)
2.基本类型数据的排序,类类型数据的排序(就排序的对象而言)
排序示例:
int型数组的非降序排序:
- package sort;
- import java.util.Arrays;
- public class Main {
- public static void displayArray(int[] array) {
- for (int i: array) {
- System.out.print(i + " ");
- }
- System.out.println();
- }
- public static void main(String[] args) {
- int[] arr = new int[]{43, 84, 3, 8, 4, 7, 3, 75, 82, 748, 35};
- System.out.println("排序前:");
- displayArray(arr);
- Arrays.sort(arr);
- System.out.println("排序后:");
- displayArray(arr);
- }
- }
运行结果如下:
int型数组的非升序排序:
- package sort;
- import java.util.Arrays;
- import java.util.Comparator;
- public class Main {
- public static void displayArray(Integer[] array) {
- for (int i: array) {
- System.out.print(i + " ");
- }
- System.out.println();
- }
- public static void main(String[] args) {
- Integer[] arr = new Integer[]{43, 84, 3, 8, 4, 7, 3, 75, 82, 748, 35};
- System.out.println("排序前:");
- displayArray(arr);
- Arrays.sort(arr, new JX());
- System.out.println("排序后:");
- displayArray(arr);
- }
- }
- class JX implements Comparator<Integer> {
- @Override
- public int compare(Integer o1, Integer o2) {
- if (o1 <= o2) {
- return 1;
- }
- return -1;
- }
- }
运行结果如下:
类类型的非降序排序:
- package sort;
- import java.util.Arrays;
- public class Main {
- public static void displayArray(Student[] student) {
- for (Student s : student) {
- System.out.println(s);
- }
- }
- public static void main(String[] args) {
- Student[] s = new Student[5];
- s[0] = new Student("wwww", 20, 2.34);
- s[1] = new Student("kkkk", 2, 2.34);
- s[2] = new Student("pppp", 25, 3.34);
- s[3] = new Student("hhhh", 12, 4.34);
- s[4] = new Student("jjjj", 10, 5.34);
- System.out.println("排序前:");
- displayArray(s);
- Arrays.sort(s);
- System.out.println("排序后:");
- displayArray(s);
- }
- }
- /*
- * 根据年龄进行非降序排序
- */
- class Student implements Comparable<Student> {
- private String name;
- private int age;
- private double height;
- public Student(String name, int age, double height) {
- this.name = name;
- this.age = age;
- this.height = height;
- }
- @Override
- public int compareTo(Student o) {
- if (this.age <= o.age) {
- return -1;
- }
- return 1;
- }
- public String toString() {
- return "Name: " + name + " Age: " + age + " Height: " + height;
- }
- }
运行结果如下:
类类型的非升序排序:
- package sort;
- import java.util.Arrays;
- public class Main {
- public static void displayArray(Student[] student) {
- for (Student s : student) {
- System.out.println(s);
- }
- }
- public static void main(String[] args) {
- Student[] s = new Student[5];
- s[0] = new Student("wwww", 20, 2.34);
- s[1] = new Student("kkkk", 2, 2.34);
- s[2] = new Student("pppp", 25, 3.34);
- s[3] = new Student("hhhh", 12, 4.34);
- s[4] = new Student("jjjj", 10, 5.34);
- System.out.println("排序前:");
- displayArray(s);
- Arrays.sort(s);
- System.out.println("排序后:");
- displayArray(s);
- }
- }
- /*
- * 根据年龄进行非升序排序
- */
- class Student implements Comparable<Student> {
- private String name;
- private int age;
- private double height;
- public Student(String name, int age, double height) {
- this.name = name;
- this.age = age;
- this.height = height;
- }
- @Override
- public int compareTo(Student o) {
- if (this.age <= o.age) {
- return 1;
- }
- return -1;
- }
- public String toString() {
- return "Name: " + name + " Age: " + age + " Height: " + height;
- }
- }
运行结果如下:
根据指定属性对类类型数组排序:
- package sort;
- import java.util.Arrays;
- import java.util.Comparator;
- public class Main {
- public static void displayArray(Student[] student) {
- for (Student s : student) {
- System.out.println(s);
- }
- }
- public static void main(String[] args) {
- Student[] s = new Student[5];
- s[0] = new Student("wwww", 20, 2.34);
- s[1] = new Student("kkkk", 2, 2.34);
- s[2] = new Student("pppp", 25, 3.34);
- s[3] = new Student("hhhh", 12, 4.34);
- s[4] = new Student("jjjj", 10, 5.34);
- /*
- System.out.println("排序前:");
- displayArray(s);
- */
- System.out.println("按age进行非降序排序");
- Arrays.sort(s, new SortByAge());
- displayArray(s);
- System.out.println("按height进行非升序排序");
- Arrays.sort(s, new SortByHeight());
- displayArray(s);
- }
- }
- class Student {
- private String name;
- private int age;
- private double height;
- public Student(String name, int age, double height) {
- this.name = name;
- this.age = age;
- this.height = height;
- }
- public String toString() {
- return "Name: " + name + " Age: " + age + " Height: " + height;
- }
- public int getAge() {
- return age;
- }
- public double getHeight() {
- return height;
- }
- }
- /*
- * 按age进行非降序排序
- */
- class SortByAge implements Comparator<Student> {
- @Override
- public int compare(Student o1, Student o2) {
- if (o1.getAge() <= o2.getAge()) {
- return -1;
- }
- return 1;
- }
- }
- /*
- * 按height进行非升序排序
- */
- class SortByHeight implements Comparator<Student> {
- @Override
- public int compare(Student o1, Student o2) {
- if (o1.getHeight() - o2.getHeight() < 0.01) {
- return 1;
- }
- return -1;
- }
- }
运行结果如下:
Java中的数组排序的更多相关文章
- java中sort方法的自定义比较器写法(转载)
java中sort方法的自定义比较器写法 摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collecti ...
- java中Comparator的用法 -- 实现集合和数组排序
在java中,如果要对集合对象或数组对象进行排序,需要实现Comparator接口以达到我们想要的目标. 接下来我们模拟下在集合对象中对日期属性进行排序 一.实体类Step package com.l ...
- Java 中的数组操作
前言 在Java中,有很多封装好的类可以用来操纵数组(排序,复制等等),使得数组使用起来非常的方便.这就是高级语言带来的好处. 代码示例 - 一维数组 package test; import jav ...
- 数组在C++和java中的区别
几乎所有的程序设计语言都支持数组.在C和C++中使用数组是很危险的.因为C和C++中的数组就是内存块.如果一个程序要访问其自身内存块之外的数组,或者在数组初始化之前使用它,都会产生难以预料的后果. j ...
- 【JAVA零基础入门系列】Day10 Java中的数组
什么是数组?顾名思义,就是数据的组合,把一些相同类型的数放到一组里去. 那为什么要用数组呢?比如需要统计全班同学的成绩的时候,如果给班上50个同学的成绩信息都命名一个变量进行存储,显然不方便,而且在做 ...
- 【Java学习笔记之十】Java中循环语句foreach使用总结及foreach写法失效的问题
foreach语句使用总结 增强for(part1:part2){part3}; part2中是一个数组对象,或者是带有泛性的集合. part1定义了一个局部变量,这个局部变量的类型与part2中的对 ...
- Java中常见的比较器的实现方法
在Java中经常会涉及到对象数组的排序问题,那么就涉及到对象之间的比较问题. 通常对象之间的比较可以从两个方面去看: 第一个方面:对象的地址是否一样,也就是是否引用自同一个对象.这种方式可以直接使用& ...
- javascript中对两个对象进行排序 和 java中的两个对象排序
javascript中的对象数组排序 一 定义一个对象数组 var text = [{"name":"张","age":24},{" ...
- 【Java】Java中的Collections类——Java中升级版的数据结构【转】
一般来说课本上的数据结构包括数组.单链表.堆栈.树.图.我这里所指的数据结构,是一个怎么表示一个对象的问题,有时候,单单一个变量声明不堪大用,比如int,String,double甚至一维数组.二维数 ...
随机推荐
- POJ 2406 Power Strings KMP运用题解
本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...
- 调用webservice查询手机号码归属地信息
Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务.在这里我们使用soap协议往webservice发送信息,然 ...
- LumiSoft.Net邮件接收乱码问题解决
本文非本人编写,转载自:http://www.cnblogs.com/youngerliu/archive/2013/05/27/3101488.html 今天遇到用LumiSoft.Net这个组件收 ...
- JAVA反射机制学�
JAVA反射机制:对于随意一个类,都可以知道这个类的全部属性和方法:对于随意一个对象,都可以调用它的随意一个方法和属性:这样的动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制. J ...
- iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)
前言:一个路径可以包含由一个或者多个shape以及子路径subpath,quartz提供了很多方便的shape可以直接调用.例如:point,line,Arc(圆弧),Curves(曲线),Ellip ...
- apache常见错误汇总
<>问题: Access forbidden! You don't have permission to access the requested directory. There is ...
- Spark_Api_图解
- MySQL查询缓存详解
一:缓存条件,原理 MySQL Query Cache是用来缓存我们所执行的SELECT语句以及该语句的结果集,MySql在实现Query Cache的具体技术细节上类似典型的KV存储,就是将SELE ...
- javascript 关于语义化作用的理解
看代码实例1 var a=1; function m(a){ //此处为形参第一个传入函数的参数,既为arguments[0] alert(a); //此处a为与形参绑定的 } m(a);//1 此时 ...
- Android图片选择器--仿QQ
当做一款APP,需要选择本地图片时,首先考虑的无疑是系统相册,但是Android手机五花八门,再者手机像素的提升,大图无法返回等异常因数,导致适配机型比较困难,微信.QQ都相继的在自己的APP里集成了 ...