平常会用到的一些工具
Arrays数组工具类 举个例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.Arrays;public class ArrarysTest { public static void main (String[] args) { int [] a = {234 ,24 ,25 ,2323 ,25 ,45 }; Arrays.sort(a); for (int i : a) { System.out.print(i+" " ); } System.out.println(); int index = Arrays.binarySearch(a, 234 ); System.out.println(index); } }
Math工具类 举个例子
1 2 3 4 5 6 7 8 9 10 11 12 public class MathTest { public static void main (String[] args) { System.out.println(Math.pow(2 , 1 )); System.out.println(Math.E); System.out.println(Math.PI); System.out.println(Math.abs(-9 )); System.out.println(Math.ceil(3.4 )); System.out.println(Math.floor(3.5 )); System.out.println(Math.max(32 ,34 )); System.out.println(Math.cos(3 )); } }
结果
1 2 3 4 5 6 7 8 2.0 2.718281828459045 3.141592653589793 9 4.0 3.0 34 -0.9899924966004454
随机数 1 2 3 4 System.out.println(Math.random()); System.out.println(Math.random()*4 ); System.out.println((Math.random()*4 )+5 ); System.out.println((int )((Math.random()*4 )+5 ));
BigDecimal 先简单说明一下double精度的问题,由于浮点数在计算机里面特殊的存储方法,所以double总会有精度的损失,double的精度在15-16位,所以只要出错大于这个位数,double就不会显示出来,如果在这个范围内出错了,double也会显示出来,反正不管如何,用double存储数据都是有风险的,于是可以用Math里的BigDecimal解决这个问题
举个例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package com.manu3.bigdecimal;import java.math.BigDecimal;public class BigDecimalTest { public static void main (String[] args) { BigDecimal num1 = new BigDecimal("0.04" ); BigDecimal num2 = BigDecimal.valueOf(0.02 ); System.out.println(num1); System.out.println(num2); BigDecimal result1 = num1.add(num2); BigDecimal result2 = num1.subtract(num2); BigDecimal result3 = num1.multiply(num2); BigDecimal result4 = num1.divide(num2); System.out.println(result1); System.out.println(result2); System.out.println(result3); System.out.println(result4); } }
Date&Calender类 举个例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.manu3.date;import java.util.Calendar;import java.util.Date;public class DateCalenderTest { public static void main (String[] args) { Date now = new Date(); System.out.println(now); Calendar c = Calendar.getInstance(); System.out.println(c); System.out.println(c.get(Calendar.AM)); System.out.println(c.get(Calendar.DATE)); System.out.println(c.get(Calendar.HOUR)); System.out.println(c.get(Calendar.MINUTE)); } }
基本类型包装类 基本数据一般都在栈里面,而包装就是栈存地址,数据在堆里
1.Byte
2.Short
3.Integer
4.BigInteger(可以存一个大整数),与BigDecimal差不多
5.Long
6.Float
7.Double
8.Character
9.Boolean
举个例子吧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.manu4.packageclasss;public class PackagingClass { public static void main (String[] args) { Integer n1 = Integer.valueOf("100" ); System.out.println(n1); Byte n2 = Byte.valueOf("100" ); System.out.println(n2); Long n3 = Long.valueOf("100" ); System.out.println(n3); Float n4 = Float.valueOf("100.234" ); System.out.println(n4); Double n5 = Double.valueOf("100.2345325" ); System.out.println(n5); } }
拆箱和装箱 其实赋值没必要像上面那样麻烦
1 2 Integer n1 = 100 ; n1 += 100 ;