Java中的Scanner类和String类
1:Scanner的使用(了解)
?? ?(1)在JDK5以后出現(xiàn)的用于鍵盤(pán)錄入數(shù)據(jù)的類(lèi)。
?? ?(2)構(gòu)造方法:
?? ??? ?A:講解了System.in這個(gè)東西。
?? ??? ??? ?它其實(shí)是標(biāo)準(zhǔn)的輸入流,對(duì)應(yīng)于鍵盤(pán)錄入
?? ??? ?B:構(gòu)造方法
?? ??? ??? ?InputStream is = System.in;
?? ??? ??? ?Scanner(InputStream is)
?? ??? ?C:常用的格式
?? ??? ??? ?Scanner sc = new Scanner(System.in);
?? ?(3)基本方法格式:
?? ??? ?A:hasNextXxx() 判斷是否是某種類(lèi)型的
?? ??? ?B:nextXxx()?? ?返回某種類(lèi)型的元素
?? ?(4)要掌握的兩個(gè)方法
?? ??? ?A:public int nextInt()
?? ??? ?B:public String nextLine()
?? ?(5)需要注意的小問(wèn)題
?? ??? ?A:同一個(gè)Scanner對(duì)象,先獲取數(shù)值,再獲取字符串會(huì)出現(xiàn)一個(gè)小問(wèn)題。
?? ??? ?B:解決方案:
?? ??? ??? ?a:重新定義一個(gè)Scanner對(duì)象
?? ??? ??? ?b:把所有的數(shù)據(jù)都用字符串獲取,然后再進(jìn)行相應(yīng)的轉(zhuǎn)換
2:String類(lèi)的概述和使用(掌握)
?? ?(1)多個(gè)字符組成的一串?dāng)?shù)據(jù)。
?? ??? ?其實(shí)它可以和字符數(shù)組進(jìn)行相互轉(zhuǎn)換。
?? ?(2)構(gòu)造方法:
?? ??? ?A:public String()
?? ??? ?B:public String(byte[] bytes)
?? ??? ?C:public String(byte[] bytes,int offset,int length)
?? ??? ?D:public String(char[] value)
?? ??? ?E:public String(char[] value,int offset,int count)
?? ??? ?F:public String(String original)
?? ??? ?下面的這一個(gè)雖然不是構(gòu)造方法,但是結(jié)果也是一個(gè)字符串對(duì)象
?? ??? ?G:String s = "hello";
?? ?(3)字符串的特點(diǎn)
?? ??? ?A:字符串一旦被賦值,就不能改變。
?? ??? ??? ?注意:這里指的是字符串的內(nèi)容不能改變,而不是引用不能改變。
?? ??? ?B:字面值作為字符串對(duì)象和通過(guò)構(gòu)造方法創(chuàng)建對(duì)象的不同
?? ??? ??? ?String s = new String("hello");和String s = "hello"的區(qū)別?
一個(gè)是在堆上創(chuàng)建,一個(gè)是在方法區(qū)的常量區(qū)創(chuàng)建。
其中String s = new String("hello")是在堆上創(chuàng)建,再創(chuàng)建的過(guò)程中,回去方法區(qū)查找“hello”的存在,若存在即返回對(duì)應(yīng)的內(nèi)存地址給堆上的對(duì)象。若不存在就創(chuàng)建,再返回創(chuàng)建后的地址值。
String s = "hello"是直接去查找方法區(qū),若存在就返回地址值給s,若不存在,則創(chuàng)建后再返回。
?? ?(4)字符串的面試題(看程序?qū)懡Y(jié)果)
注意: “==”比較的是對(duì)象的地址值,equals是object類(lèi)中的方法,默認(rèn)比較的也是地址值,一般情況下,子類(lèi)都會(huì)重寫(xiě)equals方法。
?? ??? ?A:==和equals()
?? ??? ??? ?String s1 = new String("hello");
?? ??? ??? ?String s2 = new String("hello");
?? ??? ??? ?System.out.println(s1 == s2);// false
?? ??? ??? ?System.out.println(s1.equals(s2));// true
?? ??? ??? ?String s3 = new String("hello");
?? ??? ??? ?String s4 = "hello";
?? ??? ??? ?System.out.println(s3 == s4);// false
?? ??? ??? ?System.out.println(s3.equals(s4));// true
?? ??? ??? ?String s5 = "hello";
?? ??? ??? ?String s6 = "hello";
?? ??? ??? ?System.out.println(s5 == s6);// true
?? ??? ??? ?System.out.println(s5.equals(s6));// true
?? ??? ?B:字符串的拼接
?? ??? ??? ?String s1 = "hello";
?? ??? ??? ?String s2 = "world";
?? ??? ??? ?String s3 = "helloworld";
?? ??? ??? ?System.out.println(s3 == s1 + s2);// false
?? ??? ??? ?System.out.println(s3.equals((s1 + s2)));// true
?? ??? ??? ?System.out.println(s3 == "hello" + "world");// false 這個(gè)我們錯(cuò)了,應(yīng)該是true
?? ??? ??? ?System.out.println(s3.equals("hello" + "world"));// true
?? ?(5)字符串的功能(自己補(bǔ)齊方法中文意思)
?? ??? ?A:判斷功能
?? ??? ??? ?boolean equals(Object obj)
?? ??? ??? ?boolean equalsIgnoreCase(String str)
?? ??? ??? ?boolean contains(String str)
?? ??? ??? ?boolean startsWith(String str)
?? ??? ??? ?boolean endsWith(String str)
?? ??? ??? ?boolean isEmpty()
?? ??? ?B:獲取功能(查詢(xún)功能)
?? ??? ??? ?int length()
?? ??? ??? ?char charAt(int index)
?? ??? ??? ?int indexOf(int ch)
?? ??? ??? ?int indexOf(String str)
?? ??? ??? ?int indexOf(int ch,int fromIndex)
?? ??? ??? ?int indexOf(String str,int fromIndex)
?? ??? ??? ?String substring(int start)
?? ??? ??? ?String substring(int start,int end)
?? ??? ?C:轉(zhuǎn)換功能
?? ??? ??? ?byte[] getBytes()
?? ??? ??? ?char[] toCharArray()
?? ??? ??? ?static String valueOf(char[] chs)
?? ??? ??? ?static String valueOf(int i)
?? ??? ??? ?String toLowerCase()
?? ??? ??? ?String toUpperCase()
?? ??? ??? ?String concat(String str)
?? ??? ?D:其他功能
?? ??? ??? ?a:替換功能
?? ??? ??? ??? ?String replace(char old,char new)
?? ??? ??? ??? ?String replace(String old,String new)
?? ??? ??? ?b:去空格功能
?? ??? ??? ??? ?String trim()
?? ??? ??? ?c:按字典比較功能
?? ??? ??? ??? ?int compareTo(String str)
?? ??? ??? ??? ?int compareToIgnoreCase(String str)
?? ?(6)字符串的案例
?? ??? ?A:模擬用戶(hù)登錄
?? ??? ?B:字符串遍歷
?? ??? ?C:統(tǒng)計(jì)字符串中大寫(xiě),小寫(xiě)及數(shù)字字符的個(gè)數(shù)
?? ??? ?D:把字符串的首字母轉(zhuǎn)成大寫(xiě),其他小寫(xiě)
?? ??? ?E:把int數(shù)組拼接成一個(gè)指定格式的字符串
?? ??? ?F:字符串反轉(zhuǎn)
?? ??? ?G:統(tǒng)計(jì)大串中小串出現(xiàn)的次數(shù)
轉(zhuǎn)載于:https://www.cnblogs.com/ljy2013/p/4729425.html
總結(jié)
以上是生活随笔為你收集整理的Java中的Scanner类和String类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: innodb中master线程的调度的算
- 下一篇: SQL 修改排序规则的问题 sql_la