4.Java比较器
比较对象的大小,使用到两个接口:Comparable, Comparator
Comparable:自然排序
String、包装类等实现了Comparable接口,重写了compareTo()方法,给出了比较两个对象大小的方式
重写compareTo()的规则:
如果this大于形参对象obj,返回正整数
如果this小于形参对象obj,返回负整数
如果this等于形参对象obj,返回0
实现方法:自定义类要实现Comparable接口、重写compareTo()方法(指明如何对自定义类排序)
public void test1(){
Goods[] goods = new Goods[4];
goods[0] = new Goods(14,"abc");
goods[1] = new Goods(15,"def");
goods[2] = new Goods(16,"ghi");
goods[3] = new Goods(17,"jklmn");
Arrays.sort(goods);
System.out.println(Arrays.toString(goods));
//[Goods{price=14.0, name='abc'}, Goods{price=15.0, name='def'}, Goods{price=16.0, name='ghi'}, Goods{price=17.0, name='jklmn'}]
}
//实现Comparable
public class Goods implements Comparable{
private double price;
private String name;
/*
构造器、get、set、toString...
*/
//重写compareTo
@Override
public int compareTo(Object o) {
if(o instanceof Goods){
Goods goods = (Goods)o;
if (goods.getPrice() > this.price){
return -1;
}
else if (goods.getPrice() < this.price){
return 1;
}
return 0;
}
throw new RuntimeException("传入的对象类型非法!");
}
}
Comparator:定制排序
某些类没有实现Comparable接口,且不方便修改代码;
或者实现的Comparable接口排序规则不适合当前的操作。
实现的方法:
- new Comparator()并重写compare(Object o1, Object o2)方法
- Comparator传递给sort方法(如Collections.sort或Arrays.sort)
public void test2(){
//字符串从大到小排序
String[] arr = new String[]{"yyy", "abc", "wkm", "sly", "wp"};
Arrays.sort(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof String && o2 instanceof String){
String str1 = (String) o1;
String str2 = (String) o2;
return -str1.compareTo(str2);
}
return 0;
}
});
System.out.println(Arrays.toString(arr));//[yyy, wp, wkm, sly, abc]
}
5.System类
System类代表系统,位于java.lang包。
类的构造器是private,成员变量和方法是static
成员变量:in:标准输入流(键盘);out:标准输出流(显示器);标准错误输出流:err(显示器)
成员方法:
-
native long currentTimeMillis(): 获取时间戳
-
void exit(int status):退出程序。0代表正常退出,非0代表异常退出。使用该方法可以在图形界面编程中实现程序的退出功能
-
void gc():请求系统进行垃圾回收
-
String getProperty(String key):获得系统中属性名为key的属性对应的值。
属性 属性名 java.version java运行时环境 java.home java安装目录 os.version 操作系统版本 os.name 操作系统名称 user.name 用户名称 user.home 用户主目录 user.dir 用户当前工作目录
6.Math类
java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回值类型一般为double型。
函数名 | 作用 |
---|---|
abs | 绝对值 |
acos、asin、atan、cos、sin、tan | 三角函数 |
pow(double a,double b) | a的b次方 |
log | 取对数 |
exp | e为底数 |
max(double a,double b) | |
min(double a,double b) | |
random() | 返回0.0-1.0的随机数 |
long round(double a) | double型数据a转换为long型(四舍五入) |
toDegrees(double angrad) | 弧度—>角度 |
toRadians(double angdeg) | 角度—>弧度 |
sqrt | 平方根 |
7.BigInteger和BigDecimal
BigInteger可以表示不可变的任意精度的整数
- 构造器:
BigInteger(String val):根据字符串构建BigInteger对象
- 常用方法:
- public BigInteger abs():返回此 BigInteger 的绝对值的 BigInteger
- BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger
- BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger
- BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger
- BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。整数相除只保留整数部分
- BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger
- BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟(this % val) 的两个BigInteger的数组
- BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger
BigDecimal类支持不可变的、任意精度的有符号十进制定点数
- 构造器:
- public BigDecimal(double val)
- public BigDecimal(String val)
- 常用方法:
- public BigDecimal add(BigDecimal augend)
- public BigDecimal subtract(BigDecimal subtrahend)
- public BigDecimal multiply(BigDecimal multiplicand)
- public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)