Java高级之数据格式化
Labmda表达式: Runnable runnable =new Runnable(){ public void run(){ //123 } } 变: Runnable runnable =()->{ //123 } “123”为多句代码,可以写上括号,如果仅一句,可以不写。 获得小数位数: 经测试,使用小数(1.01)通过使用“.”进行split操作后,得到的数组为空,原因在于String的split方法 public String[] split(String regex, int limit) { /* fastpath if the regex is a (1)one-char String and this character is not one of the RegEx's meta characters ".$|()[{^?*+\\", or (2)two-char String and the first char is the backslash and the second is not the ascii digit or ascii letter. */ char ch = 0; if (((regex.value.length == 1 && ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) || (regex.length() == 2 && regex.charAt(0) == '\\' && (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 && ((ch-'a')|('z'-ch)) < 0 && ((ch-'A')|('Z'-ch)) < 0)) && (ch < Character.MIN_HIGH_SURROGATE || ch > Character.MAX_LOW_SURROGATE)) { ... } return Pattern.compile(regex).split(this, limit); } if语句判断无法通过,因为不允许使用"."做split的regex字符;第2个条件明显长度只有1;第3个条件ch<55276或者ch>57344才满足。 运行到return语句,Pattern执行结果,获得空数据。 那么只能通过indexOf方法来获得小数位数了: 1位小数且小数第1位是0的(如1.0),显示整数;其他情况显示原小数(String默认会把1.10看成1.1)。 public static String get1BitData(double number) { String first = String.valueOf(number); String second = first.substring(first.indexOf(".")); if (second.length() > 1 && !second.endsWith("0")) { return String.valueOf(number); } return String.valueOf((int) number); } 取相应格式的小数: //取一位整数 System.out.println(newDecimalFormat("0").format(pi));//3 //取一位整数和两位小数 System.out.println(newDecimalFormat("0.00").format(pi));//3.14 //取两位整数和三位小数,整数不足部分以0填补。 System.out.println(newDecimalFormat("00.000").format(pi));//03.142 //取所有整数部分 System.out.println(newDecimalFormat("#").format(pi));//3 //以百分比方式计数,并取两位小数 System.out.println(newDecimalFormat("#.##%").format(pi));//314.16% longc=299792458; //显示为科学计数法,并取五位小数 System.out.println(newDecimalFormat("#.#####E0").format(c));//2.99792E8 //显示为两位整数的科学计数法,并取四位小数 System.out.println(newDecimalFormat("00.####E0").format(c));//29.9792E7 //每三位以逗号进行分隔。 System.out.println(newDecimalFormat(",###").format(c));//299,792,458 //将格式嵌入文本 System.out.println(newDecimalFormat("光速大小为每秒,###米。").format(c)); Ioc反转和Di注入的可用容器有Spring、JBoss、Ejb等。 它们都是一种编程思想,目的是解耦。 A调用B,正常情况下是,创建B,再调用B,则A依赖B,并且使用完毕还要销毁B。 Ioc,A告诉容器要调用B,容器创建B,并通知A,然后A通过构造函数、属性和接口调用方式,获得B,再去调用B。 Di,A告诉容器要调用B,容器创建B,并通知A,然后A通过反射的方式,获得B,再去调用B。 AOP:Aspect Oriented Programming。面向切面编译,切面指某类的某方法的代码片断,切点(JoinPoint)指某类的某方法。AOP指通过动态注入的方式,将几处共用的代码片断,在使用时注入,使得代码维护更加便利。