20175305张天钰《java程序设计》第七周学习总结
《java程序設(shè)計》第七周學(xué)習(xí)總結(jié)
第八章 常用實用類
1.String類
1.String類不可以有子類。
2.用戶無法輸出String對象的引用,輸出的是字符序列
3.構(gòu)造方法:String s = new String("We are students");
4.其他構(gòu)造方法:String (char a[ ]) 和 String(char a[],int startIndex,int count)
2.字符串的并置
*1.String對象可以用“+”進(jìn)行并置運算,即首尾相接得到一個新的String對象。
3.String類的常用方法
1.public int length( ) 用來獲取一個String對象的字符序列的長度;
2.public boolean equals(String s) 用來比較當(dāng)前String對象的字符序列是否與參數(shù)s指定的String對象的字符序列相同;
3.public boolean startsWith(String s)、public boolean endsWith(Sting s) 判斷當(dāng)前String對象的字符序列前綴是否是參數(shù)指定的String對象s的字符序列;
4.public boolean contains(String s) 用來判斷當(dāng)前String對象的字符序列是否包含參數(shù)s的字符序列;
5.public int indexOf (string s) 從當(dāng)前String對象的字符序列的0索引位置開始檢索首次出現(xiàn)s的字符序列位置并返回該位置,若沒檢索到,該方法的返回值為-1;
6.public int lastIndexOf(String s) 從當(dāng)前String對象的字符序列的0索引位置開始檢索最后一次出現(xiàn)s的字符序列的位置,并返回該位置,若沒檢索到,則返回-1;
*7.public String trim() 得到一個新的String對象,這個String對象的字符序列是當(dāng)前String對象的字符序列去掉前后空格后的字符序列。
4.字符串與字符、字節(jié)數(shù)組
1.字符串與字符數(shù)組的方法:getChars//復(fù)制到參數(shù)c指定的數(shù)組中和public char[] toCharArray()
2.字符串與字節(jié)數(shù)組的方法:
String(byte[],int offset,int length)
public byte[] getBytes()
public byte[] getBytes(String charsetName)
*3.字符串的加密算法:P186
5.正則表達(dá)式:
*正則表達(dá)式是一個String對象的字符序列,該字符序列中含有具有特殊意義的字符,這些特殊字符稱作正則表達(dá)式的元字符
元字符:
6.常用使用類
1)StringTokenizer類
1.StringTokenizer(String s)
2.StringTokenizer(String s, String delim)
2)Scanner類
1.next()方法用來一次返回被解析的字符序列中的單詞
2.如果含有數(shù)字可以用nextInt()和nextDouble()來代替next()方法。
3)StringBuffer類
1.length():獲取實體中存放的字符序列的長度。
2.capacity():獲取當(dāng)前實體的實際容量。
3.StringBuffer append(String s):將String對象s的字符序列追加到當(dāng)前StringBuffer對象的字符序列中,并返回當(dāng)前StringBuffer對象的引用。
4、public char charAt(int n)和public void setCharAt(int n,char ch):得到序列位置n上的字符和替換ch指定的字符。
5、StringBuffer insert(int index,String str):將參數(shù)str指定的字符序列插入到參數(shù)index指定的位置。
6、public StringBuffer reverse():將對象實體中的字符序列翻轉(zhuǎn)。
7、StringBuffer delete(int startIndex,int endIndex):從當(dāng)前StringBuffer對象的字符序列中刪除一個子字符序列,并返回當(dāng)前對象的引用。
8、StringBuffer replace(int startIndex,int endIndex,String str):將當(dāng)前StringBuffer對象的字符序列的一個子字符序列用參數(shù)str指定的字符序列替換。
4)Date類與Calendar類
1.使用無參數(shù)構(gòu)造方法:Date()
2.使用帶參數(shù)的構(gòu)造方法:Date(long time)
3.使用Calendar類的static方法getInstance()可以初始化一個日歷對象:Calendar calendar = Calendar.getInstance()
Date date = new Date();
long dt=315532800;//十年前 單位是ms 1000ms=1s
date.setTime(System.currentTimeMillis()-dt1000);
/formate方法/
/* %ty 年 %tm 月 %td 日 /
/ %tY 4位年 */
System.out.println(String.format("%tY-%<tm-%<td",date));
System.out.println(String.format("%tm/%<td %<tY",date));
System.out.print(String.format(Locale.CHINA,"%tY年%<tm月%<td日 %<ta",date));
5)日期的格式化
1.format方法:format(格式化模式,日期列表)
2.format(Locale locale,格式化模式,日期列表);
6)Math類:用于靜態(tài)方法、常量E和PI
1.public static long abs(double a)
2.public static double max(double a,double b)
*3.public static double min(double a,double b)
7)BigInteger類:用于處理特別大的整數(shù)
1.public BigInteger add(BigInteger val)
2.public BigInteger subtract(BigInteger val)
*3.public BigInteger multiply(BigInteger val)
8)Random類:用于產(chǎn)生隨機數(shù)
7.托管代碼
public static void main(String []args){
String mess="姓名:張三 出生時間:1989.10.16.個人網(wǎng)站:http://www.zhang.com 身高:185cm,體重:72kg";
int index= mess.indexOf(":"); //返回字符串中首次出現(xiàn)冒號的位置
String name=mess.substring(index+1);
if(name.startsWith("張")) System.out.println("簡歷中的姓名姓張");
index= mess.indexOf(":",index+1); //返回字符串中第2次出現(xiàn)冒號的位置
String date=mess.substring(index+1,index+11);
System.out.println(date);
index=mess.indexOf(":",index+1);
int heightPosition= mess.indexOf("身",index+1);//返回字符串中首次出現(xiàn)身高的位置
String personNet=mess.substring(index+1,heightPosition-1);
System.out.println(personNet);
index=mess.indexOf(":",heightPosition);//返回字符串中身高后面的冒號位置
int cmposition=mess.indexOf("cm");
String height=mess.substring(index+1,cmposition);
height=height.trim();
int h=Integer.parseInt(height);
if(h>=180){
System.out.println("簡歷中的身高"+height+"大于或等于180cm");
}
else {
System.out.println("簡歷中的身高"+height+"小于180cm");
}
index=mess.lastIndexOf(":");//返回字符串中最后一個冒號的位置
int kgPosition=mess.indexOf("kg");//轉(zhuǎn)化為整形
String weight=mess.substring(index+1,kgPosition);
weight=weight.trim();
int w=Integer.parseInt(weight);
if(w>=75){
System.out.println("簡歷中的體重"+weight+"大于或等于75kg");
}
else{
System.out.println("簡歷中的體重"+weight+"小于75kg");
}
}
轉(zhuǎn)載于:https://www.cnblogs.com/zhangtianyu/p/10707612.html
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的20175305张天钰《java程序设计》第七周学习总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Delphi:第一个hello worl
- 下一篇: 线性表之链式存储结构_单链表相关算法