天天看點

常用類及方法總結 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