疯狂JAVA讲义---第九章(中):常用类

首先讲下Math,下面列出它的各种使用,eg(注意有些是1.6才有的新方法)

  1. public class TestMath
  2. {
  3.     public static void main(String[] args) 
  4.     {
  5.         /*---------下面是三角运算---------*/
  6.         //将弧度转换角度
  7.         System.out.println("Math.toDegrees(1.57):" + Math.toDegrees(1.57)); 
  8.         //将角度转换为弧度
  9.         System.out.println("Math.toRadians(90):" + Math.toRadians(90));
  10.         //计算反余弦,返回的角度范围在 0.0 到 pi 之间。
  11.         System.out.println("Math.acos(0.3):" + Math.acos(1.2)); 
  12.         //计算反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。 
  13.         System.out.println("Math.asin(0.8):" + Math.asin(0.8)); 
  14.         //计算反正切;返回的角度范围在 -pi/2 到 pi/2 之间。 
  15.         System.out.println("Math.atan(2.3):" + Math.atan(2.3)); 
  16.         //计算三角余弦。
  17.         System.out.println("Math.cos(1.57):" + Math.cos(1.57)); 
  18.         //计算值的双曲余弦。 
  19.         System.out.println("Math.cosh(1.2 ):" + Math.cosh(1.2 )); 
  20.         //计算正弦
  21.         System.out.println("Math.sin(1.57 ):" + Math.sin(1.57 )); 
  22.         //计算双曲正弦
  23.         System.out.println("Math.sinh(1.2 ):" + Math.sinh(1.2 ));
  24.         //计算三角正切
  25.         System.out.println("Math.tan(0.8 ):" + Math.tan(0.8 )); 
  26.         //计算双曲余弦
  27.         System.out.println("Math.tanh(2.1 ):" + Math.tanh(2.1 )); 
  28.         //将矩形坐标 (x, y) 转换成极坐标 (r, thet));,返回所得角 theta。 
  29.         System.out.println("Math.atan2(0.1, 0.2):" + Math.atan2(0.10.2));
  30.         /*---------下面是取整运算---------*/
  31.         //取整,返回小于目标数的最大整数。
  32.         System.out.println("Math.floor(-1.2 ):" + Math.floor(-1.2 )); 
  33.         //取整,返回大于目标数的最小整数。
  34.         System.out.println("Math.ceil(1.2):" + Math.ceil(1.2)); 
  35.         //四舍五入取整
  36.         System.out.println("Math.round(2.3 ):" + Math.round(2.3 )); 
  37.         /*---------下面是乘方、开方、指数运算---------*/
  38.         //计算平方根。
  39.         System.out.println("Math.sqrt(2.3 ):" + Math.sqrt(2.3 )); 
  40.         //计算立方根。 
  41.         System.out.println("Math.cbrt(9):" + Math.cbrt(9)); 
  42.         //返回欧拉数 e 的n次幂。
  43.         System.out.println("Math.exp(2):" + Math.exp(2)); 
  44.         //返回 sqrt(x2:" +y2),没有中间溢出或下溢。
  45.         System.out.println("Math.hypot(4 , 4):" + Math.hypot(4 , 4));
  46.         // 按照 IEEE 754 标准的规定,对两个参数进行余数运算。
  47.         System.out.println("Math.IEEEremainder(5 , 2):" + Math.IEEEremainder(5 , 2));
  48.         //计算乘方
  49.         System.out.println("Math.pow(3, 2):" + Math.pow(32));
  50.         //计算自然对数
  51.         System.out.println("Math.log(12):" + Math.log(12)); 
  52.         //计算底数为 10 的对数。
  53.         System.out.println("Math.log10(9):" + Math.log10(9)); 
  54.         // 回参数与 1 之和的自然对数。 
  55.         System.out.println("Math.log1p(9):" + Math.log1p(9)); 
  56.         /*---------下面是符号相关的运算---------*/
  57.         //计算绝对值。
  58.         System.out.println("Math.abs(-4.5):" + Math.abs(-4.5));
  59.         //符号赋值,返回带有第二个浮点数符号的第一个浮点参数。
  60.         System.out.println("Math.copySign(1.2, -1.0):" + Math.copySign(1.2, -1.0));
  61.         //符号函数;如果参数为 0,则返回 0;如果参数大于 0,则返回 1.0;如果参数小于 0,则返回 -1.0。
  62.         System.out.println("Math.signum(2.3):" + Math.signum(2.3)); 
  63.         /*---------下面是大小相关的运算运算---------*/
  64.         //找出最大值
  65.         System.out.println("Math.max(2.3 , 4.5):" + Math.max(2.3 , 4.5));
  66.         //计算最小值 
  67.         System.out.println("Math.min(1.2 , 3.4):" + Math.min(1.2 , 3.4));
  68.         //返回第一个参数和第二个参数之间与第一个参数相邻的浮点数。
  69.         System.out.println("Math.nextAfter(1.2, 1.0):" + Math.nextAfter(1.21.0));
  70.         //返回比目标数略大的浮点数
  71.         System.out.println("Math.nextUp(1.2 ):" + Math.nextUp(1.2 ));
  72.         //返回一个伪随机数,该值大于等于 0.0 且小于 1.0。
  73.         System.out.println("Math.random():" + Math.random());
  74.     }
  75. }

方法过多,大家可以参看以上代码,或查javadoc

Random类也是很常用的类,这里要说下计算机产生的都是伪随机数(什么叫伪随机数.计算机不会产生绝对随机的随机数,计算机只能产生“伪随机数”。其实绝对随机的随机数只是一种理想的随机数,即使计算机怎样发展,它也不会产生一串绝对随机的随机数。计算机只能生成相对的随机数,即伪随机数。伪随机数并不是假随机数,这里的“伪”是有规律的意思,就是计算机产生的伪随机数既是随机的又是有规律的。)看下面的例子大家就知道有多伪了~~呵呵,eg

  1. public class TestSeed
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Random r1 = new Random(50);
  6.         System.out.println("第一个种子为50的Random对象");
  7.         System.out.println("r1.nextBoolean():/t" + r1.nextBoolean());
  8.         System.out.println("r1.nextInt():/t/t" + r1.nextInt());
  9.         System.out.println("r1.nextDouble():/t" + r1.nextDouble());
  10.         System.out.println("r1.nextGaussian():/t" + r1.nextGaussian());
  11.         System.out.println("---------------------------");
  12.         
  13.         Random r2 = new Random(50);
  14.         System.out.println("第二个种子为50的Random对象");
  15.         System.out.println("r2.nextBoolean():/t" + r2.nextBoolean());
  16.         System.out.println("r2.nextInt():/t/t" + r2.nextInt());
  17.         System.out.println("r2.nextDouble():/t" + r2.nextDouble());
  18.         System.out.println("r2.nextGaussian():/t" + r2.nextGaussian());
  19.         System.out.println("---------------------------");
  20.         
  21.         Random r3 = new Random(100);
  22.         System.out.println("种子为100的Random对象");
  23.         System.out.println("r3.nextBoolean():/t" + r3.nextBoolean());
  24.         System.out.println("r3.nextInt():/t/t" + r3.nextInt());
  25.         System.out.println("r3.nextDouble():/t" + r3.nextDouble());
  26.         System.out.println("r3.nextGaussian():/t" + r3.nextGaussian());        
  27.     }
  28. }

当随机种子一样的时候结果都一样。

一般都用系统时间当种子Random rand=new Random(System.currentTimeMillis());或直接用Random的默认构造,eg

  1. public class TestRandom
  2. {
  3.     public static void main(String[] args) 
  4.     {
  5.         Random rand = new Random();
  6.         System.out.println("rand.nextBoolean():" + rand.nextBoolean());
  7.         byte[] buffer = new byte[16];
  8.         rand.nextBytes(buffer);
  9.         System.out.println(Arrays.toString(buffer));
  10.         //生成0.0~1.0之间的伪随机double数
  11.         System.out.println("rand.nextDouble():" + rand.nextDouble());
  12.         //生成0.0~1.0之间的伪随机float数
  13.         System.out.println("rand.nextFloat():" + rand.nextFloat());
  14.         //生成平均值是 0.0,标准差是 1.0的伪高斯数
  15.         System.out.println("rand.nextGaussian():" + rand.nextGaussian());
  16.         //生成一个处于long整数取值范围的伪随机整数
  17.         System.out.println("rand.nextInt():" + rand.nextInt());
  18.         //生成0~26之间的伪随机整数
  19.         System.out.println("rand.nextInt(26):" + rand.nextInt(26));
  20.         //生成一个处于long整数取值范围的伪随机整数
  21.         System.out.println("rand.nextLong():" +  rand.nextLong());
  22.     }
  23. }

刚看了一个例子发现,java基本类型浮点数是那么的容易发生精度丢失,eg

 

  1. public class TestDouble
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));
  6.         System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));
  7.         System.out.println("4.015 * 100 = " + (4.015 * 100));
  8.         System.out.println("123.3 / 100 = " + (123.3 / 100));
  9.     }
  10. }

一般使用BigDecimal的String构造法或BigDecimal.valueOf(...)不会发生精度丢失。eg

  1. public class TestBigDecimal
  2. {
  3.     public static void main(String[] args) 
  4.     {
  5.         BigDecimal f1 = new BigDecimal("0.05");
  6.         BigDecimal f2 = BigDecimal.valueOf(0.01);
  7.         BigDecimal f3 = new BigDecimal(0.05);
  8.         System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:");
  9.         System.out.println("0.05 + 0.01 = " + f1.add(f2));
  10.         System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
  11.         System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
  12.         System.out.println("0.05 / 0.01 = " + f1.divide(f2));
  13.         System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:");
  14.         System.out.println("0.05 + 0.01 = " + f3.add(f2));
  15.         System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
  16.         System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
  17.         System.out.println("0.05 / 0.01 = " + f3.divide(f2));
  18.     }
  19. }

下面提供个工具类代码方便浮点数的运算

  1. public class Arith
  2. {
  3.     //默认除法运算精度
  4.     private static final int DEF_DIV_SCALE = 10;
  5.     //构造器私有,让这个类不能实例化
  6.     private Arith() {} 
  7.     /**
  8.      * 提供精确的加法运算。
  9.      * @param v1 被加数
  10.      * @param v2 加数
  11.      * @return 两个参数的和
  12.      */
  13.     public static double add(double v1,double v2)
  14.     {
  15.         BigDecimal b1 = BigDecimal.valueOf(v1);
  16.         BigDecimal b2 = BigDecimal.valueOf(v2);
  17.         return b1.add(b2).doubleValue();
  18.     }
  19.     /**
  20.      * 提供精确的减法运算。
  21.      * @param v1 被减数
  22.      * @param v2 减数
  23.      * @return 两个参数的差
  24.      */
  25.     public static double sub(double v1,double v2)
  26.     {
  27.         BigDecimal b1 = BigDecimal.valueOf(v1);
  28.         BigDecimal b2 = BigDecimal.valueOf(v2);
  29.         return b1.subtract(b2).doubleValue();
  30.     } 
  31.     /**
  32.      * 提供精确的乘法运算。
  33.      * @param v1 被乘数
  34.      * @param v2 乘数
  35.      * @return 两个参数的积
  36.      */
  37.     public static double mul(double v1,double v2)
  38.     {
  39.         BigDecimal b1 = BigDecimal.valueOf(v1);
  40.         BigDecimal b2 = BigDecimal.valueOf(v2);
  41.         return b1.multiply(b2).doubleValue();
  42.     } 
  43.     /**
  44.      * 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
  45.      * 小数点以后10位的数字四舍五入。
  46.      * @param v1 被除数
  47.      * @param v2 除数
  48.      * @return 两个参数的商
  49.      */
  50.     public static double div(double v1,double v2)
  51.     {
  52.         BigDecimal b1 = BigDecimal.valueOf(v1);
  53.         BigDecimal b2 = BigDecimal.valueOf(v2);
  54.         return b1.divide(b2 , DEF_DIV_SCALE , BigDecimal.ROUND_HALF_UP).doubleValue();
  55.     } 
  56.     public static void main(String[] args)
  57.     {
  58.         System.out.println("0.05 + 0.01 = " + Arith.add(0.05 , 0.01));
  59.         System.out.println("1.0 - 0.42 = " + Arith.sub(1.0 , 0.42));
  60.         System.out.println("4.015 * 100 = " + Arith.mul(4.015 , 100));
  61.         System.out.println("123.3 / 100 = " + Arith.div(123.3 , 100));
  62.     }
  63. }

最后讲下日期有关的类,date现在已经被Calendar类替代,下面看个Calendar使用例子,eg

  1. public class TestCalendar {
  2.     public static void main(String[] args)
  3.     {
  4.         Calendar c = Calendar.getInstance();
  5.         //取出年
  6.         System.out.println(c.get(Calendar.YEAR));
  7.         //取出月份
  8.         System.out.println(c.get(Calendar.MONTH));
  9.         //取出日
  10.         System.out.println(c.get(Calendar.DATE));
  11.         //分别设置年、月、日、小时、分钟、秒
  12.         c.set(2003 , 10 , 23 , 123223);//2003-11-23 12:32:23
  13.         System.out.println(c.getTime());
  14.         //将Calendar的年前推1年
  15.         c.add(Calendar.YEAR , -1); //2002-11-23 12:32:23
  16.         System.out.println(c.getTime());
  17.         //将Calendar的月前推8个月
  18.         c.roll(Calendar.MONTH , -8); //2002-03-23 12:32:23
  19.         System.out.println(c.getTime());
  20.     }
  21. }

Calendar类的set方法具有延迟写的功能,只有当通过get方法得到时间时才会计算时间

最后附带介绍下TimeZone类,它是根据你操作系统的设定来获得时区的,eg

  1. public class TestTimeZone
  2. {
  3.     public static void main(String[] args) 
  4.     {
  5.         //取得Java所支持的所有时区ID
  6.         String[] ids = TimeZone.getAvailableIDs();
  7.         System.out.println(Arrays.toString(ids));
  8.         TimeZone my = TimeZone.getDefault();
  9.         //获取系统默认时区的ID:Asia/Shanghai
  10.         System.out.println(my.getID());
  11.         //获取系统默认时区的名称:中国标准时间
  12.         System.out.println(my.getDisplayName());
  13.         //获取指定ID的时区的名称:纽芬兰标准时间
  14.         System.out.println(TimeZone.getTimeZone("CNT").getDisplayName());
  15.     }
  16. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值