




已閱讀5頁,還剩72頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第五章 常用實體類,5.1 String類,Java使用java.lang包中的String 類來創(chuàng)建一個字符串變量. 1.字符串常量 如:”你好” “12.3456” “SCHOOL” 2.聲明字符串 String s;,5.1 String類,3.創(chuàng)建字符串 使用String類的構(gòu)造方法 例如:s=new String(“ we are students”); 也可以用一個已經(jīng)創(chuàng)建的字符串去創(chuàng)建另一個字符串 如:String tom=String(s); 另外比較常用的構(gòu)造方法: String (char a):用一個字符數(shù)組a創(chuàng)建一個字符串對象,如: Char a=b , o , y; String s=new String(a); String(char a ,int startIndex , int count):提取字符數(shù)組a中的一部分字符創(chuàng)建一個字符串對象,參數(shù)startIndex和count分別指定在a中提取字符的起始位置和從該位置開始截取的字符個數(shù). 如:char a=s , t , b , u , s , n; String s=new String(a,2,3);,5.1 String類,4.引用字符串常量對象 字符串常量是對象,因此把字符串常量的引用賦值給一個字符串變量,則具有相同的實體. 如: String s1,s2; S1=“how are you”; S2=“how are you”;,0xAb28,0xAb28,How are you,5.1.5 String類的常用方法,1.public int length() 使用String 類的length()方法可以獲取一個字符串的長度, 如: String s=“We are students”,tom=“我們是學(xué)生”; int n1,n2; n1=s.length(); n2=s.length();,5.1.5 String類的常用方法,2.public boolean equals(String s) 使用String 類的equals方法,比較當(dāng)前字符串對象的實體是否與參數(shù)指定的字符串s的實體相同. 如: String tom=new String(“we are students”); String boy=new String(“We are students”); String jerry=new String(“we are students”); tom.equals(boy) 的值是false tom.equals(jerry)的值是true 字符串調(diào)用public boolean equalslgnoreCase(String s)比較當(dāng)前字符串對象與參數(shù)指定的字符串s是否相同,比較時忽略大小寫.,5.1.5 String類的常用方法,equals的用法 class Example5_1 public static void main(String args) String s1,s2; s1=new String(“we are students“); s2=new String(“we are students“); System.out.println(s1.equals(s2); /輸出結(jié)果是:true。 System.out.println(s1=s2); /輸出結(jié)果是:false String s3,s4; s3=“how are you“; s4=“how are you“; System.out.println(s3.equals(s4); /輸出結(jié)果是:true。 System.out.println(s3=s4); /輸出結(jié)果是:true。 ,5.1.5 String類的常用方法,3.public boolean startsWith(String s) 、 public boolean endsWith(String s) 方法 字符串對象調(diào)用startsWith(String s)方法,判斷當(dāng)前字符串對象的前綴是否是參數(shù)指定的字符串s. 如: String tom=“220302620629021”; String jerry=“21079670924022”; tom.startsWith(“220”) 的值是true; jerry.startsWith(“220”)的值是false. 使用endsWith(String s)方法,判斷一個字符串的后綴是否是字符串s,如: tom.endsWith(“021”) 的值是true; jerry.startsWith(“021”)的值是false.,5.1.5 String類的常用方法,4.public boolean regionMatches(int firstStart,String other,int ortherStart,int length) 方法 字符串對象調(diào)用regionMatches(int firstStart,String other,int ortherStart,int length) 方法,從當(dāng)前字符串參數(shù)firstStart指定的位置開始處,取長度為length的一個子串,并將這個子串和參數(shù)other指定的一個子串進行比較,其中,other指定的子串是從otherStart指定的位置開始,從other中取長度為length的一個子串。如果兩個子串相同該方法就返回true,否則返回false 使用該方法的重載方法, public boolean regMatches (boolean b,int firstStart, String other , int otherStart ,int length) 可以通過參數(shù)b決定是否忽略大小寫,當(dāng)b取true時,忽略大小寫。,5.1.5 String類的常用方法,/判斷一個字符串中出現(xiàn)了幾個en class Example5_2 public static void main(String args) int number=0; String s=“student;entropy;engage,english,client“; for(int k=0;ks.length();k+) if(s.regionMatches(k,“en“,0,2) number+; System.out.println(“number=“+number); ,5.1.5 String類的常用方法,5.public int compareTo(String s) 方法 字符串對象可以使用compareTo(String s) 方法,按照字典序與參數(shù)s指定的字符串比較大小。如果當(dāng)前的字符串與s,相同,該方法返回值是0;如果當(dāng)前字符串對象大于s,該方法返回正值;如果小于s, 該方法返回負值。 如: String str=“abcde”; SpareTo(“boy”)小于0;pareTo(“aba”)大于0;pareTo(“abcde”)等于0 按字典比較兩個字符串還可以使用 public int compareTolgnoreCase(String s)方法,該方法忽略大小寫,5.1.5 String類的常用方法,/字符串排序 class Example5_3 public static void main(String args) String a=“boy“,“apple“,“Applet“,“girl“,“Hat“; for(int i=0;ia.length-1;i+) for(int j=i+1;ja.length;j+) if(pareTo(ai)0) String temp=ai; ai=aj; aj=temp; for(int i=0;ia.length;i+) System.out.print(“ “+ai); ,5.1.5 String類的常用方法,6.public int indexOf(String s) 方法 字符串對象調(diào)用indexOf(String s) 方法,從當(dāng)前字符串的頭開始檢索字符串s,并返回首次出現(xiàn)s的位置。如果沒有檢索到字符串s,該方法返回的值是-1。 字符串調(diào)用indexOf(String s,int startpoint)方法從當(dāng)前字符串的startpoint位置處開始檢索字符串s,并返回首次出現(xiàn)s的位置。如果沒有檢索到字符串s,該方法返回的值是-1。 字符串調(diào)用lastindexOf(String s)方法從當(dāng)前字符串的頭開始檢索字符串s,并返回最后出現(xiàn)s的位置,如果沒有檢索到字符s,該方法返回的值是-1。 如: String tom=“I am a good cat”; tom.indexOf(“a”); /值是2 tom.indexOf(“good”,2); /值是7 tom.indexOf(“a”,7); /值是13 tom.indexOf(“w”,2); /值是-1,5.1.5 String類的常用方法,7.public String substring(int startpoint) 方法 字符串對象調(diào)用substring(int startpoint) 方法獲得一個當(dāng)前字符串的子串,該子串是從當(dāng)前字符串的startpoint處截取到最后所得到的字符串,字符串對象調(diào)用substring(int start,int end)方法獲得一個當(dāng)前字符串的子串,該子串從當(dāng)前字符串的start處截取到end處所得到的字符串,但不包括end處所對應(yīng)的字符。 如: String tom=“I love tom”; String s=tom.substring(2,5); /s值是lov,5.1.5 String類的常用方法,8.public String replaceAll(String old,String new) 方法 字符串對象s調(diào)用該方法可以獲得一個串對象,這個串對象是通過參數(shù)new指定的字符串替換s中的由old指定的所有字符串而得到的字符串。,5.1.5 String類的常用方法,9.public String trim() 方法 字符串s調(diào)用該方法可以得到一個字符串對象,該字符串是s去掉前后空格后的字符串。,5.1.6 字符串與基本數(shù)據(jù)類型的相互轉(zhuǎn)化,java.lang包中的Integer類調(diào)用其方法: public static int parseInt(String s) 可以將“數(shù)字”格式的字符串轉(zhuǎn)化為int型數(shù)據(jù); 如: int x; String s=“3452”; x=Integet.parseInt(s);,5.1.6 字符串與基本數(shù)據(jù)類型的相互轉(zhuǎn)化,java.lang包中的Byte、Short、Long、Float、Double類的數(shù)據(jù)類型轉(zhuǎn)化方法: public static byte parseByte(String s) public static short parseShort(String s) public static long parseLong(String s) public static float parseFloat(String s) public static double parseDouble(String s),5.1.6 字符串與基本數(shù)據(jù)類型的相互轉(zhuǎn)化,/從鍵盤輸入若干個數(shù),求平均值 public class Example5_4 public static void main(String args) double n,sum=0.0 ; for(int i=0;iargs.length;i+) sum=sum+Double.parseDouble(argsi); n=sum/args.length; System.out.println(“平均數(shù):“+n); /*編譯后,使用控制臺輸入數(shù)值 *java Example5_4 “20” “67.05” “12.66” “20.1” */,5.1.6 字符串與基本數(shù)據(jù)類型的相互轉(zhuǎn)化,String 類方法將數(shù)值轉(zhuǎn)化為字符串 public String valueOf(byte n) public String valueOf(int n) public String valueOf(long n) public String valueOf(float n) public String valueOf(double n) 如:String str=String.valueOf(123.5598); float x=123.562f; String temp=String.valueOf(x);,5.1.7 對象的字符串表示,所有類都默認是java.lang包中Object類的子類或間接子類。Object類有一個public方法toString(), 一個對象通過調(diào)用該方法可以獲得該對象的字符串表示。,5.1.7 對象的字符串表示,例子5: import java.util.Date; import java.awt.*; public class Example5_5 public static void main(String args) Date date=new Date(); Button button=new Button(“確定“); System.out.println(date.toString(); System.out.println(button.toString(); ,5.1.8 字符串與字符、字節(jié)數(shù)組,1.字符串與字符數(shù)組: 用數(shù)組創(chuàng)建字符串對象,用String類的構(gòu)造方法:String(char a) 和String(char a , int offset ,int length) String類也提供將字符串存放到數(shù)組中的方法: Public void getChars(int start, int end , char c ,int offset) 字符串調(diào)用getChars方法將當(dāng)前字符串中的一部分字符復(fù)制到參數(shù)c指定的數(shù)組中,將字符串中從位置start到end-1位置上的字符復(fù)制到數(shù)組c中,并從數(shù)組c的offset處開始存放這些字符。注意必須保證c能容納下要被復(fù)制的字符。,5.1.8 字符串與字符、字節(jié)數(shù)組,class Example5_6 public static void main(String args) char c,d; String s=”巴西足球隊擊敗德國足球隊”; c=new char2; s.getChars(5,7,c,0); System.out.println(c); d=new chars.length(); s.getChars(7,12,d,0); s.getChars(5,7,d,5); s.getChars(0,5,d,7); System.out.println(d); ,5.1.8 字符串與字符、字節(jié)數(shù)組,1.字符串與字符數(shù)組: String類還提供一個方法: public char toCharArray() 字符串對象調(diào)用該方法可以初始化一個字符串?dāng)?shù)組,該數(shù)組的長度與字符串的長度相等,并將字符串對象的全部字符復(fù)制到該數(shù)組中。,5.1.8 字符串與字符、字節(jié)數(shù)組,class Example5_7 public static void main(String args) String s=“列車時刻表“; char a=s.toCharArray(); for(int i=0;ia.length;i+) ai=(char)(ait); String secret=new String(a); System.out.println(“密文:“+secret); for(int i=0;ia.length;i+) ai=(char)(ait); String code=new String(a); System.out.println(“原文:“+code); ,5.1.8 字符串與字符、字節(jié)數(shù)組,2.字符串與字節(jié)數(shù)組: String類的構(gòu)造方法String(byte)用指定的字節(jié)數(shù)組構(gòu)造一個字符串對象。 String(byte, int offset ,int length)構(gòu)造方法用指定的字節(jié)數(shù)組的一部分,即從數(shù)組起始位置offset開始取 length個字節(jié)構(gòu)造一個字符串對象。 public byte getBytes()方法使用平臺默認的字符編碼,將當(dāng)前字符串轉(zhuǎn)化為一個字節(jié)數(shù)組。,5.1.8 字符串與字符、字節(jié)數(shù)組,public class Example5_8 public static void main(String args) byte d=“你我他“.getBytes(); System.out.println(“數(shù)組d的長度是(一個漢字占兩個字節(jié)):“+d.length); String s=new String(d,0,2); System.out.println(s); ,5.2 StringBuffer類,String類創(chuàng)建的字符串對象是不可修改的,String對象一旦創(chuàng)建,那么實體是不可以再發(fā)生變化的。 如: String s=new String(“I love this game”);,0x12ABC,I love this game,不可以再發(fā)生變化,對象,實體,5.2 StringBuffer類,StringBuffer類創(chuàng)建的字符串對象可修改,該類對象的實體內(nèi)存空間可以自動地改變大小,便于存放一個可變的字符串。如: StringBuffer s=new StringBuffer(“I love this game”); s.append(“ ok”);,0x12ABC,I love this game ok,可以再發(fā)生變化,對象,實體,5.2 StringBuffer類,1.StringBuffer類的構(gòu)造方法: StringBuffer() StringBuffer(int size) StringBuffer(String s),5.2 StringBuffer類,1.StringBuffer類的構(gòu)造方法: StringBuffer() 無參數(shù)的構(gòu)造方法創(chuàng)建StringBuffer對象,分配給該對象的實體的初始容量可以容納16個字符,當(dāng)該對象的實體存放的字符序列長度大于16時候,實體自動增加。 StringBuffer對象可以通過length()方法獲取實體中存放的字符序列長度,通過capacity()方法獲取當(dāng)前實體的實際容量。,5.2 StringBuffer類,1.StringBuffer類的構(gòu)造方法: StringBuffer(int size) 用此構(gòu)造方法創(chuàng)建一個StringBuffer對象,可指定分配給該對象的實體的初始容量為參數(shù)size指定的字符個數(shù),當(dāng)對象的實體存放的字符序列的長度大于size時,實體的容量自動增加。,5.2 StringBuffer類,1.StringBuffer類的構(gòu)造方法: StringBuffer(String s) 用此構(gòu)造方法創(chuàng)建一個StringBuffer對象,可以指定分配給該對象的實體的初始容量為參數(shù)字符串s的長度再加16個字符。,5.2 StringBuffer類,class Example5_9 public static void main(String args) StringBuffer str=new StringBuffer(); str.append(“大家好“); System.out.println(“str:“+str); System.out.println(“l(fā)ength:“+str.length(); System.out.println(“capacity:“+str.capacity(); str.append(“我們大家都很愿意學(xué)習(xí)Java語言“); System.out.println(“str:“+str); System.out.println(“l(fā)ength:“+str.length(); System.out.println(“capacity:“+str.capacity(); StringBuffer sb=new StringBuffer(“Hello“); System.out.println(“l(fā)ength:“+sb.length(); System.out.println(“capacity:“+sb.capacity(); ,5.2 StringBuffer類,2.StringBuffer類的常用方法: append方法 使用StringBuffer類的append方法可以將其他Java類型數(shù)據(jù)轉(zhuǎn)化為字符串后,再追加到StringBuffer對象中。 StringBuffer append(String s):將一個字符串對象追加到當(dāng)前StringBuffer對象中,并返回當(dāng)前StringBuffer對象的引用。 StringBuffer append(int n):將一個int類型數(shù)據(jù)轉(zhuǎn)化為字符串對象后再追加到當(dāng)前StringBuffer對象中,并返回StringBuffer對象的引用。 StringBuffer append(Object o):將一個Object對象的字符串表示追加到當(dāng)前的StringBuffer對象中,并返回當(dāng)前StringBuffer對象的引用。 StringBuffer append(long n) StringBuffer append(boolean n) StringBuffer append(float n),5.2 StringBuffer類,2.StringBuffer類的常用方法: public char charAt(int n) 和 public void setCharAt(int n,char ch) char charAt(int n):得到參數(shù)n指定位置上的單個字符 setCharAt(int n,char ch):將當(dāng)前StringBuffer對象實體中的字符串位置n處的字符用參數(shù)ch制定的字符替換。,5.2 StringBuffer類,2.StringBuffer類的常用方法: StringBuffer insert(int index,String str): insert方法將一個字符串插入到另一個字符串中,并返回當(dāng)前對象的引用。 public StringBuffer reverse() 該方法將對象實體中的字符翻轉(zhuǎn),并返回當(dāng)前對象的引用。 StringBuffer delete(int startIndex,int endIndex) 從當(dāng)前StringBuffer對象實體的字符串中刪除一個子字符串,并返回當(dāng)前對象的應(yīng)用。,5.2 StringBuffer類,2.StringBuffer類的常用方法: StringBuffer replace(int startIndex,int endIndex,String str) 該方法將當(dāng)前StringBuffer對象實體中的字符串的一個字字符串用參數(shù)str指定的字符串替換。,5.2 StringBuffer類,例子10 class Example5_10 public static void main(String args) StringBuffer str=new StringBuffer(“我們大家都很愿意學(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); int index=str.indexOf(“都“); str.replace(index,str.length(),“ love java“); System.out.println(str); ,5.3 StringTokenizer類,java.util包中的StringTokenizer類可以分析一個字符串并將字符串分解成可以被獨立使用的單詞。 兩個構(gòu)造方法: StringTokenizer(String s):為字符串s構(gòu)造一個分析器,使用默認的分割符集合,即空格符、換行符、回車符、Tab符、進紙符等 StringTokenizer(String s,String delim):為字符串s構(gòu)造一個分析器。參數(shù)dilim中的字符被作為分割符。,5.3 StringTokenizer類,nextToken()方法:可以逐個獲取字符串中的單詞。 hasMoreTokens()方法:字符串有語言符號,該方法返回true countTokens():得到字符串一共有多少個單詞。,5.3 StringTokenizer類,例子11: import java.util.*; public class Example5_11 public static void main(String args) String s=“we are stud,ents“; StringTokenizer fenxi=new StringTokenizer(s,“ ,“); /空格和逗號做分 int number=fenxi.countTokens(); while(fenxi.hasMoreTokens() String str=fenxi.nextToken(); System.out.println(str); System.out.println(“還?!?fenxi.countTokens()+“個單詞“); System.out.println(“s共有單詞:“+number+“個“); ,5.4 Character類,常用方法: public static boolean isDigit(char ch)如果ch是數(shù)字字符方法返回true public static boolean isLetter(char ch)如果ch 是字母方法返回true public static boolean isLowerCase(char ch)如果ch是小寫字母方法返回true public static boolean isUpperCase(char ch)如果ch是大寫字母方法返回true public static char toLowerCase(char ch)返回ch的小寫形式 public static char toUpperCase(char ch)返回ch的大寫形式 public static boolean isSpaceChar(char ch)如果ch是空格返回true,5.4 Character類,例子12: import java.util.*; public class Example5_12 public static void main(String args) String s=new String(“abcABC123“); System.out.println(s); char a=s.toCharArray(); for(int i=0;ia.length;i+) if(Character.isLowerCase(ai) ai=Character.toUpperCase(ai); else if(Character.isUpperCase(ai) ai=Character.toLowerCase(ai); s=new String(a); System.out.println(s); ,5.5 Date類,Date類在java.util包中。使用Date類的無參數(shù)構(gòu)造方法創(chuàng)建的對象可以獲取本地當(dāng)前時間。 用Date的構(gòu)造方法Date(long time)創(chuàng)建的Date對象表示相對1970年1月1日0點(GMT)的時間。 獲取系統(tǒng)時間: System類的靜態(tài)方法 public long currentTimeMills() 用此方法獲取的時間是從1970年1月1日0點(GMT)到目前時刻所走過的毫秒數(shù)。 Date 對象表示時間的默認順序是:星期、月、日、小時、分、秒、年,5.5 Date類,格式化日期: DateFormat的子類SimpleDateFormat實現(xiàn)日期的格式化,采用SimpleDateFormat的常用構(gòu)造方法: public SimpleDateFormat(String pattern) 用該構(gòu)造方法創(chuàng)建的對象調(diào)用format(Date date)方法格式化時間對象date。注:pattern中應(yīng)當(dāng)還有一些有效的字符序列,如下: y或yy 表示用2位數(shù)字輸出年份;yyyy表示用4位數(shù)字輸出年份 M或MM表示用2位數(shù)字或文本輸出月份,如果想用漢字輸出月份,pattern中應(yīng)連續(xù)包含至少3個M,如:MMM。 d或dd 表示用2位數(shù)字輸出日 H或HH 表示用2位數(shù)字輸出小時 m或mm 表示用2位數(shù)字輸出分 s或ss 表示用2位數(shù)字輸出秒 E 表示用字符串輸出星期,5.5 Date類,import java.util.Date; import java.text.SimpleDateFormat; class Example5_13 public static void main(String args) Date nowTime=new Date(); System.out.println(“現(xiàn)在的時間:“+nowTime); SimpleDateFormat matter1=new SimpleDateFormat(“yyyy年MM月dd日 北京時間“); System.out.println(“現(xiàn)在的時間:“+matter1.format(nowTime); SimpleDateFormat matter2= new SimpleDateFormat(“yyyy年MM月Edd日HH時mm分ss秒 北京時間“); System.out.println(“現(xiàn)在的時間:“+matter2.format(nowTime); SimpleDateFormat matter3= new SimpleDateFormat(“北京時間dd日HH時MMM ss秒mm分EE“); System.out.println(“現(xiàn)在的時間:“+matter3.format(nowTime); long time=-1800; Date date=new Date(time); System.out.println(“-1800秒表示的日期時間是:“+date); ,5.6 Calendar類,Calendar類在java.util包中。 使用Calendar類的static方法getInstance()可初始化一個日歷對象。 如:Calendar calendar=Calendar.getInstance(); 可以調(diào)用以下方法,可以將日歷翻到任何一個時間: public final void set (int year,int month, int date) public final void set(int year,int month , int date,int hour,int minute) public final void set(int year,int month,int date, int hour, int minute, int second) 當(dāng)參數(shù)year取負數(shù)時候表示公元前。,5.6 Calendar類,調(diào)用方法public int get(int field)可以獲得有關(guān)年份、月份、小時、星期等信息,參數(shù)field的有效值由Calendar的靜態(tài)常量指定。 例如:calendar.get(Calendar.MONTH); 返回一個整數(shù),如果整數(shù)是0表示當(dāng)前是在一月,整數(shù)是1表示當(dāng)前日歷是在二月。 調(diào)用方法public int getTimeInMillis()可以將時間表示為毫秒。,5.6 Calendar類,import java.util.*; class Example5_14 public static void main(String args) Calendar calendar=Calendar.getInstance(); /創(chuàng)建一個日歷對象。 calendar.setTime(new Date(); /用當(dāng)前時間初始化日歷時間。 String 年=String.valueOf(calendar.get(Calendar.YEAR), 月=String.valueOf(calendar.get(Calendar.MONTH)+1), 日=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH), 星期=String.valueOf(calendar.get(Calendar.DAY_OF_WEEK)-1); int hour=calendar.get(Calendar.HOUR_OF_DAY), minute=calendar.get(Calendar.MINUTE), second=calendar.get(Calendar.SECOND); System.out.println(“現(xiàn)在的時間是:“); System.out.println(“+年+“年“+月+“月“+日+“日 “+ “星期“+星期); System.out.println(“+hour+“時“+minute+“分“+second+“秒“); calendar.set(1962,5,29); /將日歷翻到1962年6月29日,注意5表示六月。 long time1962=calendar.getTimeInMillis(); calendar.set(2003,9,5); /將日歷翻到2003年10月5日。9表示十月。 long time2003=calendar.getTimeInMillis(); long 相隔天數(shù)=(time2003-time1962)/(1000*60*60*24); System.out.println(“2003年10月5日和1962年6月29日相隔“+相隔天數(shù)+“天“); ,5.6 Calendar類,import java.util.*; class Example5_15 public static void main(String args) System.out.println(“ 日 一 二 三 四 五 六“); Calendar 日歷=Calendar.getInstance(); 日歷.set(2004,9,1); /將日歷翻到2004年10月1日,注意0表示一月。 /獲取1日是星期幾(get方法返回的值是1表示星期日,星期六返回的值是7): int 星期幾=日歷.get(Calendar.DAY_OF_WEEK)-1; String a=new String星期幾+31; /存放號碼的一維數(shù)組 for(int i=0;i星期幾;i+) ai=“*“; for(int i=星期幾,n=1;i星期幾+31;i+) if(n=9) ai=String.valueOf(n)+“ “; else ai=String.valueOf(n) ; n+; /打印數(shù)組: for(int i=0;ia.length;i+) if(i%7=0) System.out.println(“); /換行。 System.out.print(“ “+ai); ,5.7 Math類,Math類包含在Java.lang包中,它包含很多用來進行科學(xué)計算的類方法,這些方法可以直接通過類名調(diào)用。 Math類的兩個靜態(tài)常量,E 和PI, E=2.7182828284590452354 PI=3.14159265358979323846,5.7 Math類,Math類的常用方法: public staic long abs(double a) 返回a的絕對值 public staic double max(double a,double b) 返回a、b的最大值 public staic double min(double a,double b)返回a、b的最小值 public staic double random()產(chǎn)生一個01之間的隨機數(shù)(不包括0和1) public staic double pow(double a,double b)返回a的b次冪 public staic double sqrt(double a)返回a的平方根 public staic double log(double a)返回a的對數(shù) public staic double sin(double a)返回a的正弦值 public staic double asin(double a)返回a的反正弦值,5.7 Math類,NumberFormat類的方法 格式化顯示數(shù)字 常用方法 public final String format(double number) public void setMaximunFractionDigits(int newValue) public void setMinimunFractionDigits(int newValue) public void setMaximumIntegerDigits(int newValue) public void setMinimumIntegerDigits(int newValue),5.7 Math類,例子16:,5.8 Vector類,java.util包中的Vector類負責(zé)創(chuàng)建一個向量對象。向量創(chuàng)建時,不用給出大小。向量大小自動增加。 創(chuàng)建方法: Vector a=new Vector(); 當(dāng)把某一種類型的對象放入一個向量后,數(shù)據(jù)被默認為是Object對象。,5.8 Vector類,Vector類常用的方法: public void add(Object o)將對象o添加到向量的末尾 public void add(int index,Object o)將對象o插入到向量的指定位置 public void addElements(Object o)將對象o添加到向量的末尾 public boolean contains(Object o)判斷對象o是否為向量的成員 public Object elementAt(int index)獲取指定位置處的成員,5.8 Vector類,public Object get(int index)獲取向量指定位置處的成員 public Object firstElement()獲取此向量的第一個成員 public Object lastElement()獲取此向量的最后一個成員 public int indexOf(Object o)獲取對象在此向量中首次出現(xiàn)的位置 public int indexOf(Object o,int index)從指定位置查找對象o在此向量中首次出現(xiàn)的位置 public int lastIndexOf(Object o)獲取對象o在此向量中最后出現(xiàn)的位置 public int lastIndexOf(Object o,int index)對象o在此向量位置index之前最后出現(xiàn)的位置。,5.8 Vector類,public Object remove(int index)從此向量中刪除指定位置處的成員,并返回這個成員 public void removeAllElements()刪除向量的所有成員 public boolean removeElement(Object o)刪除第一次出現(xiàn)的成員o public boolean removeElement(int index)刪除指定位置處的成員 public void set (int index,Object o)把指定位置處的成員用o替換掉 public void setElementAt(Object o,int index)把指定位置處的成員用o替換掉 public Enumeration elements()獲取一個枚舉對象。,5.8 Vector類,例子17: import java.util.*; class Example5_17 public static void main(String args) Vector vector=new Vector(); for(int i=1;i0) int number=(int)(Math.random()*vector.size(); Integer integer=(Integer)vector.elementAt(number); ai=Value(); /得到整數(shù)對象中的int數(shù). vector.removeElementAt(number); /向量移掉number處的整數(shù)對象. i+; for(i=0;i18;i+) System.out.print(“ “+ai); ,5.9 LinkedList類,鏈表是由若干個稱做節(jié)點的對象組成的一種數(shù)據(jù)結(jié)構(gòu),每個節(jié)點都含有一個數(shù)據(jù)和下一個節(jié)點對象的引用(單鏈表),或含有一個數(shù)據(jù)并含有上一個節(jié)點對象的引用和下一個節(jié)點對象的引用(雙鏈表),節(jié)點的索引從0開始。,5.9 LinkedList類,創(chuàng)建鏈表 使用java.until包中的LinkedList類創(chuàng)建一個鏈表。 如: LinkedList mylist=new LinkedList(); 創(chuàng)建了一個空鏈表 然后添加節(jié)點,用add()方法
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 外科護士長個人述職報告范文
- 2025年幼兒園疫病信息報告計劃
- 抖音短視頻新媒體運營職責(zé)
- 零成本智能硬件營銷方案范文
- 以市場機制為翼鑄博物館核心產(chǎn)品之魂
- 以實驗探究為翼展初中生物創(chuàng)新之翔:創(chuàng)新能力培養(yǎng)實踐與探索
- 醫(yī)療行業(yè)一體機培訓(xùn)心得體會
- 壓瘡護理流程優(yōu)化小組職責(zé)
- 專升本學(xué)科交叉學(xué)習(xí)心得體會
- 2025年遼寧、吉林、黑龍江、內(nèi)蒙古四省高考物理真題(含答案)
- DB4201∕T 694-2024 押運行業(yè)安全生產(chǎn)標(biāo)準(zhǔn)化基本規(guī)范
- 2024年鹽城市大豐區(qū)事業(yè)單位招聘考試真題
- 2025年天津市中考語文試卷(含標(biāo)準(zhǔn)答案)
- 2025年6月浙江省高考技術(shù)試卷真題
- 2025屆上海市高考英語考綱詞匯表
- 2024年山西煙草專賣局考試真題試卷及答案
- 四川省2024普通高校招生本科一批調(diào)檔線(理科)
- 2024年秋兒童發(fā)展問題的咨詢與輔導(dǎo)終考期末大作業(yè)案例分析1-5答案
- 普通高校招生考生志愿表模板
- 意向表(標(biāo)準(zhǔn)模版)
評論
0/150
提交評論