Java基礎之常用類

來源:互聯網
上載者:User

標籤:大寫   image   index   valueof   相互   catch   產生   orm   void   

一、 String

1.多個字元組成的一串資料,它可以和字元數組進行相互轉換

2.構造方法:
  public String ( )  空構造
  public String (byte[ ] bytes)  把位元組數組轉成字串
  public String (byte[ ] bytes,int offset,int length) 把位元組數組的一部分轉成字串
  public String (char[ ] value)把字元數組轉成字串
  public String (char[ ] value,int offset,int count) 把字元數組的一部分轉成字串
  public String (String original) 把字串常量值轉成字串

3.方法:
    判斷功能
    boolean equals(Object obj) 比較字串的內容是否相同,區分大小寫
      例 s1.equals(s2) s1和s2比較
    boolean equalsIgnoreCase(String str) 比較字串內容是否相同,忽略大小寫
      例 s1.equals(s2) s1和s2比較,注意區分大小寫
    boolean contains(String str) 判斷大字串中是否包含小字串
      例 s1.contains("hello") 判斷s1中有沒有hello這個字串
    boolean startsWith(String str) 判斷字串是否以某個指定的字串開頭
      例 s1.startWith("h")  判斷s1中是否以h開頭
    boolean endsWith(String str) 判斷字串是否以某個指定的字串結尾
      例 s1.endWith("s")  判斷s1中是否以s結尾
    boolean isEmpty() 判斷字串是否為空白
      例 s1.isEmpty() 判斷s1是否為空白字串

  擷取功能
    int length() 擷取字串的長度
           例 s.length()
    char charAt(int index) 擷取指定位置索引的字元
      例 s.charAt(7) 擷取第七個位置的字元(從0開始)
    int indexOf(int ch) 返回指定字元在此字串中第一次出現的索引
      例 s.indexOf("c") 擷取 c 第一次出現的位置
    int indexOf(String str) 返回指定字串在此字串中第一次出現的索引
      例 s.indexOf("cake") 擷取 cake 第一次出現的位置
    int indexOf(int ch,int fromIndex) 返回指定字元在此字串中從指定位置後第一次出現處的索引
      例 s.indexOf("c",4) 從第4個索引後擷取 c 的索引
    int indexOf(String str,int fromIndex) 返回指定字串在此字串中從指定位置後第一次出現處的索引
      例 s.indexOf("cake",4) 從第4個索引後擷取 cake 的索引
    String substring(int start) 從指定位置截取字串,預設到結尾
      例 s.substring(5) 從第5個位置截取字串
    String substring(int start,int end) 從指定位置開始到結束截取字串
      例 s.substring(5,8) 從第5個位置截取字串到第8個結束,不包括第8個字元。(包左不包右)

  轉換功能
    byte[ ] getBytes( ) 把字串轉換為位元組數組。
      例 byte[ ] bys = s.getBytes( );
    char[ ] toCharArray( ) 把字串轉換為字元數組
      例 char[ ] cha = s.toCharArray();
    static String valueOf(char[ ] chs) 把字元數組轉成字串。
      例 String ss = String.valueOf(cha);
    static String valueOf(int i) 把int類型的資料轉成字串
      例 int y=100;
         String s2= String.valueOf(y);
      String toLowerCase( ) 把字串轉成小寫
      例 String s1=s.toLowerCase
    String toUpperCase() 把字串轉成大寫
      例 String s1=s.toUpperCase
    String concat(String str) 把字串拼接
      例 s1.concat(s2) 把s1和s2拼接


  其他功能
      String replace(char old, char new) 替換字串中的某一個字元
       例 s1.replace("p","u")  把s1中的所有p字元替換成u字元
      String replace(String old, String new) 替換字串中的字串
          例 s1.replace("hello","feiji") 把s1中的hello替換成feiji
      String trim() 去除字串兩端空格
         例 s1.trim(); 
      int compareTo(String str) 按字典順序比較兩 個字串
         例 s1.compareTo(s2);
         把s1和s2比較,一樣返回0。
      int compateToIgnoreCase(String str) 按字典順序比較兩個字串,區分大小寫
      例 同上

二、 StringBuffer(是同步的,資料安全,效率低)/StringBuilder(單線程使用,不同步,效率高)

1.安全執行緒的可變字串。


2.構造方法
  public StringBuffer() 無參構造方法。
  public StringBuffer(int capacity) 指定容量的字串緩衝區對象。
  public StringBuffer(String str) 指定字串內容的字串緩衝區對象。


3.方法
       A:添加功能
    public StringBuffer append(String str)  添加任意類型到字串杯子中
    public StringBuffer insert(int offset,String str) 在指定位置插入任意類型的資料到杯子中

      B: 刪除功能 
    public StringBuffer deleteCharAt(int index) 刪除指定位置的一個字元
    public StringBuffer delete(int start,int end) 刪除指定區間的所有字元(包左不包右)

     C: 替換功能
    public StringBuffer replace(int start,int end,String str) 替換指定區間的字串(包左不包右)

     D: 反轉功能
     public StringBuffer reverse()  反轉字串,例 abc--cba

     E: 截取功能(注意傳回值是String類型的)
    public String substring(int start) 截掉字串(截掉輸入參數之前的所有字串) 
    public String substring(int start,int end) 截掉區間的字串(包左不包右)
    public int capacity() 返回當前容量。
    public int length()  返回長度(字元數)。

三、對比String和StringBuffer拼接字串的效率
package text;public class Text {    public static void main(String[] args) {        String s = "";        long start_s = System.currentTimeMillis();        for(int i = 0;i < 10000;i++){            s += "aaaa";        }        long end_s = System.currentTimeMillis();        System.out.println("String拼接N次用的時間" + (end_s - start_s) + "ms" );                StringBuffer ss = new StringBuffer();        long start_ss = System.currentTimeMillis();        for(int i = 0;i < 10000;i++){            ss.append("aaaa");        }        long end_ss = System.currentTimeMillis();        System.out.println("StringBuffer拼接N次用的時間" + (end_ss - start_ss) + "ms" );    }}

四、 Math

1.Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。

2.成員方法
  public static int abs(int a)  返回 int 值的絕對值。
  public static double ceil(double a)  向上取整
  public static double floor(double a) 向下取整
  public static int max(int a,int b)   比較兩個數的最大值
  public static double pow(double a,double b)  a的b次冪
  public static double random()  隨機數
  public static int round(float a) 四捨五入
  public static double sqrt(double a)  正平方根

例子:因為都為靜態,所以直接Math.方法用即可

package text;public class Text {    public static void main(String[] args) {        System.out.println(Math.E);  //輸出值為:2.718281828459045        System.out.println(Math.PI); //輸出值為:3.141592653589793        System.out.println(Math.abs(-123));//輸出值為:123,絕對值    }}
五、 Random

1.此類用於產生隨機數

2.構造方法

  public Random()  沒有給種子,用的是預設種子,是目前時間的毫秒值。
    例 Random r = new Random();
  public Random(long seed)  給出指定的種子,給出種子後每次得到的隨機數是相同的
    例 Random r = new Random(1201);

3.成員方法

  public int nextInt() 返回的是int範圍內的隨機數
    例 r.nextInt()  返回一個int範圍內的隨機數
  public int nextInt(int n) 返回的是【0,n】範圍內的隨機數
    例 r.nextInt(10) 返回0到10以內的隨機數

例子1:

package text;import java.util.Random;public class Text {    public static void main(String[] args) {        Random r = new Random();        int a = r.nextInt(10);        System.out.println(a);//傳回值為0~10內的隨機一個數    }}

例子2:

package text;import java.util.Random;public class Text {    public static void main(String[] args) {        for(int i=0;i<10;i++){   //for迴圈,迴圈10次,每次輸出一個0~10的隨機數            Random r = new Random();            int a = r.nextInt(10);            System.out.println(a);        }        }}

六、 Date

1.Date 表示特定的瞬間,精確到毫秒

2.構造方法

  public Date() 根據當前的毫秒值建立日期對象
  public Date(long date) 根據給定的毫秒值建立日期對象

3.成員方法
  public long getTime()  擷取目前時間
  public void setTime(long time)  設定時間

七、DateFormat

1.DateFormat 是日期/時間格式化子類的抽象類別,它以與語言無關的方式格式化並解析日期或時間。
  是抽象類別,所以使用其子類SimpleDateFormat

2.SimpleDateFormat(可以把日期轉換成String類型)

3.構造方法
  public SimpleDateFormat() 預設模式
  public SimpleDateFormat(String pattern)
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss")
4.成員方法
  public final String format(Date date) 把日期格式轉換成String類型
  public Date parse(String source)  把給定的字串解析成日期格式

例子:

package text;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class DateFormatUtil {    public static String dateToString(Date date){  //見名知意,日期轉字串        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");        return sdf.format(date);        }        public static Date stringToDate(String str){   //見名知意,字串轉日期        Date d = null;        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        try {            d = sdf.parse(str);        } catch (ParseException e) {                e.printStackTrace();        }        return d;    }}
package text;import java.util.Date;public class Text {    public static void main(String[] args) {                Date d = new Date();        System.out.println(DateFormatUtil.dateToString(d)); //輸出值為:2017年07月23日 14:55:23                String ss = "2015-07-23";        System.out.println(DateFormatUtil.stringToDate(ss)); //輸出值為:Thu Jul 23 00:00:00 CST 2015    }}
八、 Calendar

1.Calendar 類是一個抽象類別,它為特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日曆欄位之間的轉  換提供了一些方法,並為操作日曆欄位(例如獲得下星期的日期)提供了一些方法。

2.成員方法
  public static Calendar getInstance()  擷取目前時間
  Calendar c = Calendar.getInstance;

  public int get(int field) 返回給定日曆欄位的值。
  public void add(int field,int amount)  根據給定的日曆欄位和對應的時間,來對當前的日曆進行操作
  public final void set(int year,int month,int date) 設定當前的日曆時間

Java基礎之常用類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.