亚洲情人网-亚洲情人-亚洲欧洲综合-亚洲欧洲自拍-欧美专区亚洲

字符串是什么意思(wa字符串是什么意思)

  • 生活
  • 2023-04-19 15:38
9.1裝箱拆箱封裝類

所有的基本類型,都有對應的類類型。比如int對應的類是Integer,這種類就叫做封裝類。

packagedigit;publicclassTestNumber{publicstaticvoidmain(String[]args){inti=5;//把一個基本類型的變量,轉換為Integer類型Integerit=newInteger(i);//把一個Integer對象,轉換為一個基本類型的intinti2=it.intValue();}}Number類

數字封裝類有Byte,Short,Integer,Long,Float,Double。這些類都是抽象類Number的子類。

自動裝箱與拆箱

不需要調用構造***,通過**=**符號自動把基本類型轉換為類類型就叫裝箱

不需要調用Integer的intValue***,通過**=就自動轉換成int類型**,就叫拆箱

packagedigit;publicclassTestNumber{publicstaticvoidmain(String[]args){inti=5;//基本類型轉換成封裝類型Integerit=newInteger(i);//自動轉換就叫裝箱Integerit2=i;//封裝類型轉換成基本類型inti2=it.intValue();//自動轉換就叫拆箱inti3=it;}}int的最大值和最小值

int的最大值可以通過其對應的封裝類Integer.MAX_VALUE獲取

packagedigit;publicclassTestNumber{publicstaticvoidmain(String[]args){//int的最大值System.out.println(Integer.MAX_VALUE);//int的最小值System.out.println(Integer.MIN_VALUE);}

9.2字符串轉換

數字轉成字符串

***1:使用String類的靜態***valueOf***2:先把基本類型裝箱為對象,然后調用對象的toString

packagedigit;publicclassTestNumber{publicstaticvoidmain(String[]args){inti=5;//***1Stringstr=String.valueOf(i);//***2Integerit=i;Stringstr2=it.toString();}}字符串轉成數字

調用Integer的靜態***parseInt

packagedigit;publicclassTestNumber{publicstaticvoidmain(String[]args){Stringstr="999";inti=Integer.parseInt(str);System.out.println(i);}}練習

把浮點數3.14轉換為字符串"3.14"再把字符串“3.14”轉換為浮點數3.14

如果字符串是3.1a4,轉換為浮點數會得到什么?

publicclassTestNumber{publicstaticvoidmain(String[]args){doublei=3.14;Stringstr=String.valueOf(i);System.out.println("3.14轉換成的字符串"+str);doublei2=Float.parseFloat(str);System.out.println(""3.14"轉換成的浮點數"+i2);Stringstr2="3.1a4";doublei3=Float.parseFloat(str2);System.out.println(""3.1s4"轉換成的浮點數"+i3);}}

9.3數學***

四舍五入,隨機數,開方,次方,π,自然常數publicclassTestNumber{publicstaticvoidmain(String[]args){floatf1=5.4f;floatf2=5.5f;//5.4四舍五入即5System.out.println(Math.round(f1));//5.5四舍五入即6System.out.println(Math.round(f2));//得到一個0-1之間的隨機浮點數(取不到1)System.out.println(Math.random());//得到一個0-10之間的隨機整數(取不到10)System.out.println((int)(Math.random()*10));//開方System.out.println(Math.sqrt(9));//次方(2的4次方)System.out.println(Math.pow(2,4));//πSystem.out.println(Math.PI);//自然常數System.out.println(Math.E);}}練習-數學***publicclassTestNumber{publicstaticvoidmain(String[]args){intn=Integer.MAX_VALUE;doublevalue=Math.pow((1+1/(double)n),n);System.out.println("n取最大整數時value:"+value);doubleerror=Math.abs(Math.E-value);System.out.println("error="+error);}}練習-質數

統計找出一千萬以內,一共有多少質數。

質數概念:只能被1和自己整除的數。

publicclassTestNumber{publicstaticbooleanIsZ(inti){for(intj=2;j<=Math.sqrt(i);j++){if(i%j==0){returnfalse;}}returntrue;}publicstaticvoidmain(String[]args){intmax_n=10000000;intcount=0;for(inti=2;i<max_n;i++){if(IsZ(i)){count++;}}System.out.println("一千萬以內的質數共有"+count);}}

9.4格式化輸出

格式化輸出

如果不使用格式化輸出,就需要進行字符串連接,如果變量比較多,拼接就會顯得繁瑣使用格式化輸出,就可以簡潔明了。

%s表示字符串%d表示數字%n表示換行

publicclassTestNumber{publicstaticvoidmain(String[]args){Stringname="蓋倫";intkill=8;Stringtitle="超神";//直接使用+進行字符串連接,編碼感覺會比較繁瑣,并且維護性差,易讀性差Stringsentence=name+"在進行了連續"+kill+"次擊殺后,獲得了"+title+"的稱號";System.out.println(sentence);//使用格式化輸出//%s表示字符串,%d表示數字,%n表示換行StringsentenceFormat="%s在進行了連續%d次擊殺后,獲得了%s的稱號%n";System.out.printf(sentenceFormat,name,kill,title);//使用format格式化輸出System.out.format(sentenceFormat,name,kill,title);}}總長度,左對齊,補0,千位分隔符,小數點位數,本地化表達packagedigit;importjava.util.Locale;publicclassTestNumber{publicstaticvoidmain(String[]args){intyear=2020;//總長度,左對齊,補0,千位分隔符,小數點位數,本地化表達//直接打印數字System.out.format("%d%n",year);//總長度是8,默認右對齊System.out.format("%8d%n",year);//總長度是8,左對齊System.out.format("%-8d%n",year);//總長度是8,不夠補0System.out.format("%08d%n",year);//千位分隔符System.out.format("%,8d%n",year*10000);//小數點位數System.out.format("%.2f%n",Math.PI);//不同國家的千位分隔符System.out.format(Locale.FRANCE,"%,.2f%n",Math.PI*10000);System.out.format(Locale.US,"%,.2f%n",Math.PI*10000);System.out.format(Locale.UK,"%,.2f%n",Math.PI*10000);}}

9.5字符char

保存一個字符的時候使用charchar對應的封裝類是CharacterCharacter常見***publicclassTestChar{publicstaticvoidmain(String[]args){System.out.println(Character.isLetter('a'));//判斷是否為字母System.out.println(Character.isDigit('a'));//判斷是否為數字System.out.println(Character.isWhitespace(''));//是否是空白System.out.println(Character.isUpperCase('a'));//是否是大寫System.out.println(Character.isLowerCase('a'));//是否是小寫System.out.println(Character.toUpperCase('a'));//轉換為大寫System.out.println(Character.toLowerCase('A'));//轉換為小寫Stringa='a';//不能夠直接把一個字符轉換成字符串Stringa2=Character.toString('a');//轉換為字符串}}常見轉義publicclassTestChar{publicstaticvoidmain(String[]args){System.out.println("使用空格無法達到對齊的效果");System.out.println("abcdef");System.out.println("abdef");System.out.println("adef");System.out.println("使用\t制表符可以達到對齊的效果");System.out.println("abc def");System.out.println("ab def");System.out.println("a def");System.out.println("一個\t制表符長度是8");System.out.println("換行符abc def");System.out.println("單引號abc'def");System.out.println("雙引號abc"def");System.out.println("反斜杠abc\def");}}練習-Character

通過Scanner從控制臺讀取字符串,然后把字符串轉換為字符數組。

轉換為字符數組后,篩選出控制臺讀取到的字符串中的大寫字母和數字,并打印出來。

publicclassTestNumber{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);Stringstr=sc.next();char[]cs=str.toCharArray();System.out.println(String.valueOf(cs));System.out.println(Arrays.toString(cs));Stringstr2="";for(inti=0;i<str.length();i++){if(Character.isDigit(cs[i])||Character.isUpperCase(cs[i]))str2+=cs[i];}System.out.println(str2);}}

9.6字符串*

創建字符串

字符串即字符的組合,在Java中,字符串是一個類,所以我們見到的字符串都是對象常見創建字符串手段:

每當有一個字面值出現的時候,虛擬機就會創建一個字符串。調用String的構造***創建一個字符串對象。通過+加號進行字符串拼接也會創建新的字符串對象。publicclassTestString{publicstaticvoidmain(String[]args){Stringgaren="蓋倫";//字面值,虛擬機碰到字面值就會創建一個字符串對象Stringteemo=newString("提莫");//創建了兩個字符串對象char[]cs=newchar[]{'崔','斯','特'};Stringhero=newString(cs);//通過字符數組創建一個字符串對象Stringhero3=garen+teemo;//通過+加號進行字符串拼接}}String被修飾為final,所以是不能被繼承的/*這里會報錯,因為String不能被繼承*/staticclassMyStringextendsString{}immutable-不可改變的

比如創建了一個字符串對象Stringgaren="蓋倫";

不可改變的具體含義是指:

不能增加長度不能減少長度不能插入字符不能刪除字符不能修改字符

一旦創建好這個字符串,里面的內容永遠不能改變,String的表現就像是一個常量。

字符串格式化//%s表示字符串,%d表示數字,%n表示換行StringsentenceFormat="%s在進行了連續%d次擊殺后,獲得了%s的稱號%n";Stringsentence2=String.format(sentenceFormat,name,kill,title);練習1-隨機字符串

創建一個長度是5的隨機字符串,隨機字符有可能是數字,大寫字母或者小寫字母。

publicclassTestNumber{publiccharget_char(){/*0-9correspondstoAscii48-57*A-Z65-90*a-z97-122**/Randomrandom=newRandom();//隨機生成122-48范圍類的ascii碼intnum;switch(random.nextInt(3)){//獲取數字asciicase0:num=random.nextInt(58-48)+48;break;//獲取大寫字母case1:num=random.nextInt(91-65)+65;break;case2:num=random.nextInt(123-97)+97;break;default:num=48;}charc=(char)num;returnc;}publicStringget_String(intlength){Stringstr="";for(inti=0;i<length;i++){str+=get_char();}returnstr;}publicstaticvoidmain(String[]args){TestNumbert=newTestNumber();Stringstr=t.get_String(5);System.out.println(str);}}練習2-字符串數組排序publicString[]stringDataGroup_sort(String[]strings){/*字符串數組排序按首字母排序**/for(inti=0;i<strings.length-1;i++){for(intj=i+1;j<strings.length;j++){charstr1=strings[i].charAt(0);charstr2=strings[j].charAt(0);intnum1=str1;intnum2=str2;if(num1>num2){Stringstring=strings[i];strings[i]=strings[j];strings[j]=string;}}}returnstrings;}publicstaticvoidmain(String[]args){TestNumbert=newTestNumber();String[]strs=newString[8];for(inti=0;i<8;i++){strs[i]=t.get_String(5);}System.out.println(Arrays.toString(strs));String[]sorted_str=t.stringDataGroup_sort(strs);System.out.println(Arrays.toString(sorted_str));}

//java學習交流:737251827進入可領取學習資源及對十年開發經驗大佬提問,免費解答!

練習3-窮舉法破解密碼生成一個長度是3的隨機字符串,把這個字符串作為當做密碼使用窮舉法生成長度是3個字符串,匹配上述生成的密碼

要求:使用遞歸解決上述問題

9.7操縱字符串

獲取字符charAt(intindex)獲取指定位置的字符

Stringsentence="蓋倫,在進行了連續8次擊殺后,獲得了超神的稱號";charc=sentence.charAt(0);復制代碼

獲取對應的字符數組toCharArray()Stringsentence="蓋倫,在進行了連續8次擊殺后,獲得了超神的稱號";char[]cs=sentence.toCharArray();//獲取對應的字符數組截取子字符串subStringStringsentence="蓋倫,在進行了連續8次擊殺后,獲得了超神的稱號";//截取從第3個開始的字符串(基0)//到5-1的位置的字符串//左閉右開StringsubString2=sentence.substring(3,5);System.out.println(subString2);split根據分隔符進行分隔Stringsentence="蓋倫,在進行了連續8次擊殺后,獲得了超神的稱號";//根據,進行分割,得到3個子字符串StringsubSentences[]=sentence.split(",");for(Stringsub:subSentences){System.out.println(sub);}trim去掉首位空格Stringsentence="蓋倫,在進行了連續8次擊殺后,獲得了超神的稱號";System.out.println(sentence);//去掉首尾空格System.out.println(sentence.trim());大小寫

toLowerCase全部變成小寫;toUpperCase全部變成大寫。

Stringsentence="Garen";//全部變成小寫System.out.println(sentence.toLowerCase());//全部變成大寫System.out.println(sentence.toUpperCase());定位

indexOf判斷字符或者子字符串出現的位置contains是否包含子字符串

Stringsentence="蓋倫,在進行了連續8次擊殺后,獲得了超神的稱號";System.out.println(sentence.indexOf('8'));//字符第一次出現的位置System.out.println(sentence.indexOf("超神"));//字符串第一次出現的位置System.out.println(sentence.lastIndexOf("了"));//字符串最后出現的位置System.out.println(sentence.indexOf(',',5));//從位置5開始,出現的第一次,的位置System.out.println(sentence.contains("擊殺"));//是否包含字符串"擊殺"替換

replaceAll替換所有的;replaceFirst只替換第一個。

Stringsentence="蓋倫,在進行了連續8次擊殺后,獲得了超神的稱號";Stringtemp=sentence.replaceAll("擊殺","被擊殺");//替換所有的temp=temp.replaceAll("超神","超鬼");System.out.println(temp);temp=sentence.replaceFirst(",","");//只替換第一個System.out.println(temp);練習1-每個單詞的首字母都轉換為大寫

給出一句英文句子:"lettherebelight"得到一個新的字符串,每個單詞的首字母都轉換為大寫

練習2-英文繞口令

英文繞口令peterpiperpickedapeckofpickledpeppers統計這段繞口令有多少個以p開頭的單詞

練習3-間隔大寫小寫模式

把lengendary改成間隔大寫小寫模式,即LeNgEnDaRy

練習4-最后一個字母變大寫

把lengendary最后一個字母變大寫

練習5-把最后一個two單詞首字母大寫

Naturehasgivenusthattwoears,twoeyes,andbutonetongue,totheendthatweshouldhearandseemorethanwespeak把最后一個two單詞首字母大寫

publicclassStringTest{//問題1-每個單詞的首字母都轉換為大寫publicstaticString[]Split_Space(Stringstr){returnstr.split("");}publicstaticString[]Toupcase(String[]strs){String[]newstrs=newString[strs.length];inti=0;for(Stringstr:strs){char[]cs=str.toCharArray();cs[0]=Character.toUpperCase(cs[0]);newstrs[i++]=String.valueOf(cs);}returnnewstrs;}//練習2-英文繞口令publicstaticintcountp(String[]strs){intcount=0;for(Stringstr:strs){if(str.charAt(0)=='p')count++;}returncount;}//練習3-間隔大寫小寫模式publicstaticStringupdownChange(Stringstr){Stringupstr=str.toUpperCase();char[]cs=upstr.toCharArray();for(inti=1;i<cs.length;i+=2){cs[i]=Character.toLowerCase(cs[i]);}returnnewString(cs);}//練習4-最后一個字母變大寫publicstaticStringlastup(Stringstr){char[]cs=str.toCharArray();cs[cs.length-1]=Character.toUpperCase(cs[cs.length-1]);returnnewString(cs);}//練習5-把最后一個two單詞首字母大寫publicstaticStringlast2up(Strings){char[]c=s.toCharArray();Stringn="two";c[s.lastIndexOf(n)]=Character.toUpperCase(s.charAt(s.lastIndexOf(n)));returnnewString(c);}publicstaticvoidmain(String[]args){//問題1-每個單詞的首字母都轉換為大寫Stringstr="lettherebelight";String[]newstrs=Toupcase(Split_Space(str));Stringresult="";for(Strings:newstrs){result+=s+"";}System.out.println(result);//練習2-英文繞口令Stringstr2="peterpiperpickedapeckofpickledpeppers";intcount=countp(Split_Space(str2));System.out.println("首字母為p的單詞有"+count);//練習3-間隔大寫小寫模式Stringstr3="lengendary";System.out.println("間隔大小寫模式:"+updownChange(str3));//練習4-最后一個字母變大寫System.out.println("最后一個字符變成大寫:"+lastup(str3));//練習5-把最后一個two單詞首字母大寫Stringstr5="Naturehasgivenusthattwoears,twoeyes,andbutonetongue,totheendthatweshouldhearandseemorethanwespeak";System.out.println(last2up(str5));}}9.8比較字符串是否是同一個對象

str1和str2的內容一定是一樣的!但是,并不是同一個字符串對象。

==用于判斷是否是同一個字符串對象。

是否是同一個對象-特例Stringstr1="thelight";Stringstr3="thelight";System.out.println(str1==str3);

一般說來,編譯器每碰到一個字符串的字面值,就會創建一個新的對象。所以在第6行會創建了一個新的字符串"thelight";但是在第7行,編譯器發現已經存在現成的"thelight",那么就直接拿來使用,而沒有進行重復創建。

內容是否相同

使用equals進行字符串內容的比較,必須大小寫一致;equalsIgnoreCase,忽略大小寫判斷內容是否一致。

是否以子字符串開始或者結束

startsWith//以...開始;endsWith//以...結束。

練習-比較字符串

創建一個長度是100的字符串數組,使用長度是2的隨機字符填充該字符串數組。統計這個字符串數組里重復的字符串有多少種

學了hash再來做吧..

9.9StringBuffer*

StringBuffer是可變長的字符串

追加刪除插入反轉

append追加;delete刪除;insert插入;reverse反轉.

packagecharacter;publicclassTestString{publicstaticvoidmain(String[]args){Stringstr1="letthere";StringBuffer***=newStringBuffer(str1);//根據str1創建一個StringBuffer對象***.append("belight");//在最后追加System.out.println(***);***.delete(4,10);//刪除4-10之間的字符System.out.println(***);***.insert(4,"there");//在4這個位置插入thereSystem.out.println(***);***.reverse();//反轉System.out.println(***);}}長度容量

為什么StringBuffer可以變長?和String內部是一個字符數組一樣,StringBuffer也維護了一個字符數組。但是,這個字符數組,留有冗余長度。

比如說newStringBuffer("the"),其內部的字符數組的長度,是19,而不是3,這樣調用插入和追加,在現成的數組的基礎上就可以完成了。如果追加的長度超過了19,就會分配一個新的數組,長度比原來多一些,把原來的數據復制到新的數組中,看上去數組長度就變長了。

length:“the”的長度3;capacity:分配的總空間19。

publicclassTestString{publicstaticvoidmain(String[]args){Stringstr1="the";StringBuffer***=newStringBuffer(str1);System.out.println(***.length());//內容長度System.out.println(***.capacity());//總空間}}練習-StringBuffer性能

String與StringBuffer的性能區別?

生成10位長度的隨機字符串然后,先使用String的+,連接10000個隨機字符串,計算消耗的時間然后,再使用StringBuffer連接10000個隨機字符串,計算消耗的時間

提示:使用System.currentTimeMillis()獲取當前時間(毫秒)

publicstaticvoidmain(String[]args){longstart=System.currentTimeMillis();Strings1="";for(inti=0;i<10000;i++){s1+='a';}longend=System.currentTimeMillis();System.out.println("string總共耗時了:"+(end-start));longstart2=System.currentTimeMillis();StringBuffer***=newStringBuffer();for(inti=0;i<10000;i++){***.append('a');}longend2=System.currentTimeMillis();System.out.println("stringbuffer總共耗時了:"+(end2-start2));}

//java學習交流:737251827進入可領取學習資源及對十年開發經驗大佬提問,免費解答!

練習-MyStringBuffer

根據接口IStringBuffer,自己做一個MyStringBuffer

IStringBuffer.java

packagecharacter;publicinterfaceIStringBuffer{publicvoidappend(Stringstr);//追加字符串publicvoidappend(charc);//追加字符publicvoidinsert(intpos,charb);//指定位置插入字符publicvoidinsert(intpos,Stringb);//指定位置插入字符串publicvoiddelete(intstart);//從開始位置刪除剩下的publicvoiddelete(intstart,intend);//從開始位置刪除結束位置-1publicvoidreverse();//反轉publicintlength();//返回長度}

MyStringBuffer.java

packagecharacter;publicclassMyStringBufferimplementsIStringBuffer{}

9.10MyStringBuffer

具體文字說明:how2j.cn/k/number-st…

packagedigit;publicclassMyStringBufferimplementsIStringBuffer{intcapacity=19;intlength=0;char[]value;//無參構造***publicMyStringBuffer(){value=newchar[capacity];}//帶參數構造***publicMyStringBuffer(Stringstr){this();if(null==str)return;if(capacity<str.length()){capacity=value.length*2;value=newchar[capacity];}if(capacity>=str.length()){System.arraycopy(str.toCharArray(),0,value,0,str.length());}length=str.length();}//追加字符串@Overridepublicvoidappend(Stringstr){insert(length,str);}//追加字符@Overridepublicvoidappend(charc){append(String.valueOf(c));}//指定位置插入字符@Overridepublicvoidinsert(intpos,charb){insert(pos,String.valueOf(b));}//指定位置插入字符串@Overridepublicvoidinsert(intpos,Stringb){//邊界條件判斷if(pos<0)return;if(pos>length)return;if(null==b)return;//擴容while(length+b.length()>capacity){capacity=(int)((length+b.length())*2);char[]new_value=newchar[capacity];System.arraycopy(value,0,new_value,0,length);value=new_value;}char[]cs=b.toCharArray();//先把已經存在的數據往后移System.arraycopy(value,pos,value,pos+cs.length,length-pos);//把要插入的數據插入到指定位置System.arraycopy(cs,0,value,pos,cs.length);length=length+cs.length;}//從開始位置刪除剩下的@Overridepublicvoiddelete(intstart){delete(start,length);}//從開始位置刪除結束位置-1@Overridepublicvoiddelete(intstart,intend){//邊界條件判斷if(start<0)return;if(start>length)return;if(end<0)return;if(end>length)return;if(start>=end)return;System.arraycopy(value,end,value,start,length-end);length-=end-start;}//反轉@Overridepublicvoidreverse(){for(inti=0;i<length/2;i++){chartemp=value[i];value[i]=value[length-i-1];value[length-i-1]=temp;}}//返回長度@Overridepublicintlength(){returnlength;}publicStringtoString(){char[]realValue=newchar[length];System.arraycopy(value,0,realValue,0,length);returnnewString(realValue);}publicstaticvoidmain(String[]args){MyStringBuffer***=newMyStringBuffer("therelight");System.out.println(***);***.insert(0,"let");System.out.println(***);***.insert(10,"be");System.out.println(***);***.insert(0,"GodSay:");System.out.println(***);***.append("!");System.out.println(***);***.append('?');System.out.println(***);***.reverse();System.out.println(***);***.reverse();System.out.println(***);***.delete(0,4);System.out.println(***);***.delete(4);System.out.println(***);}}

猜你喜歡

主站蜘蛛池模板: 在线观看网站国产 | 久久国内精品 | 成人毛片一区二区三区 | 五月亭亭免费高清在线 | 久久久91精品国产一区二区三区 | 亚洲影视一区 | 亚洲综合一区二区精品久久 | 亚洲成人黄色网址 | 自拍亚洲欧美 | 亚洲天堂男人 | 欧美视频在线播放观看免费福利资源 | 制服丝袜自拍偷拍 | 天天五月天丁香婷婷深爱综合 | 欧美.亚洲.日本一区二区三区 | 欧美中日韩在线 | 亚洲六月丁香六月婷婷色伊人 | 爱啪啪影视| 国产a一级毛片午夜剧场14 | 久久午夜免费视频 | 九色视频网址 | 波多野结衣电影网址 | 激情网页| 日韩久久综合 | 久久久久久久亚洲精品 | 亚洲这里只有精品 | 国产成人一区二区三中文 | 免费在线观看日本 | 中文字幕精品1在线 | 欧美亚洲一区二区三区 | 修罗的游戏 | 亚洲国产网站 | 最新在线精品国自拍视频 | 亚洲精品天堂自在久久77 | 久久综合一本 | 伊人精品在线观看 | 久久久国产精品网站 | 五月婷婷在线观看 | 最新99热 | 欧美日韩国产亚洲一区二区三区 | 99欧美| 成人在线日韩 |