20180729_10_Java中关于Arrays类,包装类以及正则表达式的介绍

Arrays

      数组的工具类,针对数组进行操作的类。提供了排序,查找等功能。

      成员方法:

            public static String toString(int a)              打印数组元素。

            public static void sort(int[] arr)                 默认从小到大排序

            public static int binarySearch(int[] arr,int key)   二分查找

                  二分查找的前提:数组元素必须有序。

包装类

      Java中为了方便我们操作这些基本的数据类型,给我们提供了与之对应的包装类。

      基本类                    包装类

      byte                        Byte

      short                       Short

      int                           Integer

      long                        Long

      float                        Float

      double                    Double

      char                        Character

      boolean                  Bollean

 

包装类

integer类的介绍(其余类与之相同):

      Integer中的静态常量

            int maxValue = Integer.MAX_VALUE;   int类型能够表示的最大值

            int minValue = Integer.MIN_VALUE;          int类型能够表示的最小值

      Integer中常用的构造方法:

            Integer(int value)      构造一个新分配的 Integer 对象,它表示指定的 int 值。

            Integer(String s) 构造一个新分配的Integer对象,它表示String参数所指示的int值。

      Integerz中String和int类的相互转换

      int ---> String

            a:和” ”拼接。

            bpublic static String valueOf(int i)          推荐使用

                  eg: int a = 100;

                       String st = String.valueOf(a);

            c:int   --  Integer  --  String

            d:public static String toString(int i)

      String ---> int

            a: String -- Integer -- intValue();

            b:public static int parseInt(String s)         推荐使用

                  eg: int a = Integer.parseInt(“100”);   

正则表达式

      正则表达式:是一个独立的技术,很多语言都支持。

      正确规则的表达式,用来校验数据是否满足我们自己定义的规则。

            String  regx="[abc]";                  //允许出现a,b,c中的任意一个

            regx="[123ABCabc]";            //允许出现1,2,3中任意一个

            regx="[^123]";        //除了123其他都可以

            regx="[0-9]";                 //允许出现0 - 9中任意一个

            regx="[a-z]";                 //允许出现a - z中任意一个

            regx="[A-Z]";                 //允许出现A - Z中任意一个

            regx="[0-9a-zA-Z]";       

            regx=".";                 //匹配任意单个字符

            regx="\\.";               //想让.代表这个.本身需要对点进行转意

            regx="\\d";              //表示[0-9]

            regx="\\w";             //表示 [a-z_A-Z]

            regx="a?b?";                  //一次或一次也没有 ""空串就代表0次

            regx="a*";               //零次或多次  大于等于1次 都算多次

            regx="a+";              //至少一次,或多次

            regx="\\w+";

            regx="[0-9]{5}";       //正好出现几次

            regx="[a-z]{5,}";            //至少5次

            regx="[a-zA-Z0-9]{6,18}";     //表示6到18

      正则表达式的判断功能

            String类中的判断功能:public boolean matches(String regex)

      正则表达式的分割功能

            String类的功能:public String[] split(String regex)

            字符串数组,它是根据给定正则表达式的匹配拆分此字符串确定的。

      正则表达式的替换功能

            String类的功能:

                  public String replaceAll(String regex,String replacement)

Pattern类和Matcher

      Pattern  :模式器 用来封装正则表达式

      Matcher:匹配器 用来匹配正则表达式

            把一个正则表达式封装进模式器里:

                  Pattern p = Pattern.compile(“a*b”);

            通过模式器,获取匹配,并传入需要匹配的字符

                  Matcher m = p.matcher(“aaa”);

            调用匹配器中的方法进行匹配

                  boolean b = m.matcher();

Match类:

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

      成员变量

            public static final double E :         自然底数

            public static final double PI:         圆周率

      成员方法

            public static int abs(int a)       取绝对值

            public static double ceil(double a)  向上取整

            public static double floor(double a)      向下取整

            public static int max(int a,int b)      获取最大值

            public static int min(int a, int b)    获取最小值

            public static double pow(double a,double b) 获取a的b次幂

            public static double random() 获取随机数 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。

            public static int round(float a) 四舍五入

            public static double sqrt(double a)获取正平方根

Random类:

      System 类包含一些有用的类字段和方法。它不能被实例化。

      成员方法

            public static void gc()//调用垃圾回收器

            public static void exit(int status)//退出java虚拟机 0 为正常退出 非0为 异常退出

            public static long currentTimeMillis()//获取当前时间的毫秒值

猜你喜欢

转载自blog.csdn.net/Lisiluan/article/details/81293479
今日推荐