首頁  >  文章  >  Java  >  Java中的常用類別

Java中的常用類別

巴扎黑
巴扎黑原創
2016-12-10 09:38:011078瀏覽

API:Application Program Interface,應用程式介面  ,通俗而言就是系統提供的一組類別庫。

在API文件中找到lang包,裡面很多常用的類別。

1.基本型

基本資料型別    類別型

byte                    Byte🠎                       Integer

long                  

double                 Double

char                  類型分別有各自的屬性和方法,我們舉一個例子:

Integer類別

屬性:

       int的最大值:Integer.MAX_VALUE

     int的最大值:Integer.MAX_VALUE

整數轉成字串:  Integer.toString(100);

      將整數轉換為對應進位的字串: Integer.toString(a,2);

      將純數字轉換成整數:  ue ("12345");

      將純數字字符串轉成整數:     Integer.valueOf("1100011",2);

 

      將純數字字符串轉成整數:    Integer.parseInt("12345");

      將純數字字串轉成整數:   Integer.parseInt("1100011",2);

      將整數轉 Integer.toHexString(a);

      將整數轉成八進位:Integer.toOctalString(a);

 

例:

1.將正值整數90轉成八進制

Java代碼  

public class 常用類別 {  

  

    public static void  

        int a=90;  

        String s=Integer.toBinaryString(a);//將a轉成二進位  

        System.out.println(s);  

        將a轉成八進位  

        System.out.println(s1);  

       String s2=Integer.toHexString(a);//將a轉換為十六進位  

        System.out.println(s2);     String s3=Integer.toString(a, 2);//將a以二元形式的字串輸出  

        System.out.println(s3);  

       

 

 

2.Math類

屬性:   E

           PI

方法:Math .abs(t);    //a的絕對值

Math.max(a,b);    //a的最大值

Math.min(a,b);    //a的最小值

Math. pow(a,b);    //a的b次方

Math.sqrt(a);    //a的平方根

Math.random();    //隨機取得一個0.0到1.0之間的一個數

 

3.System類別

屬性:System.err.println();

           System.out.println(); it(0);    //正常退出程序

          System.currentTimeMillis();     //取得系統目前時間

         ).

參數1:源數組

參數2:來源數組中的起始下標位置

參數3:目標數組

參數4:目標資料中的起始下標位置

參數5:要複製的數量

例:int[] a = { 1, 2, 3, 4, 5 };

       int[] b = new int[10];

. , 1, b, 4, 3);

 

Java代碼  

import java.util.Scanner;  

  🎠.    public static void main(String[] args) {

          

        Scanner sc=new Scanner(System.in);  🠎5=new ;  

        int a=sc.nextInt();  

        int b= sc.nextInt();  

        int[]array1={2,9,11,5,8,3};  

         if(b==0){

            System.err.println("被除數不能為0");  

        }  

        int c=a/b;  

        System.out.println(c);       System.out.println(c); long x=System.currentTimeMillis();  

        System.out.println("現在的時間為 "+x);    System.arraycopy(array1, 1, array2, 2, 4);  

        System. out.print("陣列array2中的資料為 ");  

        for(int i=0;i

        }  

    }  

}  

號標準:一個字元是由哪些位元組構成,有多套不同的標準。

常用的字元集標準:

ISO-8859 西歐字元集,不包含全角字元

GB2312/GBK  簡體中文字元集

Big5    繁體的中文字元集繁文ANSI表示採用當地預設的字元集標準

 

建構方法

String(byte[] bytes)

String(byte[] bytes,"字元集編碼")

String(byte[]起始下標),length(取幾))

String(char[] c)

String(char[] c,start,length)

 

方法:

字串轉成位元組數組byte bs[]=s.getBytes();//得到一個字串陣列

 

字串依照指定字元集轉成位元組數組byte bs[]=s.getBytes("UTF-8");

 

將字串轉成字元陣列Char c[]=s.toCharArray();

 

將字串中和部分字元複製到字元陣列getChars(int srcBegin,int srcEnd src dstBegin);

 

取得指定位置的字元 char c=s.charAt(下標);

 

依字典順序比較兩個字串 compareTo(String anotherString);個字串,不考慮大小寫compareToIgnoreCase(String str);

 

判斷在字串中是否包含另一個字串contains(CharSquence(父類別) s);

 

個字尾結尾 endWith(String s);

 

判斷字串是否以某個前綴開始 startsWith(String s);

 

判斷兩個字串是否相等,忽略大小寫equalsInnoreCase(String s);

 

判斷子字串在大字串中出現的位置indexOf(s1);

 

字串子字串在大字串子字串在大字串中最後一次出現的位置s.lastIndexOf(s1);

 

得到字串的字元長度 .length();陣列的長度.length;

 

將替換字串 s.replace("oldChar","newChar");

 

將取代第一個字串s.replaceFrist("oldChar","newChar");

串 s.substring(startIndex(起始下標));

 

將替換字串s.substring(startIndex,endIndex(結束下標));

 

轉成小​​寫成小寫);大寫 toUpperCase(s1);

 

去掉字串首尾的空白字元 trim();

 

將其他類型的資料轉換為任意切割類型。 String[] id main(String[] args) {  

          

         

        String m="週";  

        String z="" ;  

        String n="";  

        String st="";       for(int i=0;i

            char c=s.charAt (i);  

            z=z+c;  

             for(int j=0;j

             t=str.charAt(j);        t=str.charAt(j);      System.out.print(z+t+"  ");  

        }  

        );  

          

        for(int i=0;i

             st=m.substring(0,1);        }  

        for(int j=0;j🎠  str.charAt(j);  

             System.out.print(st+t+"  ");  

}  

 結果是這樣的:

星期日 星期一 星期二 星期三 星期四 星期五 星期六 

週日 週一 週二 週三 週四 週五 週六 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn