Day12 JavaSE基础复习 (14)正则表达式&常用工具类 (15)Collection集合

    xiaoxiao2022-06-24  192

     (14)正则表达式&常用工具类

    一. 正则表达式(regex)

     字符类预定义字符类数量词Pattern和Matcher     java.util.regex.Pattern   java.util.regex.Matcher Pattern p = Pattern.compile(regex);    //获取到正则表达式 Matcher m = p.matcher(字符串);         //获取匹配器 boolean b = m.matches();           //看是否匹配,匹配就返回true syso(b); syso(字符串.matches(regex));     //与上边结果一样 while(m.find())         syso(m.group());  // 通过循环,输出找到匹配的字符串  

    二.常用工具类

     Math         java.lang.Math Math.PI    Math.abs();        //取绝对值 Math.ceil();         //向上取整,但结果是一个double值       例如2.5   向上取整是3.0 Math.floor();        //向下取整,但结果是一个double值       例如2.5   向下取整是2.0 Math.pow(2,3);    //前边底数,后边指数     2^3 Math.random();    //生成一个0到1.0的随机小数,区间前闭后开 Math.round();        //四舍五入Random     java.util.Random Random r = new Random(); r.nextInt(100);           //生成一个0-99的随机数System System.exit(0);          //退出JVM,0是终止,输入其他的数也可以,但属于异常终止,最好用0 System.currentTimeMillis();       //获取当前时间的毫秒值,可以用long接收 System.arraycopy(src,0,dest,0,length);      //数组拷贝       原数组,起始位置,目标数组,起始位置,拷贝长度Date           java.util.Date Date d1 = new Date(); syso(d1);                   //当前时间 Date d2 = new Date(0); syso(d2);                  //1970.1.1   Unix和c语言生日 d1.setTime(1000);  //设置毫秒值,改变时间对象SimpleDateFormat                        父类是DateFormat,抽象类不允许实例化         DateFormat df = new SimpleDateFormat();            DateFormat df = DateFormat.getDateInstance();   //底层封装了上一行 Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat();  //创建日期格式化对象 syso(sdf.format(d));               //显示格式化后的日期 String str = "2000年01月01日01:01:01"; SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月dd日 HH:mm:ss”);  Date d = sdf.parse(str); syso(d);

     (15)Collection集合 

    一.

    集合遍历之集合转数组遍历 Collection c = new ArrayList(); Object[] o = c.toArray();         //集合转换成数组  然后for循环遍历集合遍历值迭代器遍历 Collection c = new ArrayList(); Iterator it = c.iterator();          //获取迭代器 while(it.hasNext())            syso(it.next());通过索引遍历List集合    set集合不可以,因为set集合无索引 List list = new ArrayList(); list.get(index);         //用for循环遍历  

    最新回复(0)