java第四部分工具的使用-集合.docx_第1頁
java第四部分工具的使用-集合.docx_第2頁
java第四部分工具的使用-集合.docx_第3頁
java第四部分工具的使用-集合.docx_第4頁
java第四部分工具的使用-集合.docx_第5頁
已閱讀5頁,還剩178頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

String類特點:字符串對象一旦被初始化就不會被改變package on.itcast.p1.string.demo;public class StringDemo public static void main(String args) / TODO Auto-generated method stub/* * String類的特點: * 字符串對象一旦被初始化就不會被改變 */stringDemo2();public static void stringDemo2() / TODO Auto-generated method stubString s=abc;/創(chuàng)建一個字符串對象在常量池中String s1=new String(abc);/創(chuàng)建兩個對象,一個new,一個字符串對象,在堆內(nèi)存中System.out.println(s=s1);/falseSystem.out.println(s.equals(s1);/ true string類中的equals復(fù)寫了object中equals建立了string類自己的判斷字符串對象是否相同的依據(jù) /其實就是比較字符串內(nèi)容/System.out.println(s=+s);/System.out.println(s1=+s1);/* * 演示字符串定義的第一種方式,并明確字符串常量池的特點。 * 池中沒有就建立,池中有,直接用 */private static void stringDemo1() String s=abc;/abc存儲在字符串常量池中,如果沒有就創(chuàng)建,如果有就直接拿過來使用/s=nba;String s1=abc;System.out.println(s=s1);/true/System.out.println(s=+s);構(gòu)造函數(shù)String s=new String();/等效于String s=;內(nèi)容相同,地址不同;不等效于String s=null;private static void stringConstructorDemo() byte arr=65,66,67,68;String s1=new String(arr);/將一個字節(jié)數(shù)字變成字符串System.out.println(s1=+s1);/s1=ABCDpublic static void stringConstructorDemo2() char arr=w,a,p,q,x;String s=new String(arr);System.out.println(s=+s);/s=wapqxpublic static void stringConstructorDemo2() char arr=w,a,p,q,x;String s=new String(arr,1,3);/含頭含尾System.out.println(s=+s);/s=apq常見功能常見功能-獲取/* * 按照面向?qū)ο蟮乃枷雽ψ址M行功能分類 * “abcde” * 1,獲取 * 1.1獲取字符串中字符的個數(shù)(長度) * int length(); * 1.2根據(jù)位置獲取字符 * char charAt(int index); * 1.3根據(jù)指定字符獲取在字符串中第一次出現(xiàn)的位置(重點掌握 ) * int indexOf(int ch); * int indexOf(int ch,int fromIndex);從指定位置進行指定字符查找, 獲取其第一次出現(xiàn)的位置 * int IndexOf(String str);返回指定字符串在此字符串中第一次出現(xiàn)的素銀 * int IndexOf(String str,int fromIndex);從指定索引開始,獲取指定字符串 在此字符串中第一次出現(xiàn)的索引 * 根據(jù)指定字符串獲取在字符串中最后一次出現(xiàn)的位置(重點掌握 ) * int lastIndexOf(int ch); * int lastIndexOf(int ch,int fromIndex); * int lastIndexOf(String str); * int lastIndexOf(String str,int fromIndex); * 1.4獲取字符串中一部分字符串。也叫子串 * String substring(int beginIndex,int endIndex);/包含begin,不包含end * String substring(int beginIndex); */package on.itcast.p1.string.demo;public class StringMethodDemo public static void main(String args) stringMethodDemo_1();private static void stringMethodDemo_1() String s=abcdae;System.out.println(length=+s.length();/6System.out.println(char:+s.charAt(2);/c System.out.println(char:+s.charAt(20);/StringIndexOutOfBoundsException字符串角標越界System.out.println(index:+s.indexOf(a);/0System.out.println(index:+s.indexOf(k);/-1,我們可以根據(jù)-1,來判斷該字符或者字符串是否存在 System.out.println(lastIndex:+s.lastIndexOf(a);/4 System.out.println(substring:+s.substring(2);/cdae System.out.println(substring:+s.substring(1, 3);/bc常見功能-轉(zhuǎn)換/* * 按照面向?qū)ο蟮乃枷雽ψ址M行功能分類 * “abcde” * 2.轉(zhuǎn)換 * 2.1將字符串轉(zhuǎn)換成字符串數(shù)組(字符串的切割) * String split(String regex);涉及到正則表達式 * 2.2將字符串轉(zhuǎn)換成字符數(shù)組 * char toCharArray(); * 2.3將字符串轉(zhuǎn)換成 字節(jié)數(shù)組 * byte getBytes(); * 2.4將字符串中的字母轉(zhuǎn)換成大小寫 * String toUpperCase();大寫 * String toLowerCase();小寫 * 2.5將字符串中的內(nèi)容進行替換 * String replace(char oldChar,char newChar); * String replace(String s1,String s2); * 2.6將字符串兩端的空格去除 * String trim(); * 2.7將字符串進行連接 * String concat(String str); * 2.8將數(shù)字轉(zhuǎn)換成字符串 * String valueOf(char c); */private static void stringMethodDemo_2() String s=張三.李四.王五;String s1=張三,李四,王五;String arr=s.split(.);String arr1=s1.split(,);for(int i=0;iarr.length ;i+)System.out.println(arri);System.out.println(arr1i);/ 張三/ 張三/李四/李四/王五/王五char chs=s.toCharArray();for(int i=0;ichs.length ;i+)System.out.println(chsi);/張/三/./李/四/./王/五s=ab你;/一個中文兩個字節(jié)byte bytes=s.getBytes();for (int i = 0; i bytes.length; i+) System.out.println(bytesi);/97/98/-60/-29/System.out.println(Abc.toUpperCase();/ABCSystem.out.println(Abc.toLowerCase();/abcSystem.out.println(java.replace(a, o);/jovoString s2=java;String s3=s2.replace(q,z);System.out.println(s2=s3);/trueSystem.out.println(-+ ab c .trim()+-);/-ab c-System.out.println(abc.concat(kk);/abckkSystem.out.println(abc+kk);/abckkSystem.out.println(String.valueOf(4)+1);/41,把4變成字符串與1相連接System.out.println(+4+1);/41,把4變成字符串與1相連接常見功能-判斷/* * 3.判斷 * 3.1判斷兩個字符串內(nèi)容是否相同?。?* boolean equals(Object obj); * boolean equalsIgnoreCase(String str);忽略大小寫比較字符串內(nèi)容 * 3.2判斷字符串中是否包含指定字符串? * boolean contains(String str); * 3.3判斷字符串是否以指定字符串開頭,是否以指定字符串結(jié)尾 * boolean startsWith(string); * boolean endsWith(string); */private static void stringMethodDemo_3() String s=abc;System.out.println(s.equals(ABC);/falseSystem.out.println(s.equals(ABC.toLowerCase();/trueSystem.out.println(s.equalsIgnoreCase(ABC);/trueSystem.out.println(s.contains(bc);/trueString str=ArryDemo.java;System.out.println(str.startsWith(Arry);/trueSystem.out.println(str.endsWith(.java);/trueSystem.out.println(str.contains(Demo);/true常見功能-比較/* * 4.比較 * 4.1字符串對象之間的比較 * int compareTo(String antherString);相等為0;大于為大于0的值;小于為小于0的值; */private static void stringMethodDemo_4() System.out.println(pareTo(A);/32Intern方法:返回字符串對象的規(guī)范化表示形式package on.itcast.p1.string.demo;public class StringObjectDemo public static void main(String args) String s1=abc;String s2=abc;System.out.println(s1=s2);/true/intern():對字符串池進行操作String s3=abc;String s4=ern();System.out.println(s3=s4);/trueString s5=new String(bcd);/對象的私有數(shù)據(jù)String s6=ern();/取常量池里的數(shù)據(jù)System.out.println(s5=s6);/false練習(xí)1. 給定一個字符串數(shù)組。按照字段順序進行從小到大的排序(“hba”,”abc”,”cba”,”zz”,”qq”,”haha”)package on.itcast.p1.string.demo;/* * 1.給定一個字符串數(shù)組。按照字段順序進行從小到大的排序 * (“hba”,”abc”,”cba”,”zz”,”qq”,”haha”) * 思路: * 1.對數(shù)組排序,可以用選擇,冒泡都行 * 2.for嵌套和比較,以及換位 * 3.問題:以前排的是整數(shù),比較用的是比較運算符;但現(xiàn)在是字符串對象 * 字符串對象怎么比較呢? * 對象中提供了用于字符串對象比較的功能 */public class StringTest1 public static void main(String args) String arr = nba, abc, cba, zz, qq, haha ;printArry(arr);sortString(arr);printArry(arr);public static void sortString(String arr) for (int i = 0; i arr.length - 1; i+) for (int j = i + 1; j 0)swap(arr, i, j);private static void swap(String arr, int i, int j) String temp = arri;arri = arrj;arrj = temp;public static void printArry(String arr) System.out.print();for (int i = 0; i s2.length()?s1:s2;min=max.equals(s1)?s2:s1;System.out.println(max=+max);System.out.println(min=+min);for (int i = 0; i min.length(); i+) for (int a= 0,b=min.length()-i; b!=min.length()+1;a+,b+) String sub=min.substring(a,b);/System.out.println(sub);if (max.contains(sub) return sub;return null;4. 模擬一個trim功能一致的方法package on.itcast.p1.string.demo;/* * 4.模擬一個trim功能一致的方法 * 去除字符串兩端的空白 * 思路: * 1.定義兩個變量 * 一個變量作為從頭開始判斷字符串空格的角標,不斷+ * 一個變量作為從尾開始判斷字符串空格的角標,不斷- * 2.判斷到不是空格為止,取頭尾之間的字符串即可 */public class StringTest4 public static void main(String args) String s= ab c ;s=myTrim(s);System.out.println(-+s+-);public static String myTrim(String s) int start=0,end=s.length()-1;while (start=end&s.charAt(start)= ) start+;while (start=end&s.charAt(end)= ) end-;return s.substring(start,end+1);StringBuffer類(了解會用即可)package on.itcast.p2.stringbuffer.demo;/* * StringBuffer:就是字符串緩沖區(qū) * 用于存儲數(shù)據(jù)的容器 * 特點: * 1.長度是可變的 * 2.可以存儲不同類型的數(shù)據(jù) * 3.最終要轉(zhuǎn)成字符串進行使用 * 4.可以對字符串進行修改 * * 既然是一個容器對象,應(yīng)該具備什么功能呢? * 1.添加: * StringBuffer append(data); * StringBuffer insert(index,data); * 2.刪除 * StringBuffer delete(start,end);包含頭,不包含尾 *StringBuffer delete(int index);刪除指定位置的元素 *StringBuffer delete(0,str.length();清空緩沖區(qū) *3.查找 *char charAt(index); *int indexOf(string); *int lastIndexOf(string); *4.修改 *StringBuffer replace(start,end,string); *void setCharAt(index,char); *StringBuffer setLength(int data);/設(shè)置長度 *StringBuffer reverse();/反轉(zhuǎn) *增刪查改C(create)U(update)R(read)D(delete) */public class StringBufferDemp public static void main(String args) bufferMethodDemo3();public static void bufferMethodDemo3() StringBuffer sb=new StringBuffer(we are here);System.out.println(sb);/we are heresb.replace(3, 6, love);System.out.println(sb);/we love heresb.setCharAt(0, m);System.out.println(sb);/me love here/sb.setLength(10);/System.out.println(sb);/we are her/System.out.println(sb.length();/10sb.reverse();System.out.println(sb);/ereh evol empublic static void bufferMethodDemo2() StringBuffer sb=new StringBuffer(abcdefghijkl);sb.delete(2, 6);System.out.println(sb);/abghijkl/清空緩沖區(qū)sb.delete( 0, sb.length();System.out.println(sb);/空白public static void bufferMethodDemo1() /創(chuàng)建緩沖區(qū)對象StringBuffer sb=new StringBuffer();sb.append(4);sb.append(true).append(false).append(521).append(haha);sb.insert(10, love);System.out.println(sb);/4truefalselove521hahaSystem.out.println(sb.toString();/4truefalselove521hahaStringBuilderpackage on.itcast.p2.stringbuffer.demo;/* * jdk1.5以后出現(xiàn)功能和StringBuffer一模一樣的對象,就是StrngBuilder * 不同的是: * StringBuffer是線程同步的,通常用于多線程 * StringBuilder是線程不同的,通常用于單線程 * * jdk升級目的: * 1.簡化書寫 * 2.提高效率 * 3.增加安全性 */public class StringBuilderDemo public static void main(String args) /* class StringBuffer/jdk1.0 Object lock; public StringBuffer append(int x) synchronized(lock) public StringBuffer delete(int start,int end) synchronized(lock) */何時使用存完的數(shù)據(jù)最終要以字符串形式來使用就想到StringBuffer和StringBuilderpackage on.itcast.p2.stringbuffer.test;public class StringBuilderTest public static void main(String args) int arr=3,1,2,5,6;String s=arrayToString2(arr);System.out.println(s);public static String arrayToString2(int arr) /該方法效率高StringBuilder sb=new StringBuilder();sb.append();for (int i = 0; i arr.length; i+) if (i!=arr.length-1) sb.append(arri+,);elsesb.append(arri+);return sb.toString();/* * 將一個int數(shù)組變成字符串 */public static String arrayToString(int arr) String str=;for (int i = 0; i 字符串 * 1.基本類型數(shù)值+; * 2.用String類中的靜態(tài)方法valueOf(基本類型數(shù)值); * 3.用Integer的靜態(tài)方法valueOf(基本類型數(shù)值); *字符串-基本類型 *1.使用包裝類中的靜態(tài)方法xxx parseXxx(xxx類型的字符串);(重點) *int parseInt(intstring); *long parseLong(longstring); *boolean parseBoolean(booleanstring); *只有Character沒有parse方法 *2.如果字符串被Integer進行對象的封裝 *可以使用另一個非靜態(tài)的方法,intValue(); *將一個Integer對象轉(zhuǎn)成基本數(shù)據(jù)類型值。 */int num;System.out.println(Integer.MAX_VALUE);/2147483647 System.out.println(Integer.toBinaryString(6);/110,6轉(zhuǎn)換為二進制是110System.out.println(Integer.toBinaryString(-6);/11111111111111111111111111111010int num=4;Integer i=new Integer(num);System.out.println(123+1);/1231System.out.println(Integer.parseInt(123)+1);/124Integer i=new Integer(123);System.out.println(Value()+2);/125/* * 整數(shù)具備不同的進制體現(xiàn) * * 十進制-其他進制 * toBinaryString * toOctalString * toHexString * 其他進制-十進進制 * parseInt(String s,int radix);/s使用第二個參數(shù)指定的基數(shù),將字符串參數(shù)解析為有符號的整數(shù);radix表示的是該數(shù)據(jù)的進制。S為2進制radix就代表2 * */ 十進制-其他進制System.out.println(Integer.toBinaryString(60);/十進制轉(zhuǎn)二進制111100System.out.println(Integer.toOctalString(60);/十進制轉(zhuǎn)八進制74System.out.println(Integer.toHexString(60);/十進制轉(zhuǎn)十六進制3cSystem.out.println(Integer.toString(60,4);/十進制轉(zhuǎn)換成四進制330/其他進制-十進制System.out.println(Integer.parseInt(110,2);/6Integer a=new Integer(3);Integer b=new Integer(3);System.out.println(a=b);/false因為比較的是兩個對象System.out.println(a.equals(b);/true因為比較的是兩個對象的值System.out.println(pareTo(b);/0因為兩個內(nèi)容相同JDK1.5自動裝箱拆箱(面試題)package cn.itcast.p2.wapper.demo;public class WrapperDemo2 public static void main(String args) int num = 4;num = num + 5;Integer i = 4;/ i=new Integer(4);1.5以前的版本這樣寫/ 現(xiàn)在可以自動裝箱,簡化書寫i = i + 6;/i=i+6show(55);/面試題Integer a=new Integer(127);Integer b=new Integer(127);System.out.println(a=b);/falseSystem.out.println(a.equals(b);/trueInteger x=127;Integer y=127;System.out.println(x=y);/trueSystem.out.println(x.equals(y);/trueInteger c=128;/jdk1.5以后,自動裝箱,如果裝箱的是一個字節(jié),那么該數(shù)據(jù)會被共享不會開辟空間Integer d=128;System.out.println(c=d);/falseSystem.out.println(c.equals(d);/truepublic static void show(Object a) System.out.println(a=+a);/a=55練習(xí)package cn.itcast.p2.wapper.demo;import java.util.Arrays;/* * 對一個字符串中的數(shù)值進行從小到大的排序 * * 20 78 9 -7 88 36 29 * 思路: * 1.排序,我很熟 * 2.如何獲取到這個字符串中的這些需要排序的數(shù)值? * 發(fā)現(xiàn)這個字符串中其實都是空格來對數(shù)值進行分隔的。 * 所以就想到用字符串對象的切割方法將大串變成多個小串 * 3.數(shù)值最終變成字符串數(shù)組,怎么變成一個int數(shù)呢? * 字符串-基本類型可以使用包裝類 */public class WrapperTest private static final String SPACE_SEPARATOR= ;public static void main(String args) String numStr=20 78 9 -7 88 36 29;System.out.println(numStr);numStr=sortStringNumber(numStr);System.out.println(numStr);public static String sortStringNumber(String numStr) /1.將字符串變成字符串數(shù)組String str_arr=stringToArry(numStr);/2.將字符串數(shù)組變成int數(shù)值int num_arr=toIntArry(str_arr);/3.對int數(shù)值排序mySortArry(num_arr);/4.將排序后的int數(shù)值變成字符串String temp=arrayToString(num_arr);return temp;public static String arrayToString(int num_arr) StringBuilder sb=new StringBuilder();for (int i = 0; i num_arr.length; i+) if (i!=num_arr.length-1)sb.append(num_arri+ );elsesb.append(num_arri);return sb.toString();public static void mySortArry(int num_arr) Arrays.sort(num_arr);static int toIntArry(String str_arr) int arr=new intstr_arr.length;for (int i = 0; i arr.length; i+) arri=Integer.parseInt(str_arri);return arr;public static String stringToArry(String

溫馨提示

  • 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)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論