Java中Array.sort()的几种用法(需要初始化要排序的对象)
======================================================
1、Arrays.sort(int[] a)
这种形式是对一个数组的所有元素进行排序,并且是按从小到大的顺序。
举例如下(点“+”可查看代码):
import java.util.Arrays;
publicclassMain {4publicstaticvoid main(String[] args) {
int[] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5};
Arrays.sort(a);
for(int i = 0; i < a.length; i ++) {
System.out.print(a[i] + " ");
}
}
}
运行结果如下:
0 1 2 3 4 5 6 7 8 9
---------------------------------------------------------
2、Arrays.sort(int[] a, int fromIndex, int toIndex)
这种形式是对数组部分排序,也就是对数组a的下标从fromIndex到toIndex-1的元素排序,注意:下标为toIndex的元素不参与排序哦!
举例如下(点“+”可查看代码):
import java.util.Arrays;
publicclassMain {4publicstaticvoid main(String[] args) {
int[] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5};
Arrays.sort(a, 0, 3);//3是指0位置开始后包括零位置 后的三个8for(int i = 0; i < a.length; i ++) {
System.out.print(a[i] + " ");
}
}
}
运行结果如下:
7 8 9 2 3 4 1 0 6 5
上例只是把 9 8 7排列成了7 8 9
----------------------------------------------------------
3、public static <T> void sort(T[] a,int fromIndex, int toIndex, Comparator<? super T> c)
上面有一个拘束,就是排列顺序只能是从小到大,如果我们要从大到小,就要使用这种方式
这里牵扯到了Java里面的泛型,如果读者不是很了解,可以暂时不去管它,如果真的很想了解,建议查阅上面我推荐的那本书,上面有详细的介绍。
读者只需要读懂下面的例子就可以了,其实就是多了一个Comparator类型的参数而已。
package test;
import java.util.Arrays;
import java.util.Comparator;
publicclassMain {publicstaticvoid main(String[] args) {
//注意,要想改变默认的排列顺序,不能使用基本类型(int,double, char)//而要使用它们对应的类
Integer[] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5};
//定义一个自定义类MyComparator的对象
Comparator cmp = new MyComparator();
Arrays.sort(a, cmp);
for(int i = 0; i < a.length; i ++) {
System.out.print(a[i] + " ");
}
}
}
//Comparator是一个接口,所以这里我们自己定义的类MyComparator要implents该接口//而不是extends Comparator
class MyComparator implements Comparator<Integer>{
@Overridepublicint compare(Integer o1, Integer o2) {
//如果n1小于n2,我们就返回正值,如果n1大于n2我们就返回负值,//这样颠倒一下,就可以实现反向排序了if(o1 < o2) {
return1;
}elseif(o1 > o2) {
return -1;
}else {
return0;
}
}
}
Java中Array.sort()的几种用法(需要初始化要排序的对象)的更多相关文章
- Java中Array.sort()的几种用法
****************************************************** * 精品书籍推荐:<Java从入门到经通> * 本书系统全面.浅显易懂,非常适 ...
- java中 this 关键字的三种用法
Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...
- java中Array(数组)的用法
8.Array(数组) 数组是作为对象来实现的.(really occupy the memopry,真实的占用内存 ) An array is a data structure that st ...
- Java中this关键字的几种用法
1 . 当成员变量和局部变量重名时,在方法中使用this时,表示的是该方法所在类中的成员变量.(this是当前对象自己) 如:public class Hello { String s = " ...
- java基础——Collections.sort的两种用法
Collections是一个工具类,sort是其中的静态方法,是用来对List类型进行排序的,它有两种参数形式: public static <T extends Comparable<? ...
- java基础 -- Collections.sort的两种用法
/** * @author * @version * 类说明 */ package com.jabberchina.test; import java.util.ArrayList; import j ...
- java基础—— Collections.sort的两种用法
package com.jabberchina.test; import java.util.ArrayList; import java.util.Collections; import java. ...
- Java中关于枚举的7种用法
1.定义常量: public enum Color { RED,ORANGE,YELLOW,GREEN,INDIGO,BLUE,PURPLE } 2.用于switch: enum Color { RE ...
- 分享js中的 sort 另一种用法
// 看上去正常的结果: ['Google', 'Apple', 'Microsoft'].sort(); // ['Apple', 'Google', 'Microsoft']; // apple排 ...
随机推荐
- 安装redis入门
redis官网:redis.io redis版本用的是redis-3.2.2 $ wget http://download.redis.io/releases/redis-3.2.2.tar.gz $ ...
- Apache Shiro系列(1)
Apache Shiro是啥呢,安全框架. 360百科是这么描述的: Apache Shiro(日语"堡垒(Castle)"的意思)是一个强大易用的Java安全框架, ...
- 使用Enyim.Caching访问阿里云的OCS
阿里云的开放式分布式缓存(OCS)简化了缓存的运维管理,使用起来很方便,官方推荐的.NET访问客户端类库为 Enyim.Caching,下面对此做一个封装. 首先引用最新版本 Enyim.Cachin ...
- Javascript中构造函数与new命令
典型的面向对象编程语言(比如C++和Java),存在“类”(class)这个概念.所谓“类”就是对象的模板,对象就是“类”的实例.但是,在JavaScript语言的对象体系,不是基于“类”的,而是基于 ...
- [转]实现文字跑马灯效果,scrolling text from right to left
<div> <marquee direction="left" behavior="scroll" scrollamount="10 ...
- Java学习心得之 HttpClient的GET和POST请求
作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Java学习心得之 HttpClient的GET和POST请求 1. 前言2. GET请求3 ...
- UIWebView加载本地html文件
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(, , KScreenWidth, KScreenHeight-)]; ...
- Reveal1.5破解,iOS_UI调试利器Reveal最新版本破解方法
Reveal1.0.7破解 1.官网下载最新版Reveal,拖动应用程序中,运行一次2.下载16进制编辑器"0xED" for mac(http://dl.vmall.com/c0 ...
- iOS-多线程之GCD(原创)
前言 GCD 全称 Grand Central DisPath NSOperation便是基于GCD的封装 基础知识 1.GCD的优势 (1)为多核的并行运算提出了解决方案 (2)GCD会自动利用更多 ...
- android性能优化练习:过度绘制
练习:https://github.com/zhangbz/AndroidUIPorblems 查看过度绘制 在开发者选项中开启"调试GPU过度绘制" 判断标准 无色:没有过度绘制 ...