




免費(fèi)預(yù)覽已結(jié)束,剩余88頁可下載查看
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1 第五章常用實(shí)用類 2 Java使用簡(jiǎn)單的類型 如整形 int 字符 char 等 這些數(shù)據(jù)類型不是對(duì)象層次結(jié)構(gòu)的組成部分 它們通過值傳遞給方法而不能直接通過引用傳遞 而且也沒有辦法使兩種方法對(duì)整形引用同一實(shí)例 但是 有時(shí)需要對(duì)這些簡(jiǎn)單的類型建立對(duì)象表達(dá)式 如處理對(duì)象的枚舉類 要將簡(jiǎn)單類型存儲(chǔ)到這些類中的一個(gè) 需要在類中包裝簡(jiǎn)單類型 為了滿足這種需要 Java提供了與每一個(gè)簡(jiǎn)單類型相對(duì)應(yīng)的類 從本質(zhì)上講 這些類在類中包裝簡(jiǎn)單類型 因此通常被稱作類型包裝器 3 5 1Double類和Float類 Double和Float分別是對(duì)類型double和float的浮點(diǎn)值的包裝器 Float類的構(gòu)造函數(shù)如下 Float doublenum Float floatnum Float Stringstr throwsNumberFormatException 注 str是由浮點(diǎn)數(shù)組成的字符串 否則拋出異常 Double類的構(gòu)造函數(shù)如下 Double doublenum Double Stringstr throwNumberFormatException 一 構(gòu)造函數(shù) 4 二 常量 在Float類和Double類中定義的常量 MAX VALUEMIN VALUENaNPOSITIVE INFINITYNEGETIVE INFINITY 最大正值最小正值非具體數(shù)字正無窮負(fù)無窮 try Doubled2 newDouble 123 45ae3 System out println d2 d2 catch NumberFormatExceptione System out println formaterror 5 三 Float類的常用方法 6 三 Float類的常用方法 7 四 Double類的常用方法 8 四 Double類的常用方法 9 publicclassInfNaN publicstaticvoidmain String args Doubled1 newDouble 123 Doubled2 newDouble 123 45 Doubled3 newDouble 0 0 0 0 System out println d1 isInfinite System out println d2 d2 System out println d3 isNaN falsed2 123 45true 10 5 2Byte Short Integer和Long類 Byte Short Integer 和Long類分別是字節(jié)型 byte 短整型 short 整型 int 和長(zhǎng)整型 long 整數(shù)類型的包裝器 Byte類的構(gòu)造函數(shù)如下 Byte bytenum Byte Stringstr throwsNumberFormatException 注 str是由浮點(diǎn)數(shù)組成的字符串 否則拋出異常 Short類的構(gòu)造函數(shù)如下 Short shortnum Short Stringstr throwNumberFormatException 一 構(gòu)造函數(shù) 11 Integer類的構(gòu)造函數(shù)如下 Integer intnum Integer Stringstr throwsNumberFormatException 注 str是由整數(shù)組成的字符串 否則拋出異常 Long類的構(gòu)造函數(shù)如下 Long longnum Long Stringstr throwNumberFormatException 12 二 Byte類的常用方法 13 二 Byte類的常用方法 14 三 Short Integer和Long類的常用方法 同Byte類的常用方法 15 publicclassIsChange publicstaticvoidmain String args Integeri1 newInteger 123 sub 1 i1 i1 System out println i1 i1 publicstaticvoidsub 1 Integerx1 Integery1 x1 Integer valueOf x1 toString 123 y1 Integer valueOf y1 toString 231 System out println x1 x1 System out println y1 y1 輸出 x1 123123y1 123231i1 123 16 5 3Character類 Character是對(duì)類型char值的包裝器 Character類只能用于存儲(chǔ)和操作單一的字符數(shù)值 Character類的對(duì)象包含了單一字符數(shù)值 Character charc 一 構(gòu)造函數(shù) 參數(shù)必須為一個(gè)char類型數(shù)據(jù) 它創(chuàng)建了一個(gè)Character對(duì)象 該對(duì)象包含了由參數(shù)提供的數(shù)值 一旦Character對(duì)象被創(chuàng)建 它包含的數(shù)值就不能改變 下面的語句將創(chuàng)建一個(gè)Character對(duì)象a 該對(duì)象包含字符數(shù)據(jù) b Charactera newCharacter b 17 二 Character類的常用方法 18 二 Character類的常用方法 19 classIsDemo publicstaticvoidmain Stringargs chara a b 5 A for inti 0 i a length i if Character isDigit a i System out println a i isadigit if Character isLetter a i System out println a i isaletter if Character isWhitespace a i System out println a i iswhitespace if Character isUpperCase a i System out println a i isuppercase if Character isLowerCase a i System out println a i islowercase aisaletter aislowercase bisaletter bislowercase 5isadigit Aisaletter Aisuppercase iswhitespace 20 classCharDemo publicstaticvoidmain Stringargs charc1 a c2 b Characterm1 newCharacter k Characterm2 newCharacter k System out println c1 c2 System out println m1 equals m2 System out println m1 m2 System out println c1 equals c2 falsetrue falsetruefalse 有錯(cuò) Characterm2 m1 falsetruetrue 21 5 4Boolean類 Boolean是一個(gè)圍繞布爾 boolean 值的非常細(xì)小的包裝器 主要用在通過引用傳遞布爾 boolean 變量的場(chǎng)合 它包含了常數(shù)TRUE和FALSE 這些常數(shù)定義了布爾 Boolean 對(duì)象的真與假 Boolean booleanboolValue Boolean StringboolString 一 構(gòu)造函數(shù) 第一種形式中 boolValue要么是true 要么是false 在第二種形式中 如果在boolString中包含了字符串 true 無論是大寫形式還是小寫形式 則新的布爾 Boolean 對(duì)象將為真 否則為假 22 二 Boolean類的常用方法 23 字符串指的是字符的序列 有兩種類型的字符串 一種是創(chuàng)建以后不需要改變的 稱為字符串常量 在Java中 String類用于存儲(chǔ)和處理字符串常量 另外一種字符串是創(chuàng)建以后 需要對(duì)其進(jìn)行改變的 稱為字符串變量 在Java中 StringBuffer類用于存儲(chǔ)和操作字符串變量 5 5字符串String 24 一 String類的構(gòu)造方法 在Java中 沒有內(nèi)置的字符串類型 字符串常量是作為String類的對(duì)象存在的 String類的對(duì)象表示的是字符串常量 一個(gè)字符串常量創(chuàng)建以后就不能夠被修改了 所以在創(chuàng)建String類對(duì)象時(shí) 通常需要向構(gòu)造函數(shù)傳遞參數(shù)來指定創(chuàng)建的字符串的內(nèi)容 publicString 該構(gòu)造函數(shù)用于創(chuàng)建一個(gè)空的字符串常量 publicString Stringvalue 該構(gòu)造函數(shù)用于根據(jù)一個(gè)已經(jīng)存在的字符串常量來創(chuàng)建一個(gè)新的字符串常量 該字符串的內(nèi)容和已經(jīng)存在的字符串常量一致 25 一 String類的構(gòu)造方法 publicString charvalue 該構(gòu)造函數(shù)用于根據(jù)一個(gè)已經(jīng)存在的字符數(shù)組來創(chuàng)建一個(gè)新的字符串常量 數(shù)組將在本章后面的內(nèi)容中介紹 publicString StringBufferbuffer 該構(gòu)造函數(shù)用于根據(jù)一個(gè)已經(jīng)存在的StringBuffer對(duì)象來創(chuàng)建一個(gè)新的字符串常量 publicString chara intstartindex intcount 該構(gòu)造函數(shù)提取字符數(shù)組a中的一部分字符創(chuàng)建一個(gè)字符串對(duì)象 參數(shù)startindex和count分別指定在a中提取字符的起始位置和從該位置開始截取的字符個(gè)數(shù) 26 二 String類的常用方法 在Java中 String類包含有50多個(gè)方法來實(shí)現(xiàn)字符串的各種操作 以下介紹一些我們需要經(jīng)常使用的方法 1 字符串連接 publicStringconcat Stringstr 該方法的參數(shù)為一個(gè)String類對(duì)象 作用是將參數(shù)中的字符串str連接到原來字符串的后面 產(chǎn)生一個(gè)新的對(duì)象 2 求字符串的長(zhǎng)度 publicintlength 返回字串的長(zhǎng)度 這里的長(zhǎng)度指的是字符串中Unicode字符的數(shù)目 27 二 String類的常用方法 3 求字符串中某一位置的字符 publiccharcharAt intindex 該方法在一個(gè)特定的位置索引一個(gè)字符串 以得到字符串中指定位置的字符 值得注意的是 在字符串中第一個(gè)字符的索引是0 第二個(gè)字符的索引是1 依次類推 最后一個(gè)字符的索引是length 1 28 classStringDemo publicstaticvoidmain Stringargs Stringa newString 這是一個(gè)新的程序 a a concat 123 System out println a System out println a 456 System out print 字符串a(chǎn)中共有 System out println a length 個(gè)字符 System out println 這11個(gè)字符為 for inti 0 i a length i System out print a charAt i 輸出 這是一個(gè)新的程序123這是一個(gè)新的程序123456字符串a(chǎn)共有11個(gè)字符這11個(gè)字符為 這是一個(gè)新的程序123 Stringa 這是一個(gè)新的程序 29 其比較過程實(shí)際上是兩個(gè)字符串中相同位置上的字符按Unicode中排列順序逐個(gè)比較的結(jié)果 如果在整個(gè)比較過程中 沒有發(fā)現(xiàn)任何不同的地方 則表明兩個(gè)字符串是完全相等的 compareTo方法返回0 如果在比較過程中 發(fā)現(xiàn)了不同的地方 則比較過程會(huì)停下來 這時(shí)一定是兩個(gè)字符串在某個(gè)位置上不相同 如果當(dāng)前字符串在這個(gè)位置上的字符大于參數(shù)中的這個(gè)位置上的字符 compareTo方法返回一個(gè)大于0的整數(shù) 否則返回一個(gè)小于0的整數(shù) 這個(gè)數(shù)是當(dāng)前位置上所對(duì)應(yīng)字符的Unicode代碼的差值 二 String類的常用方法 4 字符串的比較 方法一 publicintcompareTo StringanotherString 該方法比較兩個(gè)字符串 和Character類提供的compareTo方法相似 Character類提供的compareTo方法比較的是兩個(gè)字符類數(shù)據(jù) 而這里比較的是字符串?dāng)?shù)據(jù) 30 classExample5 3 publicstaticvoidmain Stringargs Stringa boy apple Applet girl Hat Stringtemp for inti 0 i a length 1 i for intj i 1 j a length j if a j compareTo a i 0 temp a i a i a j a j temp for inti 0 i a length i System out print a i 例1 將字符串從小到大排序 31 方法二 publicbooleanequals ObjectanObject 該方法比較兩個(gè)字符串 和Character類提供的equals方法相似 因?yàn)樗鼈兌际侵剌dObject類的方法 該方法比較當(dāng)前字符串和參數(shù)字符串 在兩個(gè)字符串相等的時(shí)候返回true 否則返回false 方法三 publicbooleanequalsIgnoreCase StringanotherString 該方法和equals方法相似 不同的地方在于 equalsIgnoreCase方法將忽略字母大小寫的區(qū)別 二 String類的常用方法 32 classExample5 1 publicstaticvoidmain Stringargs Strings1 s2 s1 newString wearestudents s2 newString wearestudents System out println s1 equals s2 System out println s1 s2 s1 howareyou s2 howareyou System out println s1 equals s2 System out println s1 s2 truefalsetruetrue s1 s2 wearestudents wearestudents howareyou 33 利用String類提供的substring方法可以從一個(gè)大的字符串中提取一個(gè)子串 該方法有兩種常用的形式 方法一 publicStringsubstring intbeginIndex 該方法從beginIndex位置起 從當(dāng)前字符串中取出剩余的字符作為一個(gè)新的字符串返回 方法二 publicStringsubstring intbeginIndex intendIndex 該方法從當(dāng)前字符串中取出一個(gè)子串 該子串從beginIndex位置起至endIndex 1為結(jié)束 子串的長(zhǎng)度為endIndex beginIndex 5 從字符串中提取子串 二 String類的常用方法 34 classExample5 2 publicstaticvoidmain Stringargs intnumber 0 Strings student entropy engage english client Stringtemp for intk 0 k s length 2 k temp s substring k k 2 if temp equals en number System out println number number 例 求 en 在字符串中出現(xiàn)的次數(shù) 35 classExample5 6 publicstaticvoidmain Stringargs Strings 巴西足球隊(duì)擊敗德國(guó)足球隊(duì) Stringss for inti s length 1 i 0 i ss ss concat s substring i i 1 System out println ss 例 將字符串逆序后輸出 初始化 36 方法三 publicvoidgetChars intstart intend charc intoffset 將當(dāng)前字符串中從位置start到end 1的字符復(fù)制到數(shù)組c中 并從數(shù)組c的offset位置開始存放這些字符 需要注意的是 必須保證數(shù)組c能容納下要被復(fù)制的字符 5 從字符串中提取子串 二 String類的常用方法 37 classExample5 6 publicstaticvoidmain Stringargs charc d Strings 巴西足球隊(duì)擊敗德國(guó)足球隊(duì) c newchar 2 s getChars 5 7 c 0 System out println c d newchar s length s getChars 7 12 d 0 s getChars 5 7 d 5 s getChars 0 5 d 7 System out println d 輸出結(jié)果 擊敗德國(guó)足球隊(duì)擊敗巴西足球隊(duì) 38 classExample5 6 publicstaticvoidmain Stringargs Strings 巴西足球隊(duì)擊敗德國(guó)足球隊(duì) Stringss ss s substring 7 12 ss ss concat s substring 5 7 ss ss concat s substring 0 5 System out println ss 39 5 從字符串中提取子串 二 String類的常用方法 方法四 publicbooleanregionMatches intfirstStart Stringother intotherStart intlength 從當(dāng)前字符串指定的位置firstStart開始 取長(zhǎng)度為length的一個(gè)子串 并將這個(gè)子串和參數(shù)other指定的一個(gè)子串進(jìn)行比較 其中 other指定的子串是從參數(shù)otherStart指定的位置開始 如果兩個(gè)子串相等 則返回true 否則返回false 40 classExample5 2 publicstaticvoidmain Stringargs intnumber 0 Strings student entropy engage english client for intk 0 k s length k if s regionMatches k en 0 2 number System out println number number 例 求 en 在字符串中出現(xiàn)的次數(shù) 41 判斷字符串的前綴是否為指定的字符串利用String類提供的下列方法 publicbooleanstartsWith Stringprefix 該方法用于判斷當(dāng)前字符串的前綴是否和參數(shù)中指定的字符串prefix一致 如果是 返回true 否則返回false publicbooleanstartsWith Stringrefix inttoffset 該方法用于判斷當(dāng)前字符串從toffset位置開始的子串的前綴是否和參數(shù)中指定的字符串prefix一致 如果是 返回true 否則返回false publicbooleanendsWith Stringsuffix 該方法用于判斷當(dāng)前字符串的后綴是否和參數(shù)中指定的字符串suffix一致 如果是 返回true 否則返回false 6 判斷字符串的前綴和后綴 二 String類的常用方法 42 7 字符串中單個(gè)字符的查找 二 String類的常用方法 字符串中單個(gè)字符的查找可以利用String類提供的下列方法 方法一 publicintindexOf charch 該方法用于查找當(dāng)前字符串中某一個(gè)特定字符ch出現(xiàn)的位置 該方法從頭向后查找 如果在字符串中找到字符ch 則返回字符ch在字符串中第一次出現(xiàn)的位置 如果在整個(gè)字符串中沒有找到字符ch 則返回 1 方法二 publicintindexOf charch intfromIndex 該方法和第一種方法類似 不同的地方在于 該方法從fromIndex位置向后查找 返回的仍然是字符ch在字符串第一次出現(xiàn)的位置 43 方法三 publicintlastIndexOf charch 該方法和第一種方法類似 不同的地方在于 該方法從字符串的末尾位置向前查找 返回的是字符ch在字符串中最后一次出現(xiàn)的位置 方法四 publicintlastIndexOf charch intfromIndex 該方法和第二種方法類似 不同的地方在于 該方法從fromIndex位置向前查找 返回的是字符ch在字符串中從位置fromIndex開始向前查找時(shí)第一次出現(xiàn)的位置 7 字符串中單個(gè)字符的查找 二 String類的常用方法 44 8 字符串中子串的查找 二 String類的常用方法 字符串中子串的查找與字符串中單個(gè)字符的查找十分相似 可以利用String類提供的下列方法 publicintindexOf Stringstr publicintindexOf Stringstr intfromIndex publicintlastIndexOf Stringstr publicintlastIndexOf Stringstr intfromIndex 45 9 字符串中字符大小寫的轉(zhuǎn)換 二 String類的常用方法 字符串中字符大小寫的轉(zhuǎn)換 可以利用String類提供的下列方法 方法一 publicStringtoLowerCase 該方法將字符串中所有字符轉(zhuǎn)換成小寫 并返回轉(zhuǎn)換后的新串 方法二 publicStringtoUpperCase 該方法將字符串中所有字符轉(zhuǎn)換成大寫 并返回轉(zhuǎn)換后的新串 46 11 字符串中字符的替換 二 String類的常用方法 方法一 publicStringreplace charoldChar charnewChar 該方法用字符newChar替換當(dāng)前字符串中所有的字符oldChar 并返回一個(gè)新的字符串 publicStringtrim 該方法只是去掉開頭和結(jié)尾的空格 并返回得到的新字符串 值得注意的是 在原來字符串中間的空格并不去掉 10 字符串多余空格的去除 47 方法二 publicStringreplaceFirst Stringregex Stringreplacement 該方法用字符串replacement的內(nèi)容替換當(dāng)前字符串中遇到的第一個(gè)和字符串regex相一致的子串 并將產(chǎn)生的新字符串返回 方法三 publicStringreplaceAll Stringregex Stringreplacement 該方法用字符串replacement的內(nèi)容替換當(dāng)前字符串中遇到的所有和字符串regex相一致的子串 并將產(chǎn)生的新字符串返回 11 字符串中字符的替換 二 String類的常用方法 48 classExample5 6 publicstaticvoidmain Stringargs Strings thisisthedog oh thatisthecat themice s s replaceAll the a System out println s 49 5 6字符串變量StringBuffer StringBuffer類對(duì)象表示的是字符串變量 每一個(gè)StringBuffer類對(duì)象都是可以擴(kuò)充和修改的字符串變量 publicStringBuffer 創(chuàng)建一個(gè)空的字符串對(duì)象 分配給該對(duì)象的初始容量為16個(gè)字符 當(dāng)實(shí)體存放的字符序列的長(zhǎng)度大于16時(shí) 實(shí)體的容量自動(dòng)增加 以便存放新的字符串 publicStringBuffer intlength 創(chuàng)建一個(gè)空的字符串對(duì)象 分配給該對(duì)象的初始容量為size個(gè)字符 當(dāng)實(shí)體存放的字符序列的長(zhǎng)度大于size時(shí) 實(shí)體的容量自動(dòng)增加 以便存放新的字符串 一 StringBuffer類的構(gòu)造方法 50 一 StringBuffer類的構(gòu)造方法 publicStringBuffer Stringstr 創(chuàng)建一個(gè)字符串對(duì)象 該對(duì)象的初值為str 分配給該對(duì)象的初始容量為字符串str的長(zhǎng)度再加上16個(gè)字符 當(dāng)實(shí)體存放的字符序列的長(zhǎng)度大于size時(shí) 實(shí)體的容量自動(dòng)增加 以便存放新的字符串 注 length 方法可以獲取對(duì)象中存放的實(shí)際字符的個(gè)數(shù) capacity 方法可以獲取對(duì)象的容量 StringBuffers this 是錯(cuò)誤的 只能寫成 StringBuffers newStringBuffer this StringBuffer對(duì)象不能使用運(yùn)算符 進(jìn)行串連接 51 二 StringBuffer類的常用方法 52 二 StringBuffer類的常用方法 53 classExample5 10 publicstaticvoidmain Stringargs StringBufferstr newStringBuffer 我們大家都很愿意學(xué)習(xí)Java語言 str setCharAt 0 w str setCharAt 1 e System out println str str insert 2 all System out println str str delete 6 8 System out println str intindex str indexOf 都 str replace index str length lovejava System out println str we大家都很愿意學(xué)習(xí)Java語言weall大家都很愿意學(xué)習(xí)Java語言weall都很愿意學(xué)習(xí)Java語言weallloveJava 54 5 7String和StringBuffer的區(qū)別 String創(chuàng)建的對(duì)象是字符串常量 而StringBuffer創(chuàng)建的對(duì)象是字符串變量 常量的值是不能改變的 而變量的值是可以改變的 這里的 可變 和 不可變 和是不是final沒有關(guān)系 例 Stringstr1 hello Stringstr2 world str1 str1 concat str2 例 StringBufferstr1 newStringBuffer hello StringBufferstr2 newStringBuffer world str1 str1 append str2 s1 s2 hello world helloworld s1 s2 hello world world 55 注 String是一種非常常用的數(shù)據(jù)類型 但由于String是不可變對(duì)象 在進(jìn)行String的相關(guān)操作的時(shí)候會(huì)產(chǎn)生許多臨時(shí)的String對(duì)象 而StringBuffer在操作上是在一個(gè)緩沖中進(jìn)行的 性能當(dāng)然優(yōu)越得多 不過 一般做為簡(jiǎn)單的字符串傳遞和其它操作 只不要改變字符串內(nèi)容的操作 用String效率會(huì)高一些 5 7String和StringBuffer的區(qū)別 56 publicclassReplace publicstaticvoidmain String args Strings1 newString thisis StringBuffersv1 newStringBuffer thisis s1 concat adog sv1 append acat System out println s1 System out println sv1 thisisthisisacat 5 7String和StringBuffer的區(qū)別 57 publicclassUntitled1 publicstaticvoidmain String args Strings1 String類的值是不是會(huì)變的 Strings2 s1 s1 加個(gè)試試 System out println s2 StringBufferb1 newStringBuffer StringBuffer類的值是會(huì)變的 StringBufferb2 b1 b1 append 加個(gè)試試 System out println b2 輸出結(jié)果 String類的值是不是會(huì)變的 StringBuffer類的值是會(huì)變的 加個(gè)試試 58 5 8StringTokenizer的類 有時(shí)我們需要分析字符串并將字符串分解成可被獨(dú)立使用的單詞 這些單詞叫做語言符號(hào) 例如 對(duì)于字符串 wearestudents 如果我們把空格作為該字符串的分隔符 那么該字符串有三個(gè)單詞 語言符號(hào) 當(dāng)我們分析一個(gè)字符串并將字符串分解成可被獨(dú)立使用的單詞時(shí) 可以使用java util包中的StringTokenizerd類 59 一 構(gòu)造方法 StringTokenizer Strings 為字符串s構(gòu)造一個(gè)分析器 使用默認(rèn)的分隔符集合 即空格符 若干個(gè)空格被看做是一個(gè)空格 換行符 回車符 Tab符 StringTokenizer Strings Stringdelim 為字符串s構(gòu)造一個(gè)分析器 參數(shù)delim中的字符被作為分隔符 60 二 常用方法 StringnextToken 該方法逐個(gè)獲取字符串中的語言符號(hào) 單詞 booleanhasMoreTokens 該方法只要字符串中還有語言符號(hào) 就返回true 否則返回false intcountTokens 該方法得到字符串中語言符號(hào)的個(gè)數(shù) 61 importjava util publicclassExample5 11 publicstaticvoidmain Stringargs Strings wearestud ents StringTokenizerfenxi newStringTokenizer s intnumber fenxi countTokens while fenxi hasMoreTokens Stringstr fenxi nextToken System out println str System out println s共有單詞 number 個(gè) 輸出結(jié)果 wearestudentss共有單詞4個(gè) 62 5 9Math類 在編寫程序時(shí) 可能需要計(jì)算一個(gè)數(shù)的平方根 絕對(duì)值 隨機(jī)產(chǎn)生數(shù)據(jù)等 java lang類中的包含有許多用來進(jìn)行科學(xué)計(jì)算的類方法 這些方法可以直接通過類名調(diào)用 一 靜態(tài)常量finalstaticdoubleE 2 718282828459045finalstaticdoublePI 3 141592653589793 63 二 Math類的常用方法 64 二 Math類的常用方法 65 二 Math類的常用方法 有時(shí)需要對(duì)輸出的數(shù)字進(jìn)行格式化 例如 希望輸出的實(shí)型數(shù)據(jù)均有三位小數(shù) 可以使用java text包中的NumberFormat類 方法如下 NumberFormatf NumberFormat getInstance 實(shí)例化了一個(gè)NumberFormat對(duì)象 該對(duì)象可以調(diào)用方法 publicfinalStringformat doublenumber 對(duì)數(shù)字進(jìn)行格式化 注 format方法的參數(shù)可以是byte short int long float double類型 66 NumberFormat類的常用方法 publicvoidsetMaxinumFractionDigits intnewValue 最多有newValue位小數(shù) publicvoidsetMininumFractionDigits intnewValue 最少有newValue位小數(shù) publicvoidsetMaxinumIntegerDigits intnewValue 最多有newValue位整數(shù) publicvoidsetMaxinumIntegerDigits intnewValue 最少有newValue位整數(shù) 67 importjava text classExample5 16 publicstaticvoidmain Stringargs inta newint 4 5 inti j Strings NumberFormatf NumberFormat getInstance f setMinimumIntegerDigits 3 for i 0 i 4 i for j 0 j 5 j a i j i j 1 5 s f format a i j System out print s System out println 輸出 000000000000000005010015020025010020030040050015030045060075 68 classExample5 16 publicstaticvoidmain Stringargs inta newint 10 inti for i 0 ia j k a i a i a j a j k 產(chǎn)生10個(gè) 10 30 上的整數(shù) 對(duì)這些數(shù)從小到大排序 69 5 11vector類 java的包java util中的vector類負(fù)責(zé)創(chuàng)建一個(gè)向量對(duì)象 一 向量對(duì)象的創(chuàng)建 Vectorvector newVector 說明 向量對(duì)象不同于數(shù)組 向量對(duì)象不必指出大小 向量對(duì)象的元素不一定是同類型的 某一種類型的數(shù)據(jù)放入向量后 數(shù)據(jù)被默認(rèn)為Object對(duì)象 因此當(dāng)向量中取出一個(gè)元素時(shí)需使用強(qiáng)制類型轉(zhuǎn)換運(yùn)算符轉(zhuǎn)換為原來的類型 70 二 Vector類的常用方法 71 二 Vector類的常用方法 72 二 Vector類的常用方法 73 importjava util classExample5 17 publicstaticvoidmain Stringargs Vectorvector newVector inti number for i 1 i0 number int Math random vector size Integerinteger Integer vector elementAt number a i integer intValue vector removeElementAt number i for i 0 i 18 i System out print a i 74 5 12LinkedList類 可以用LinkedList類創(chuàng)建一個(gè)雙向鏈表 鏈表的節(jié)點(diǎn)可以是任何對(duì)象 一 對(duì)象的創(chuàng)建 LinkedListmylist newLinkedList mylist add it mylist add is mylist add a mylist add door 75 二 LinkedList類的常用方法 76 二 LinkedList類的常用方法 77 二 LinkedList類的常用方法 78 importjava util publicclassExample5 17 publicstaticvoidmain Stringargs LinkedListmylist newLinkedList mylist add newInteger 123 mylist add newDouble 4 56 mylist add 0 abd for inti 0 i mylist size i System out println mylist get i 鏈表中節(jié)點(diǎn)可以是任意類型的對(duì)象 必須是對(duì)象 不能是簡(jiǎn)單數(shù)據(jù)類型如 123是錯(cuò)的 79 importjava util publicclassExample5 18 publicstaticvoidmain Stringargs LinkedListmylist newLinkedList mylist add is mylist add a intnumber mylist size System out println 現(xiàn)在鏈表中有 number 個(gè)節(jié)點(diǎn) for inti 0 i number i Stringtemp String mylist get i System out println 第 i 節(jié)點(diǎn)中的數(shù)據(jù) temp mylist addFirst It mylist addLast door 80 number mylist size System out println 現(xiàn)在鏈表中有 number 個(gè)節(jié)點(diǎn) for inti 0 i number i Stringtemp String mylist get i System out println 第 i 節(jié)點(diǎn)中的數(shù)據(jù) temp mylist remove 0 mylist remove 1 mylist set 0 open number mylist size System out println 現(xiàn)在鏈表中有 number 個(gè)節(jié)點(diǎn) for inti 0 i number i Stringtemp String mylist get i System out println 第 i 節(jié)點(diǎn)中的數(shù)據(jù) temp 81 三 使用Iterator類遍歷鏈表 我們可以借助方法get進(jìn)行鏈表的遍歷 可以借助Iterator對(duì)象實(shí)現(xiàn)遍歷鏈表 一個(gè)鏈表對(duì)象可以使用iterator 方法獲取一個(gè)Iterator對(duì)象 使用next 方法遍歷鏈表 importjava util classStudent Stringname intnumber floatscore Student Stringname intnumber floatscore this name name this number number this score score 82 publicclassExample publicstaticvoidmain Stringargs LinkedListmylist newLinkedList Studentstu 1 newStudent 趙好民 9012 80 0f stu 2 newStudent 錢小青 9013 90 0f stu 3 newStudent 孫力枚 9014 78 0f stu 4 newStudent 周左右 9015 55 0f mylist add stu 1 mylist add stu 2 mylist add stu 3 mylist add stu 4 Iteratoriter mylist iterator while iter hasNext Studentte Student iter next System out println te name te number te score 83 5 13stack類 堆棧是一種先進(jìn)后出的數(shù)據(jù)結(jié)構(gòu) 數(shù)據(jù)的所有操作都是在棧頂進(jìn)行 一 創(chuàng)建對(duì)象 Stackstack newStack 84 二 Stack類的常用方法 85 importjava util classExample5 20 publicstaticvoidmain Stringargs Stackmystack newStack intk 1 whi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030擠壓零食行業(yè)發(fā)展趨勢(shì)分析與未來投資戰(zhàn)略咨詢研究報(bào)告
- 2025至2030珠寶電子商務(wù)行業(yè)項(xiàng)目調(diào)研及市場(chǎng)前景預(yù)測(cè)評(píng)估報(bào)告
- 《搶救車管理制度》考試試題及答案
- 高端酒店廠房轉(zhuǎn)租與酒店管理合作協(xié)議
- 特色餐廳品牌入駐綜合體租賃合同及經(jīng)營(yíng)支持
- 汽車產(chǎn)業(yè)變革:未來五年新能源汽車發(fā)展趨勢(shì)
- 2025至2030規(guī)模養(yǎng)鴨場(chǎng)行業(yè)運(yùn)營(yíng)態(tài)勢(shì)與投資前景調(diào)查研究報(bào)告
- 碳達(dá)峰與碳中和戰(zhàn)略下的儲(chǔ)能期貨交易前瞻
- 綠色能源產(chǎn)業(yè)AI智能調(diào)度實(shí)踐與挑戰(zhàn)
- 市政管網(wǎng)施工調(diào)度配合措施
- 2025年武漢市漢陽區(qū)社區(qū)干事崗位招聘考試筆試試題(含答案)
- 接警調(diào)度培訓(xùn)課件
- 2025屆山東煙臺(tái)中考?xì)v史真題試卷【含答案】
- 志愿者心理調(diào)適培訓(xùn)(改)
- 個(gè)人信息保護(hù)與安全培訓(xùn)
- 基于響應(yīng)面法的工藝參數(shù)優(yōu)化研究
- 抗美援朝紅色文化課件教學(xué)
- 2025年04月河北張家口市事業(yè)單位公開招聘筆試歷年典型考題(歷年真題考點(diǎn))解題思路附帶答案詳解
- 調(diào)價(jià)合同協(xié)議書怎么寫
- 征信修復(fù)服務(wù)合同
- 財(cái)務(wù)崗位廉政風(fēng)險(xiǎn)培訓(xùn)
評(píng)論
0/150
提交評(píng)論