《常用实用类》PPT课件.ppt

上传人:牧羊曲112 文档编号:5503510 上传时间:2023-07-14 格式:PPT 页数:82 大小:1.06MB
返回 下载 相关 举报
《常用实用类》PPT课件.ppt_第1页
第1页 / 共82页
《常用实用类》PPT课件.ppt_第2页
第2页 / 共82页
《常用实用类》PPT课件.ppt_第3页
第3页 / 共82页
《常用实用类》PPT课件.ppt_第4页
第4页 / 共82页
《常用实用类》PPT课件.ppt_第5页
第5页 / 共82页
点击查看更多>>
资源描述

《《常用实用类》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《常用实用类》PPT课件.ppt(82页珍藏版)》请在三一办公上搜索。

1、第9章 常用实用类,Java专门提供了用来处理字符序列的String类String类在java.lang包中,由于java.lang包中的类被默认引入,因此程序可以直接使用String类。需要注意的是Java把String类声明为final类,因此用户不能扩展String类,即String类不可以有子类,9.1 String类,可以使用String类来创建一个字符串变量,字符串变量是对象。1常量对象字符串常量对象是用双引号括起的字符序列如:“123”,“abc”等等2字符串对象可以使用String类声明字符串对象String s;s=new String(“你好”);,9.1.1 构造字符串对

2、象,(1)String(char a):用一个字符数组a创建一个字符串对象 char a=J,a,v,a;String s=new String(a);等价于String s=new String(Java);(2)String(char a,int startIndex,int count)提取字符数组a中的一部分字符创建一个字符串对象,参数startIndex和count分别指定在a中提取字符的起始位置和从该位置开始截取的字符个数,String类还有两个较常用构造方法,String类还有两个较常用构造方法,(2)String(char a,int startIndex,int count)

3、提取字符数组a中的一部分字符创建一个字符串对象,参数startIndex和count分别指定在a中提取字符的起始位置和从该位置开始截取的字符个数 char a=J,a,v,a;String s=new String(a,1,3);,字符串常量是对象,因此可以把字符串常量的引用赋值给一个字符串变量 String s1,s2;s1=how are you;s2=how are you;,3引用字符串常量对象,1public int length()使用String 类中的length()方法可以获取一个字符串的长度 String s1;s1=“abc;n=s1.length();2public b

4、oolean equals(String s)字符串对象调用equals(String s)方法比较当前字符串对象的实体是否与参数s指定的字符串的实体相同,9.1.2 String 类的常用方法,equals举例,String s1,s2,s3;s1=how are you;s2=how are you;s3=Hi;s1.equals(s2)的值是trues1.equals(s3)的值是false,9-1,public class Example9_1 public static void main(String args)String s1,s2;s1=new String(天道酬勤);s2

5、=new String(天道酬勤);System.out.println(s1.equals(s2);/输出结果是:true System.out.println(s1=s2);/输出结果是:false String s3,s4;s3=勇者无敌;s4=勇者无敌;System.out.println(s3.equals(s4);/输出结果是:true System.out.println(s3=s4);/输出结果是:true,3public boolean startsWith(String s)、public boolean endsWith(String s)方法字符串对象调用startsW

6、ith(String s)方法,判断当前字符串对象的前缀是否是参数s指定的字符串 String tom=天气预报;tom.startsWith(“天气”)的值是true,4public int compareTo(String s)方法字符串对象可以使用String类中的compareTo(String s)方法,按字典序与参数s指定的字符串比较大小。如果当前字符串与s相同,该方法返回值0;如果当前字符串对象大于s,该方法返回正值;如果小于s,该方法返回负值,9-2,import;public class SortString public static void sort(String a)

7、int count=0;for(int i=0;ia.length-1;i+)for(int j=i+1;ja.length;j+)if(pareTo(ai)0)count+;(交换%s和%s:,ai,aj);String temp=ai;ai=aj;aj=temp;(第+count+次排序结果:);System.out.println(Arrays.toString(a);,5public boolean contains(String s)字符串对象调用contains方法,判断当前字符串对象是否含有参数指定的字符串s String tom=student tom.contains(“st

8、u”)的值为truetom.contains(“ok”)的值为false,6.public int indexOf(String s)从当前字符串的头开始检索字符串s,并返回首次出现s的索引位置。如果没有检索到字符串s,该方法返回的值是-1。字符串调用indexOf(String s,int startpoint)方法从当前字符串的startpoint位置处开始检索字符串s,并返回首次出现s的索引位置。如果没有检索到字符串s,该方法返回的值是-1。字符串调用lastIndexOf(String s)方法从当前字符串的头开始检索字符串s,并返回最后出现s的索引位置。如果没有检索到字符串s,该方法

9、返回的值是-1。,举例,String tom=I am a good cattom.indexOf(a)值为2tom.indexOf(good)的值是7tom.indexOf(a,7)值为13tom.indexOf(w,2)值为-1,7.public String substring(int startpoint)字符串对象调用该方法获得一个当前字符串的子串,该子串是从当前字符串的startpoint处截取到最后所得到的字符串。String s=abc;s.substring(1)的值是bc字符串对象调用substring(int start,int end)方法获得一个当前字符串的子串,该子

10、串是从当前字符串的star索引位置截取到end索引位置所得到的字符串,但不包括end索引位置上的字符,8public String trim()一个字符串s通过调用方法trim()得到一个字符串对象,该字符串对象是s去掉前后空格后的字符串。,9-3,public class Example9_3 public static void main(String args)String path=c:bookjavabookJava Programmer.doc;int index=path.indexOf();index=path.indexOf(,index);String sub=path.s

11、ubstring(index);System.out.println(sub);/输出结果是:bookjavabookJava Programmer.doc index=path.lastIndexOf();System.out.println(index);sub=path.substring(index+1);System.out.println(sub);/输出结果是:Java Programmer.doc System.out.println(sub.contains(Programmer);/输出结果是:true,java.lang包中的Integer类调用其类方法:public s

12、tatic int parseInt(String s)可以将由“数字”字符组成的字符串,转化为int型数据 int x;String s=123;x=Integer.parseInt(s);,9.1.3 字符串与基本数据的相互转化,类似地,使用java.lang包中的Byte、Short、Long、Float、Double类调相应的类方法:public static byte parseByte(String s)throws NumberFormatExceptionpublic static short parseShort(String s)throws NumberFormatExc

13、eptionpublic static long parseLong(String s)throws NumberFormatExceptionpublic static float parseFloat(String s)throws NumberFormatExceptionpublic static double parseDouble(String s)throws NumberFormatException可以将由“数字”字符组成的字符串,转化为相应的基本数据类型,可以使用String 类的下列类方法:public static String valueOf(byte n)publi

14、c static String valueOf(int n)public static String valueOf(long n)public static String valueOf(float n)public static String valueOf(double n)将形如123、1232.98等数值转化为字符串,将数字转化为字符串的方法,9-4,public class Example9_4 public static void main(String args)double aver=0,sum=0,item=0;boolean computable=true;for(Str

15、ing s:args)try item=Double.parseDouble(s);sum=sum+item;catch(NumberFormatException e)(您键入了非数字字符:+e);computable=false;if(computable)System.out.println(sum=+sum);,说明,main方法中的参数args能接收用户从键盘键入的字符串。要求在解释字节码的时候输入args数组中的元素:如Java Example9_4 12 33 19这时,args0=“12”,args1=“33”,args2=“19”,所有的类都默认是java.lang包中Obj

16、ect类的子类或间接子类。Object类有一个public String toString()方法,一个对象通过调用该方法可以获得该对象的字符串表示。一个对象调用toString()方法返回的字符串的一般形式为:创建对象的类的名字对象的引用的字符串表示,9.1.4 对象的字符串表示,9-5,public class TV String name;public TV()public TV(String s)name=s;public String toString()String oldStr=super.toString();return oldStr+n这是电视机,品牌是:+name;,9-

17、5,import;public class Example9_5 public static void main(String args)Date date=new Date();System.out.println(date.toString();TV tv=new TV(长虹电视);System.out.println(tv.toString();,1字符串与字符数组String类也提供了将字符串存放到数组中的方法:public void getChars(int start,int end,char c,int offset)字符串调用getChars()方法将当前字符串中的一部分字符复

18、制到参数c指定的数组中,将字符串中从位置start到end-1位置上的字符复制的数组c中,并从数组c的offset处开始存放这些字符,9.1.5 字符串与字符、字节数组,还有一个简练的将字符串中的全部字符存放在一个字符数组中的方法:public char toCharArray()字符串对象调用该方法返回一个字符数组,该数组的长度与字符串的长度相等、第i单元中的字符刚好为当前字符串中的第i个字符。,toCharArray方法,9-6,public class Example9_6 public static void main(String args)char a,b,c;String s=2

19、009年10月1日是国庆60周年;a=new char2;s.getChars(11,13,a,0);System.out.println(a);c=十一长假期间,学校都放假了.toCharArray();for(char ch:c)System.out.print(ch);,String类的构造方法String(byte)用指定的字节数组构造一个字符串对象。String(byte,int offset,int length)构造方法用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一个字符串对象。,2字符串与字节数组,public byte getBytes(

20、)方法使用平台默认的字符编码,将当前字符串转化为一个字节数组。public byte getBytes(String charsetName)使用参数指定字符编码,将当前字符串转化为一个字节数组。如果平台默认的字符编码是:GB_2312(国标,简体中文),那么调用getBytes()方法等同于调用getBytes(GB2312),public class Example9_7 public static void main(String args)byte d=Java你好.getBytes();(数组d的长度是:+d.length);String s=new String(d,6,2);/输

21、出:好 System.out.println(s);s=new String(d,0,6);System.out.println(s);/输出:Java你,3 字符串的加密算法(自学),使用一个字符串password作为密码对另一个字符串sourceString进行加密首先将password存放到一个字符数组里char p=password.toCharArray();明文中分为很多组,对每一组中的字符用数组a 的对应字符做加法运算。加密后:c0=(char)(a0+p0),c1=(char)(a1+p1),.,1正则表达式一个正则表达式是含有一些具有特殊意义字符的字符串,这些特殊字符称作正则

22、表达式中的元字符如“dcat”中的d就是由特殊意义的元字符,代表0-9中的任何一个。字符串0cat,1cat,2cat.9cat都是和正则表达式:dcat匹配字符串,9.1.6 正则表达式及字符串的替换与分解,字符串对象调用public boolean matches(String regex)方法可以判断当前字符串对象是否和参数regex指定的正则表达式匹配,元字符,说明,在正则表达式中可以用方括号括起来若个个字符来表示一个元字符,该元字符代表方括号中的任何一个字符。例如,regex=“159ABC”,那么“1ABC”,“5ABC”,”9ABC”都是和正则表达式regex匹配的字符串。更多内

23、容见P154,限定符,在正则表达式中可以使用限定符;如:X?,代表X出现0次或1次regex=“hello2468?”那么“hello”、“hello2”、“hello4”、“hello6”、“hello8”、都是与正则表达式regex匹配的字符串,限定符,例9-9,import;public class Example9_9 public static void main(String args)String regex=a-zA-Z“;Scanner scanner=new Scanner(System.in);String str=scanner.nextLine();if(str.ma

24、tches(regex)System.out.println(str+中的字符都是英文字母);,JDK1.4之后,字符串对象调用:public String replaceAll(String regex,String replacement)方法返回一个字符串,该字符串是当前字符串中所有和参数regex指定的正则表达式匹配的子字符串被参数replacement指定的字符串替换后的字符串,2.字符串的替换,举例,String result=12hello567.replaceAll(a-zA-Z+,你好);那么result的值就是“12你好567”,例10-10,public class Ex

25、ample9_10 public static void main(String args)String str=欢迎大家访问http:/了解、参观公司;String regex=(http:/|www)56?w+561w+561pAlpha+;(剔除n%sn中的网站链接信息后得到的字符串:n,str);str=str.replaceAll(regex,);System.out.println(str);,JDK1.4之后,String 类提供了一个实用的方法:public String split(String regex)字符串调用该方法时,使用参数指定的正则表达式regex做为分隔标记分

26、解出其中的单词,并将分解出的单词存放在字符串数组中,3.字符串的分解,举例,String str=1931年2月1日;String regex=D+;String d=str.split(regex);for(String s:d)System.out.println(s);,9-11,import;public class Example9_11 public static void main(String args)(一行文本:);Scanner reader=new Scanner(System.in);String str=reader.nextLine();/空格、数字和符号(!#$

27、%,String 类创建的字符串对象是不可修改的,也就是说,String字符串不能修改、删除或替换字符串中的某个字符StringBuffer类可以创建一个可以修改的字符串序列例如:一个StringBuffer对象调用append方法可以追加字符序列,例如:StringBuffer buffer=new StringBuffer(“我喜欢”);buffer.append(“玩篮球”);,9.2 StringBuffer类,9.2.1 StringBuffer对象的创建,StringBuffer类有三个构造方法:1StringBuffer()2StringBuffer(int size)3Stri

28、ngBuffer(String s),1append方法使用StringBuffer类的append方法可以将其它Java类型数据转化为字符串后,再追加到StringBuffer对象中StringBuffer append(String s)StringBuffer append(int n)StringBuffer append(Object o)StringBuffer append(long n)StringBuffer append(boolean n)StringBuffer append(float n)StringBuffer append(double n)StringBuff

29、er append(char n),9.2.2 StringBuffer类的常用方法,2public chat charAt()和public void setCharAt(int n,char ch)char charAt(int n)得到参数n指定的置上的单个字符。当前对象实体中的字符串序列的第一个位置为0,第二个位置为1,依次类推。n的值必须是非负的,并且小于当前对象实体中字符串序列的长度。setCharAt(int n,char ch)将当前StringBuffer对象实体中的字符串位置n处的字符用参数ch指定的字符替换。n的值必须是非负的,并且小于当前对象实体中字符串序列的长度,3S

30、tringBuffer insert(int index,String str)StringBuffer对象使用insert方法将参数str指定的字符串插入到参数index指定的位置,并返回当前对象的引用4public StringBuffer reverse()StringBuffer对象使用reverse()方法将该对象实体中的字符翻转,并返回当前对象的引用。,5StringBuffer delete(int startIndex,int endIndex)delete(int startIndex,int endIndex)从当前StringBuffer对象实体中的字符串中删除一个子字符

31、串,并返回当前对象的引用。这里startIndex指定了需删除的第一个字符的下标,而endIndex指定了需删除的最后一个字符的下一个字符的下标。因此要删除的子字符串从startIndex到endIndex-1。deleteCharAt(int index)方法删除当前StringBuffer对象实体的字符串中index位置处的一个字符,6StringBuffer replace(int startIndex,int endIndex,String str)replace(int startIndex,int endIndex,String str)方法将当前StringBuffer对象实体中

32、的字符串的一个子字符串用参数str指定的字符串替换。被替换的子字符串由下标startIndex 和endIndex,指定,即从startIndex到endIndex-1的字符串被替换。该方法返回当前StringBuffer对象的引用,public class Example9_12 public static void main(String args)StringBuffer str=new StringBuffer();str.append(大家好);System.out.println(str:+str);System.out.println(length:+str.length();S

33、ystem.out.println(capacity:+str.capacity();str.setCharAt(0,w);str.setCharAt(1,e);System.out.println(str);str.insert(2,are all);System.out.println(str);int index=str.indexOf(好);str.replace(index,str.length(),right);System.out.println(str);,当分析一个字符串并将字符串分解成可被独立使用的单词时,可以使用java.util包中的StringTokenizer类,该

34、类有两个常用的构造方法:StringTokenizer(String s):为字符串s构造一个分析器。使用默认的分隔标记,即空格符(若干个空格被看做一个空格)、换行符、回车符、Tab符、进纸符做分隔标记。StringTokenizer(String s,String delim):为字符串s构造一个分析器。参数dilim中的字符被作为分隔标记,9.3 StringTokenizer类,9.4.1 构造Date对象1使用无参数构造方法使用Date类的无参数构造方法创建的对象可以获取本地当前时间Date nowTime=new Date();那么,如果当前nowTime含有的日期、时间就是创建no

35、wTime对象时的本地计算机的日期和时间,9.4 Date类,2使用带参数的构造方法计算机系统将其自身的时间的“公元”设置在1970年1月1日0时(格林威治时间),可以根据这个时间使用Date的带参数的构造方法:Date(long time)来创建一个Date对象,可以使用java.text包中的DateFormat的子类SimpleDateFormat来实现日期的格式化。SimpleDateFormat有一个常用构造方法:public SimpleDateFormat(String pattern);该构造方法可以用参数pattern指定的格式创建一个对象,该对象调用:public Stri

36、ng format(Date date)方法格式化时间对象date。pattern是由普通字符和一些称作格式符组成的字符序列,9.4.2 日期格式化,Calendar类在java.util包中。使用Calendar类的static方法getInstance()可以初始化一个日历对象,如:Calendar calendar=Calendar.getInstance();然后,calendar对象可以调用方法:public final void set(int year,int month,int date)public final void set(int year,int month,int

37、date,int hour,int minute)public final void set(int year,int month,int date,int hour,int minute,int second)将日历翻到任何一个时间,当参数year取负数时表示公元前(实际世界中的公元前),9.5 Calendar类,calendar对象调用方法:public int get(int field)可以获取有关年份、月份、小时、星期等信息,参数field的有效值由Calendar的静态常量指定,9.6.1 Math类java.lang包中的Math类包含许多用来进行科学计算的类方法,这些方法可以

38、直接通过类名调用。另外,Math类还有两个静态常量,E和PI,它们的值分别是:和,9.6 Math和BigInteger类,以下是Math类的常用类方法:public static long abs(double a)返回a的绝对值。public static double max(double a,double b)返回a、b的最大值。public static double min(double a,double b)返回a、b的最小值。public static double random()产生一个0到1之间的随机数(不包括0和1)。public static double pow(d

39、ouble a,double b)返回a的b次幂。public static double sqrt(double a)返回a的平方根。public static double log(double a)返回a的对数。public static double sin(double a)返回正弦值。public static double asin(double a)返回反正弦值。,程序有时需要处理大整数,java.math包中的BigInteger类提供任意精度的整数运算。可以使用构造方法:public BigInteger(String val)构造一个十进制的BigInteger对象。该构

40、造方法可以发生NumberFormatException异常,也就是说,字符串参数val中如果含有非数字字符就会发生NumberFormatException异常,9.6.2 BigInteger类,public BigInteger add(BigInteger val)返回当前大整数对象与参数指定的大整数对象的和。public BigInteger subtract(BigInteger val)返回当前大整数对象与参数指定的大整数对象的差。public BigInteger multiply(BigInteger val)返回当前大整数对象与参数指定的大整数对象的积。public Big

41、Integer divide(BigInteger val)返回当前大整数对象与参数指定的大整数对象的商。public BigInteger remainder(BigInteger val)返回当前大整数对象与参数指定的大整数对象的余。public int compareTo(BigInteger val)返回当前大整数对象与参数指定的大整数的比较结果,返回值是1、-1或0,分别表示当前大整数对象大于、小于或等于参数指定的大整数。public BigInteger abs()返回当前大整数对象的绝对值。public BigInteger pow(int a)返回当前大整数对象的a次幂。pub

42、lic String toString()返回当前大整数对象十进制的字符串表示。public String toString(int p)返回当前大整数对象p进制的字符串表示。,BigInteger类的常用方法,可以使用java.text包中的DecimalFormat类对数字进行格式化以符合程序的要求1.格式化整数位和小数位可以使用DecimalFormat类的构造方法,并将把一个由数字“0”和“.”组成(只能有一个“.”)的字符串,传递给构造方法的参数来创建一个DecimalFormat对象,9.7 DecimalFormat类,2.整数位的分组当希望将数字的整数部分分组(用逗号分隔),比

43、如按“千”或“万”分组等,那么可以在DecimalFormat对象中的数字格式化模式前面增加分组作为前缀分组是用逗号做分隔的“”组成的字符串分组通常用于千位,但是在某些国家中它用于分隔万位。分组所给出的分组大小决定数字中从左向右每隔多少位添加一个逗号,3.格式化为百分数或千分数在DecimalFormat对象中的数字格式化模式尾加“%”,可以将数字格式化为百分数、尾加“u2030”将数字格式化为千分数,4.格式化为科学计数在DecimalFormat对象中的数字格式化模式尾加“E0”,可以将数字格式化为科学计数,5.格式化为货币值在DecimalFormat对象中的数字格式化模式尾加货币符号,

44、例如“$”“¥”,可以将数字格式化为带货币符号的串,可以根据要转化的字符串创建一个DecimalFormat对象,并将适合该字符串的格式化模式传递给该对象,9.7.2 将格式化字符串转化为数字,9.8.1 模式对象进行模式匹配的第一步就是使用Pattern类创建一个对象,称作模式对象,模式对象是对正则表达式的封装。Pattern类调用类方法compile(String regex)返回一个模式对象,其中的参数regex是一个正则表达式(有关正则表达式的知识参见前面的),称作模式对象使用的模式,9.8 Pattern与Match类,Pattern类也可以调用类方法compile(String r

45、egex,int flags)返回一个Pattern对象,参数flags可以取下列有效值:Pattern.CASE_INSENSITIVEPattern.MULTILINEPattern.DOTALLPattern.UNICODE_CASEPattern.CANON_EQ,模式对象p调用matcher(CharSequence input)方法返回一个Matcher对象m,称作匹配对象,参数input可以是任何一个实现了CharSequence接口的类创建的对象,前面学习的String类和StringBuffer类都实现了CharSequence接口,9.8.2 匹配对象,一个Matcher对

46、象m可以使用下列3个方法寻找参数input指定的字符序列中是否有和模式regex匹配的子序列(regex是创建模式对象p时使用的正则表达式)public boolean find()public boolean matches()public boolean lookingAt(),下列几个方法也是Matcher对象m常用的方法public boolean find(int start)public String replaceAll(String replacement)public String replaceFirst(String replacement),1使用默认分隔标记解析字符串

47、创建Scanner对象,并将要解析的字符串传递给所构造的对象例如,对于字符串:String NBA=I Love This Game;为了解析出NBA中的单词,可以如下构造一个Scanner对象:Scanner scanner=new Scanner(NBA);调用next()方法依次返回NBA中的单词,9.9 Scanner类,解析数字,对于数字型单词,如168,168.98等可以用nextInt()或nextDouble()方法来代替next方法。即scanner 可以调用nextInt()或nextDouble()方法将数字型单词转化为int型或double型数据返回,import ja

48、va.util.*;public class Example9_20 public static void main(String args)String cost=TV cost 876 dollar.Computer cost 2398 dollar.telephone cost 1278 dollar;Scanner scanner=new Scanner(cost);double sum=0;while(scanner.hasNext()try double price=scanner.nextDouble();sum=sum+price;System.out.println(pric

49、e);catch(InputMismatchException exp)String t=scanner.next();(总消费:+sum+元);,上例是以空格作为分隔标记的实际上,Scanner对象可以调用useDelimiter(正则表达式);方法将一个正则表达式作为分隔标记,即和正则表达式匹配的字符串都是分隔标记。,2使用正则表达式作为分隔标记解析字符串,import java.util.*;public class Example9_21 public static void main(String args)String cost=话费清单:市话费76.89元,长途花费167.38元

50、,短信费12.68元;Scanner scanner=new Scanner(cost);scanner.useDelimiter(0123456789.+);double sum=0;while(scanner.hasNext()try double price=scanner.nextDouble();sum=sum+price;System.out.println(price);catch(InputMismatchException exp)String t=scanner.next();(总通信费用:+sum+元);,java.lang包中的System类中有许多类方法,这些方法用于

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号