JAVA-基础-常用API(Math类,Arrays类,大数据运算)

 

       Math类

Math 类是包含用于执行基本数学运算的方法的数学工具类,如初等指数、对数、平方根和三角函数。

其所有方法均为静态,且一般不会创建对象


 

l  abs方法,结果都为正数

double d1 = Math.abs(-5); // d1的值为5

double d2 = Math.abs(5); // d2的值为5

l  ceil方法,结果为比参数值大的最小整数的double值

double d1 = Math.ceil(3.3); //d1的值为 4.0

double d2 = Math.ceil(-3.3); //d2的值为 -3.0

double d3 = Math.ceil(5.1); // d3的值为 6.0

l  floor方法,结果为比参数值小的最大整数的double值

double d1 = Math.floor(3.3); //d1的值为3.0

double d2 = Math.floor(-3.3); //d2的值为-4.0

double d3 = Math.floor(5.1); //d3的值为 5.0

l  max方法,返回两个参数值中较大的值

double d1 = Math.max(3.3, 5.5); //d1的值为5.5

double d2 = Math.max(-3.3, -5.5); //d2的值为-3.3

l  min方法,返回两个参数值中较小的值

double d1 = Math.min(3.3, 5.5); //d1的值为3.3

double d2 = Math.max(-3.3, -5.5); //d2的值为-5.5

l  pow方法,返回第一个参数的第二个参数次幂的值

double d1 = Math.pow(2.0, 3.0); //d1的值为 8.0

double d2 = Math.pow(3.0, 3.0); //d2的值为27.0

l  round方法,返回参数值四舍五入的结果

double d1 = Math.round(5.5); //d1的值为6.0

double d2 = Math.round(5.4); //d2的值为5.0

l  random方法,产生一个大于等于0.0且小于1.0的double小数

double d1 = Math.random();

 

       Arrays类

此类包含用来操作数组(比如排序和搜索)的各种方法。需要注意,如果指定数组引用为 null,则访问此类中的方法都会抛出空指针异常

NullPointerException

static int       binarySearch(int[ ] a,   intKey)    用二分搜索法来 搜索指定的int类型数组,以或得指定值

static void     sort (int[ ] a)              对指定int 数组 按数字的升序进行排序

static String   toString (int[ ] a)         返回指定数组内容的字符串表示形式

 

l  sort方法,用来对指定数组中的元素进行排序(元素值从小到大进行排序)

//源arr数组元素{1,5,9,3,7}, 进行排序后arr数组元素为{1,3,5,7,9}
int[] arr = {1,5,9,3,7};
Arrays.sort( arr );

l  toString方法,用来返回指定数组元素内容的字符串形式

int[] arr = {1,5,9,3,7};
String str = Arrays.toString(arr); // str的值为[1, 3, 5, 7, 9]

l  binarySearch方法,在指定数组中,查找给定元素值出现的位置。若没有查询到,返回位置为-1。要求该数组必须是个有序的数组。

int[] arr = {1,3,4,5,6};
int index = Arrays.binarySearch(arr, 4); //index的值为2
int index2= Arrasy.binarySearch(arr, 2); //index2的值为-1

    Arrays类的方法练习

 

l  练习一:定义一个方法,接收一个数组,数组中存储10个学生考试分数,该方法要求返回考试分数最低的后三名考试分数。

public static int[] method(double[] arr){
    Arrays.sort(arr); //进行数组元素排序(元素值从小到大进行排序)
    int[] result = new int[3]; //存储后三名考试分数
    System.arraycopy(arr, 0, result, 0, 3);//把arr数组前3个元素复制到result数组中
return result;
}

          大数据运算

 

  BigInteger

  java中long型为最大整数类型,对于超过long型的数据如何去表示呢.在Java的世界中,超过long型的整数已经不能被称为整数了,它们被封装成BigInteger对象.在BigInteger类中,实现四则运算都是方法来实现,并不是采用运算符.

  BigInteger类的构造方法:

 

 

 构造方法中,采用字符串的形式给出整数:

public static void main(String[] args) {
        //大数据封装为BigInteger对象
          BigInteger big1 = new BigInteger("12345678909876543210");
          BigInteger big2 = new BigInteger("98765432101234567890");
          //add实现加法运算
          BigInteger bigAdd = big1.add(big2);
          //subtract实现减法运算
          BigInteger bigSub = big1.subtract(big2);
          //multiply实现乘法运算
          BigInteger bigMul = big1.multiply(big2);
          //divide实现除法运算
          BigInteger bigDiv = big2.divide(big1);
}

 

    BigDecimal

 在程序中执行下列代码,会出现什么问题?

    System.out.println(0.09 + 0.01);

    System.out.println(1.0 - 0.32);

    System.out.println(1.015 * 100);

    System.out.println(1.301 / 100);

 double和float类型在运算中很容易丢失精度,造成数据的不准确性,Java提供我们BigDecimal类可以实现浮点数据的高精度运算

   构造方法如下:

 

 

建议浮点数据以字符串形式给出,因为参数结果是可以预知的

  实现加法减法乘法代码如下:

public static void main(String[] args) {
          //大数据封装为BigDecimal对象
          BigDecimal big1 = new BigDecimal("0.09");
          BigDecimal big2 = new BigDecimal("0.01");
          //add实现加法运算
          BigDecimal bigAdd = big1.add(big2);
          
          BigDecimal big3 = new BigDecimal("1.0");
          BigDecimal big4 = new BigDecimal("0.32");
          //subtract实现减法运算
          BigDecimal bigSub = big3.subtract(big4);
          
          BigDecimal big5 = new BigDecimal("1.105");
          BigDecimal big6 = new BigDecimal("100");
          //multiply实现乘法运算
          BigDecimal bigMul = big5.multiply(big6);

 对于浮点数据的除法运算,和整数不同,可能出现无限不循环小数,因此需要对所需要的位数进行保留和选择舍入模式

 

posted @ 2018-06-26 10:08  代码运输工  阅读(954)  评论(0编辑  收藏  举报