Math
java
package com.edu.math;
public class Demo01 {
public static void main(String[] args) {
//Math常用的静态方法
//1.abs绝对值 可以用来比较浮点数的大小
int abs = Math.abs(-9);
System.out.println(abs);
//2.pow 求幂
double pow = Math.pow(2,4);
System.out.println(pow);
//3.ceil 向上取整,返回>=改参数的最小整数,转为double
double ceil = Math.ceil(3.9);//4.0
System.out.println(ceil);
//4.floor 向下取整数,<= 转为double
double floor = Math.floor(-5.001);//-6.0
System.out.println(floor);
//5.round 四舍五入 转为long
long round = Math.round(5.51);
//6.sqrt 开方
double sqrt =Math.sqrt(9);
//7.random 返回0-1 0<=x<1之间的任何数
/*
Math.random()*6 返回的是 2<=x<6的数
思考:请写出获取a-b之间的一个随机整数,a,b都为整数
*/
for (int i =0;i<10;i++) {
System.out.println((int)(2+Math.random()*(7-2+1)));
}
}
}
Arrays类
javapackage com.edu.math; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class Arrays01 { public static void main(String[] args) { Integer[] integers = {1,20,90}; //1.遍历数组,显示数组的字符串形式 System.out.println(Arrays.toString(integers)); //2. 排序 sort直接影响实参,Arrays.sort(arr); Integer[] arr = {1,-1,7,0,89}; //定制排序 Arrays.sort(arr, new Comparator() { @Override public int compare(Object o1, Object o2) { Integer i1 = (Integer) o1; Integer i2 = (Integer) o2; return i2-i1;//逆序 } }); int[] arr1 = {1,-1,8,0,20}; buubleSort(arr1); //结合定制+冒泡 buuble02(arr1, new Comparator() { @Override public int compare(Object o1, Object o2) { int i1 = (Integer)o1; int i2 = (Integer)o2; return i2-i1; } }); System.out.println(Arrays.toString(arr1)); //3.数组元素的复制,从arr数组中拷贝arr.length个元素到数组中 Integer[] newArr = Arrays.copyOf(arr, arr.length); //4.数组填充 Integer[] num = new Integer[]{9,3,2}; Arrays.fill(num,99);//填充后数组变成{99,99,99} //5.equals 比较两个数组的元素是否完全一致 int[] num1 ={1,2,3,4}; int[] num2 ={2,1,3,4}; System.out.println(Arrays.equals(num1,num2));//对应下标元素比较 //6.将一组数据转换为List List<Integer> asList = Arrays.asList(1,2,3,5,4); System.out.println(asList); } //使用冒泡完成排序 public static void buubleSort(int[] nums){ for(int i=0;i<nums.length-1;i++){ for (int j =0;j<nums.length-1-i;j++){ if(nums[j]>nums[j+1]){ int temp = nums[j]; nums[j]=nums[j+1]; nums[j+1]=temp; } } } for (int i:nums){ System.out.print(i+"\t"); } } //结合定制+冒泡 public static void buuble02(int[] nums,Comparator c){ for(int i=0;i<nums.length-1;i++){ for (int j =0;j<nums.length-1-i;j++){ if(c.compare(nums[j],nums[j+1])>0){ int temp = nums[j]; nums[j]=nums[j+1]; nums[j+1]=temp; } } } } }
练习题
javapackage com.edu.math; import java.util.Arrays; import java.util.Comparator; public class Arrays02 { public static void main(String[] args) { Book[] books = new Book[4]; books[0] = new Book("112",100); books[1] = new Book("22222",90); books[2] = new Book("3",5); books[3] = new Book("4",12); /* //(1)price从小到大 Arrays.sort(books, new Comparator() { @Override public int compare(Object o1, Object o2) { Book book1 = (Book)o1; Book book2 = (Book)o2; double gap = book2.getPrice()-book1.getPrice(); if(gap>0){ return -1; }else if (gap<0){ return 1; }else { return 0; } } }); System.out.println(Arrays.toString(books));*/ /* //(2)price从大到小 Arrays.sort(books, new Comparator() { @Override public int compare(Object o1, Object o2) { Book book1 = (Book)o1; Book book2 = (Book)o2; double gap = book2.getPrice()-book1.getPrice(); if(gap>0){ return 1; }else if (gap<0){ return -1; }else { return 0; } } }); System.out.println(Arrays.toString(books));*/ //(3)按书名从大到小 Arrays.sort(books, new Comparator() { @Override public int compare(Object o1, Object o2) { Book book1 = (Book) o1; Book book2 = (Book) o2; return book2.getName().length() - book1.getName().length(); } }); System.out.println(Arrays.toString(books)); } } class Book{ private String name; private double price; public Book(String name,int price){ this.name = name; this.price = price; } public double getPrice(){ return this.price; } public String getName(){ return name; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", price=" + price + '}'; } }
System类
java
package com.edu.math;
public class System01 {
public static void main(String[] args) {
//(1)exit退出当前程序
System.out.println("1");
//0表示正常退出
//System.exit(0);
System.out.println("2");
//(2)arraycopy 复制数组元素,适合底层调用
int[] src = {1,2,3};
int[] dest = new int[3];
//参数含义src:源数组 srcPos:开始拷贝的索引,dest:目标数组,
// destPos:拷贝到目标数组的哪个索引
System.arraycopy(src,0,dest,0,3);
//currentTimeMillis :返回当前时间距离1970-1-1的毫秒数
System.out.println(System.currentTimeMillis());
}
}
大数的处理(BigInteger、BigDecimal)
java
package com.edu.math;
import java.math.BigDecimal;
import java.math.BigInteger;
public class BigNumbers {
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("212122222222222222222222222222211");
System.out.println(bigInteger);
BigInteger bigInteger1 = new BigInteger("121");
//大数的加减乘除要使用对应的方法 +:add -:subtract *:multiply /:divide
BigInteger bigInteger2 = bigInteger.add(bigInteger1);
System.out.println(bigInteger2);
//同样加减乘除也要对应的方法
BigDecimal bigDecimal = new BigDecimal("1.111111111111111111111111111111111111111111");
System.out.println(bigDecimal);
BigDecimal bigDecimal1 = new BigDecimal("1");
System.out.println(bigDecimal.divide(bigDecimal1));//结果是无限循环小数,可能会有异常ArithmeticException
//在调用divide方法时指定精度即可,会保留分子的精度
System.out.println(bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING));
}
}