java常用類

常用類
1. 字符串相關類(String、StringBuffer)
1.1 String類
1.1.1 java.lang.String類代表不可變的字符序列
1.1.2 "abcde" 爲該類的一個對象
1.1.3 String類的常見構造方法:
String(String original) //創建一個String對象爲original的拷貝
String(char[] value) //用一個字符數組創建一個String對象
String(char[] value,int offset,int count)//用一個字符數組從offset項開始的count個字符序列創建一個
//String對象
1.1.4 example1
public class StudyString {
public static void main(String[] args) {
String s1 = "金剛葫蘆娃";
String s2 = "人蔘娃";
String s3 = "金剛葫蘆娃";
System.out.println(s1 == s3); //true

String s4 = new String("金剛葫蘆娃");
String s5 = new String("金剛葫蘆娃");
System.out.println(s4 == s5); //false
System.out.println(s4.equals(s5)); //true

char song[] = {'n','e','v','e','r',' ','s','a','y',' ',
'g','o','o','d','b','y','e'};
String s6 = new String(song);
String s7 = new String(song, 10, 4);
System.out.println(s6); //never say goodbye
System.out.println(s7); //good

}
}
1.1.5 String類常用方法
public char charAt(int index) //返回字符串中第index個字符
public int length() 返回字符串的長度
public int indexOf(String str) //返回字符串中出現str的第一個位置
public int indexOf(String str, int fromIndex) //返回字符串中從fromIndex開始出現str的第一個位置
public boolean equalsIgnoreCase(String another) //比較字符串與another是否一樣(忽略大小寫)
public String replace(char oldChar, char newChar)//在字符串中用newChar字符替換oldChar字符

public boolean startsWidth(String prefix) //判斷字符串是否以prefix字符串開頭
public boolean endsWidth(String suffix) //判斷字符串是否以suffix字符串結尾
public String toUpperCase() //返回一個字符串爲該字符串的大寫形式
public String toLowerCase() //返回一個字符串爲該字符串的小寫形式
public String substring(int beginIndex) //返回該字符串從beginIndex開始到結尾的字符串
public String substring(int beginIndex,int endIndex)//返回從beginIndex開始到endIndex結尾的子字符串
public String trim() //返回將該字符串去掉開頭和結尾空格的字符串

//靜態重載方法
public static String valueOf(...) //可以將基本類型數據轉換爲字符串

public String[] split(String regex) //可以將一個字符串按照指定的分隔符分隔,返回分隔後的字符串數組
1.1.6 example2
public class StudyString {
public static void main(String[] args) {
String s1 = "you jump,I jump";
String s2 = "You Jump,I Jump";
System.out.println(s1.charAt(1)); //o
System.out.println(s1.length()); //15
System.out.println(s1.indexOf("jump")); //4
System.out.println(s1.indexOf("Jump")); //-1
System.out.println(s1.equals(s2)); //false
System.out.println(s1.equalsIgnoreCase(s2)); //true

String s3 = s1.replace("jump","not jump");
System.out.println(s3); //you not jump,I not jump
}
}
1.2 StringBuffer類
1.2.1 java.lang.StringBuffer代表可變的字符序列
1.2.2 StringBuffer和String類似,但StringBuffer可以對其字符串進行改變
1.2.3 StringBuffer類的常見的構造方法:
StringBuffer() 創建一個不包含字符序列的"空"的StringBuffer對象
StringBuffer(String str) 創建一個StringBuffer對象,包含與String對象str相同的字符序列
1.2.4 StringBuffer常用方法
//重載方法public StringBuffer append(...) 可以爲該StringBuffer 對象添加字符序列,
//返回添加後的該StringBuffer對象引用
public StringBuffer append(String str)
public StringBuffer append(StringBuffer sbuf)
public StringBuffer append(char[] str)
public StringBuffer append(char[] str, int offset, int len)
public StringBuffer append(double d)
public StringBuffer append(Object obj)

//重載方法public StringBuffer insert(...) 可以爲該StringBuffer對象在指定位置插入字符序列,
//返回修改後的該StringBuffer對象引用
public StringBuffer insert( int offset, String str)
public StringBuffer insert( int offset double d)

//delete方法可以刪除從start開始到end-1爲止的一段字符序列,返回修改後的該StringBuffer對象的引用
public StringBuffer delete(int start,int end)

1.2.5 examle3
public class StudyStringBuffer {
public static void main(String[] args) {
String s = "C:\Program Files";
char[] c = {'p','h','o','n','e'};
StringBuffer sb1 = new StringBuffer(s);
sb1.apped('\').append("easyMule");
System.out.println(sb1); //C:\Program Files\easyMule

StringBuffer sb2 = new StringBuffer("海豚");
for(int i=0; i<=9; i++) {
sb2.append(i);
}
System.out.println(sb2); //海豚0123456789

sb2.delete(8,sb2.length()).insert(0,a);
System.out.println(sb2); //phone海豚012345
System.out.println(sb2.reverse()); //543210豚海enohp
}
}

2. 基本數據類型包裝類
2.1 包裝類(如: Integer, Double等)這些類封裝了一個相應的基本數據類型數值,併爲其提供了一系列操作。
2.2 以java.lang.Integer類爲例;構造方法:
Integer(int value)
Integer(String s)
2.3 以下是java.lang.Integer的常用方法
public static final int MAX_VALUE //最大的int型數(2^31-1)
public static final int MIN_VALUE //最小的int型數(-2^31)
pubic long longValue() //返回封裝數據的long型值
public double doubleValue() //返回封裝數據的double型值
public int intValue() //返回封裝數據的int型值
public static int parseInt(String s) throws NumberFormatException //將字符串解析成int型數據,返回該數據
public static Integer valueOf(String s) throws NumberFormatException// 返回Integer對象, 其中封裝的
整型數據爲字符串s表示
3. Math類
3.1 jav.lang.Math提供了一系列靜態方法用於科學計算;其方法的參數和返回值類型一般爲double型
abs //絕對值
acos,asin,atan,cos,sin,tan //三角函數
sqrt //平方根
pow(double a,double b) //a的b次冪
log //自然對數
exp //e爲底指數
max(double a, double b)
min(double a.double b)
random() //返回0.0到1.0的隨機數
long round(double a) //double型的數據a轉換爲long型(四捨五入)
toDegrees(double angrad) //弧度->角度
toRadians(double angdeg) //角度->弧度

4. File類
4.1 java.io.File類代表系統文件名(路徑和文件名).
4.2 File類的常見構造方法:
4.2.1 public File(String pathname)
以pathname爲路徑創建File對象,如果pathname是相對路徑,則默認的當前路徑在系統
屬性user.dir中存儲
4.2.2 public File(String parent,String child)
以parent爲父路徑,child爲子路徑創建File對象
4.3 File的靜態屬性String separator存儲了當前系統的路徑分隔符
4.4 通過File對象可以訪問文件的屬性
public boolean canRead()
public boolean canWrite()
public boolean exists()
public boolean isDirectory()
public boolean isFile()
public boolean isHidden()
public long lastModified()
public long length()
public String getName()
public String getPath()
4.5 通過File對象創建空文件或目錄(在該對象所指的文件或目錄不存在的情下)
public boolean createNewFile() throws IOException
public boolean delete()
public boolean mkdir()
public boolean mkdirs() //創建在路徑中的一系列目錄

5. 枚舉類
5.1 枚舉類型:
只能夠取特定值中的一個
使用enum關鍵字
是java.lang.Enum類型
5.2 枚舉類型的定義和調用
public enum MyGrade { one, two, three, four };
MyGrade g = MyGrade.one; //枚舉類型的調
發佈了41 篇原創文章 · 獲贊 2 · 訪問量 2981
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章