java.math包
BigDecimal
BigInteger
MathContext
RoundingMode這是枚舉
BigDecimal
不可變的,任意精度的有符號十進制數,適合商業計算中,數字很長的數字運算
方法add,subtract,multiply,dividejava
package java_20180210_api_datetime; import java.math.BigDecimal; import java.math.MathContext; public class BigDemo { public static void main(String[] args) { // long l1=1213133242353245345345; BigDecimal d1=new BigDecimal("20"); BigDecimal d3=new BigDecimal("5"); System.out.println(d1.add(d3)); System.out.println(d1.subtract(d3)); System.out.println(d1.multiply(d3)); System.out.println(d1.divide(d3)); System.out.println(d1.doubleValue()); System.out.println(d1.toString()); System.out.println(d1.toPlainString()); BigDecimal d2=new BigDecimal("134.56", new MathContext(2)); System.out.println(d2.toPlainString()); } }
數學運算類
Math
Random
Math是java.lang中的類
Random是java.util中的類設計模式
package java_20180211_api_math; import java.util.Random; public class MathDemo { public static void main(String[] args) { System.out.println(Math.abs(-12.34)); System.out.println(Math.ceil(5.324)); System.out.println(Math.floor(-234.876)); for (int i = 0; i < 10; i++) { System.out.println(Math.random()); } System.out.println("=============="); int i = 0; do { i++; int max=100; int min=60; Random random = new Random(); int s = random.nextInt(max)%(max-min+1)+min; System.out.print(s); System.out.print(" "); } while (i<10); System.out.println("=============="); //下面這個5是種子值 Random r1=new Random(5); for (int j = 0; j < 10; j++) { //加10就表明10-20之間的一個數字 System.out.println(r1.nextInt(11)+10); } } }
比較
Comparable
Comparator
Comparable是一個函數式接口(只有一個抽象方法的接口)。java.lang.Comparable<T>
爲現有類提供一種比較的能力
Comparator是一個函數式接口。java.util.Comparator<T>
將兩個類進行比較,在集合排序中會大量使用
兩者比較
Comparable接口爲子類提供比較的能力
Comparator接口封裝了比較的邏輯,可用於策略設計模式,其子類常做爲排序方法中的參數api
package java_20180211_api_math; public class User implements Comparable<User> { int age; @Override public int compareTo(User o) { if (age > o.age) { return 1; } else if (age < o.age) { return -1; } return 0; } } ================== package java_20180211_api_math; public class CompDemo { public static void main(String[] args) { User u1=new User(); u1.age=40; User u2=new User(); u2.age=20; if (u1.compareTo(u2)>0) { System.out.println("u1>u2"); } else if(u1.compareTo(u2)<0){ System.out.println("u1<u2"); }else { System.out.println("u1==u2"); } } } ===================== package java_20180211_api_math; import java.util.Comparator; public class CompDemo2 implements Comparator<User> { @Override public int compare(User o1, User o2) { if (o1.age > o2.age) { return 1; } else if (o1.age < o2.age) { return -1; } return 0; } }
迭代
Iterable
Iterator
java.lang.Iterable<T>
若是想要一個類放在foreach中遍歷,就必須實現Iterable<T>這個接口,通常都是數組數組
package java_20180211_api_math; import java.util.Iterator; class Student { String name; public Student(String name) { this.name = name; } } class Clazz implements Iterable<Student> { Student[] students; public Clazz(int num) { students = new Student[num]; } int index = 0; public void add(Student s) { students[index++] = s; } @Override public Iterator<Student> iterator() { return new Iterator<Student>() { int index = 0; @Override public Student next() { return students[index++]; } @Override public boolean hasNext() { return index != students.length; } }; } } public class IterDemo { public static void main(String[] args) { Clazz c1 = new Clazz(3); Student s1 = new Student("s1"); Student s2 = new Student("s2"); Student s3 = new Student("s3"); c1.add(s1); c1.add(s2); c1.add(s3); // for (Student stu : c1.students) { // System.out.println(stu.name); // } for (Student stu : c1) { System.out.println(stu.name); } } }