常用类及方法总结 JAVA

Scanner类

   * hasNextXxx() 判断是否还有下一个输入项,其中Xxx可以是Int,Double等。如果需要判断是否包含下一个字符串,则可以省略Xxx

   * nextXxx() 获取下一个输入项。Xxx的含义和上个方法中的Xxx相同,默认情况下,Scanner使用空格,回车等作为分隔符

      Scanner sc = new Scanner(System in);

      if(sc.hasNextInt()) {

          int i = sc.nextInt();

          System.out.println(i);

      }

    * public int nextInt():获取一个Int类型的值  

         int i = sc.nextInt();
    * public String nextLine():获取一个String类型的值  

         String line = sc.nextLine();


String类

  构造方法

     * public String():空构造
        * String s1 = new String()
    * public String(byte[] bytes):把字节数组转成字符串
        * byte[] arr1 = {97,98,99};
        * String s2 = new String(arr1);
        * System.out.println(s2);
        输出的结果:abc
    * public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
        * byte[] arr2 = {97,98,99,100,101,102};
        * String s3 = new String(arr2,2,3)
        * System.out.println(s3)
        输出的结果:cde
    * public String(char[] value):把字符数组转成字符串
        * char[] arr3 = {'a','b','c','d','e'};
        * String s4 = new String(arr3);
        * System.out.println(s4);
        输出的结果:abcde
    * public String(char[] value,int index,int count):把字符数组的一部分转成字符串
        * String s5 = new String(arr3,1,3);
        * System.out.println(s5);
        输出的结果:bcd
    * public String(String original):把字符串常量值转成一字符串
        * String s6 = new String("wodetian");
        * System.out.println(s6);
        输出的结果:wodetian

  判断功能

     * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
     * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
     * boolean contains(String str):判断大字符串中是否包含小字符串
     * boolean startsWith(String str):判断字符串是否以某个字符串开头
     * boolean endsWith(String str):判断字符串是否以某个指定的字符串开头
     * boolean isEmpty():判断字符串是否为空


  获取功能

     * int length():获取指定索引位置的字符
     * char charAt(int index):获取指定索引位置的字符
     * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引
     * int indexOf(String str):返回指定字符串在此字符串中第一次出现出的索引
     * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
     * int indexOf(String str,int fromindex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引
     * lastIndexOf
     * String substring(int start):从指定位置开始截取字符串,默认到末尾
     * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串


  转换功能

     * byte[] getBytes():把字符串转换为字节数组
     * char[] toCharArray():把字符串转换为字符数组
     * static String valueOf(char[] chs):把字符数组转成字符串
     * static String valueOf(int i):把int类型的数据转成字符串
        * 注意:String类的valueOf方法可以把任意类型的数据转成字符串
     * String toLowerCase():把字符串转成小写
     * String toUpperCase():把字符串转成大写
     * String concat(String str):把字符串拼接


  替换功能

     * String replace(char old,char new)
     * String replace(String old,String new)


  去处字符串两空格
     * String trim()


StringBuffer类

  构造方法

     * public StringBuffer():无参构造方法
     * public StringBuffer(int capacity):指定容量的字符串缓冲区对象
     * public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
     * public int length():返回长度(字符数)。 实际值

  添加功能

     * public StringBuffer append(String str):
        * 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
     * public StringBuffer insert(int offset,String str):
        * 在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身

  删除功能

      * public StringBuffer deleteCharAt(int index):
        * 删除指定位置的字符,并返回本身
      * public StringBuffer delete(int start,int end):
        * 删除从指定位置开始指定位置结束的内容,并返回本身

  替换功能

       * public StringBuffer replace(int start,int end,String str):
        * 从start开始到end用str替换

  反转功能

       * public StringBuffer reverse():
        * 字符串反转

  截取功能

       * public String substring(int start):
        * 从指定位置截取到末尾
       * public String substring(int start,int end):
        * 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置

Arrays类

       * public static String toString(int[] a)
       * public static void sort(int[] a)
       * public static int binarySearch(int[] a,int key)

Integer类

       * public Integer(int value)
       * public Integer(String s)

Math类

       * public static int abs(int a)
        Math.abs()  取绝对值
       * public static double ceil(double a)
        Math.ceil() 向上取整,结果是一个double值
       * public static double floor(double a)
        Math.floor() 向下取整,结果是一个double值
       * public static int max(int a,int b) min自学
        Math.max(a,b) 获取两个值当中的最大值
       * public static double pow(double a,double b)
        Math.pow(a,b) 求a得b次方
       * public static double random()
        Math.random() 生成0.0到1.0之间的所有小数,包括0.0,不包括1.0
       * public static int round(float a) 参数为double的自学
        Math.round() 四舍五入
       * public static double sqrt(double a)
        Math.sqrt() 开平方

Random类

   构造方法
      * public Random()
      * public Random(long seed)
  成员方法
      * public int nextInt()
      * public int nextInt(int n)(重点掌握)
          Random r = new Random();
          int i = r.nextInt(100);
          System.out.println(i);     返回0到99的int值

System类

  成员方法
       * public static void gc()
           System.gc() 运行垃圾回收器,相当于呼叫保洁阿姨
       * public static void exit(int status)
           System.exit(0) 退出jvm,0是正常终止,非0是异常终止
       * public static long currentTimeMillis()
           System.currentTimeMillis() 返回当前时间与协调世界时1970年1月1日午夜之间的时间差(以毫秒为单位测量)
       * pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
           int[] src = {11,22,33,44,55};
           int[] dest = new int[8];
           System.arraycopy(src,0,dest,0,src.length);
           for (int i = 0; i < dest.length; i++) {
               System.out.println(dest[i]);
        }
        输出的结果为:11 22 33 44 55 0 0 0

BigInteger类

  构造方法
       * public BigInteger(String val)
          String s = "123456789987654321";
          BigInteger bt = new BigInteger(s);
          System.out.println(bt);
        输出的结果为:123456789987654321

 成员方法
       * public BigInteger add(BigInteger val)
           a.add(b) a+b
       * public BigInteger subtract(BigInteger val)
           a.subtract(b) a-b
       * public BigInteger multiply(BigInteger val)
           a.mulitiply(b) a*b
       * public BigInteger divide(BigInteger val)
           a.divide(b) a/b
       * public BigInteger[] divideAndRemainder(BigInteger val)
           a.divideAndReminder(b) a/b的值和a/b的余数

BigDecimal类

  构造方法
        * public BigDecimal(String val)
  成员方法
        * public BigDecimal add(BigDecimal augend)
        * public BigDecimal subtract(BigDecimal subtrahend)
        * public BigDecimal multiply(BigDecimal multiplicand)
        * public BigDecimal divide(BigDecimal divisor)
            BigDecimal bd1 = new BigDecimal("2.0");
            BigDecimal bd2 = new BigDecimal("1.1");
            System.out.println(bd1.subtract(bd2));
            输出的结果为:0.9

Date类

 构造方法
       * public Date()
       * public Date(long date)
  成员方法
       * public long getTime()
           获取毫秒值,和System,currentTimeMillis()相似
       * public void setTime(long time)
           设置毫秒值
           Date d = new Date();
           d.setTime(1000);
           System.out.println(d);
           输出的结果为:Thu Jan 01 08:00:01 CST 1970

SimpleDateFormat类

  构造方法
       * public SimpleDateFormat()
       * public SimpleDateFormat(String pattern)
  成员方法
       * public final String format(Date date)
           format()是将日期对象转换为字符串的形式输出
           Date d = new Date();
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
           System.out.println(sdf.format(d));
           输出的结果:2017年08月07日 09:45:02
       * public Date parse(String source)
           parse()是将时间字符串换成日期对象
           String s = "2000年08月08日 08:08:08";
           SimpleDateFormat sdf = new SimpleDateFormat("yy年MM月dd日 HH:mm:ss");
           Date d = sdf.parse(s);
           System.out.println(d);
           输出的结果为:Tue Aug 08 08:08:08 CST 2000

Calendar类

   成员方法
       * public static Calendar getInstance()
       * public int get(int field)
           Calendar c = Calendar.getInstance();     //父类引用子类对象
           System.out.println(c.get(Calendar.YEAR));
           System.out.println(c.get(Calendar.MONTH));
           输出的结果:2017
                   7                            //这里的7不是7月,是8月

       * public void add(int field,int amount)
          Calendar c = Calendar.getInstance();  //父类引用子类对象
          c.add(Calendar.YEAR, 1);
          System.out.println(c.get(Calendar.YEAR));
          输出的结果:2018
      * public final void set(int year,int month,int date)
          Calendar c = Calendar.getInstance();  //父类引用子类对象
          c.set(Calendar.YEAR, 2000);
          System.out.println(c.get(Calendar.YEAR));
          输出的结果为:2000

猜你喜欢

转载自blog.csdn.net/philsonzhao/article/details/78464474