JAVAlession9常用类.ppt_第1页
JAVAlession9常用类.ppt_第2页
JAVAlession9常用类.ppt_第3页
JAVAlession9常用类.ppt_第4页
JAVAlession9常用类.ppt_第5页
已阅读5页,还剩51页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第9章JAVA常用类 www aicsws www aicsws 9 1内容索引 字符串常用方法 常用类字符串常用方法Object类常用方法日期 数字和货币格式正则表达式 Pattern和Matcher类进行模式匹配分解字符串 www aicsws 9 1String类 String类 字符串字面量 对象 未修改的原始字符串 使用String类的方法 可以更改字符串版本 原始字符串保持不变 www aicsws 9 1String类的构造方法 www aicsws 9 1字符串长度 字符串 长度 由length 方法确定 语法 publicintlength 返回字符串中的字符数 www aicsws 9 1字符串长度 字符串1 长度 Determinedbylength method Syntax Publicintlength Returnsnumberofcharactersinthestring Stringname JohnSmith System out println name length www aicsws 练习 想想如何将一个String对象的每个字符依次打印出来 参看JDK帮助文档 www aicsws 9 1字符串比较1 字符串1 字符串2 字符串1 字符串2 由equals 方法确定 检查组成字符串内容的字符 同一个对象 用 运算符检查 检查字符串是否指向同一个或不同的对象 www aicsws 9 1字符串比较2 演示 示例3 publicclassEquality 构造方法 protectedEquality 它演示两个字符串的比较 paramargs传递至main方法的参数 publicstaticvoidmain String args Stringstring1 newString 苹果是一种水果 Stringstring2 newString 玫瑰花是一种花 Stringstring3 newString 苹果是一种水果 System out println 字符串1 string1 System out println 字符串2 string2 System out println 字符串3 string3 字符串比较运算符的用法使用String类的方法 如equals 和 运算符 if string1 string2 System out println 字符串1和字符串2相等 else System out println 字符串1和字符串2不等 if string1 equals string3 System out println 字符串1和字符串3相等 else System out println 字符串1和字符串3不等 System out println 设置字符串1等于字符串2 string2 string1 if string1 equals string2 System out println 两个字符串相等 else System out println 两个字符串不等 www aicsws 9 1字符串比较3 www aicsws 9 1字符串比较4 演示 示例4 比较不同的字符串使用String类的方法 如equalsIgnoreCase compareTo startsWith 和endsWith publicclassStringdemo 构造方法 protectedStringdemo 这是main方法 它演示String类的比较方法 paramargs传递至main方法的参数 publicstaticvoidmain String args Stringstring1 string2 string3 string1 newString Answer string2 newString ANSWER string3 newString Question System out println 字符串A是 string1 System out println 字符串B是 string2 System out println 字符串C是 string3 Stringstring1 newString HelloWorld Stringstring2 newString Helloworld Stringstring3 newString helloworld if string1 string2 System out println 字符串1和字符串2指同一个对象 else System out println 字符串1和字符串2指不同的对象 if string1 equals string2 System out println 字符串1和字符串2的内容相同 else System out println 字符串1和字符串2的内容不同 if string1 equalsIgnoreCase string2 System out println 忽略大小写 字符串1和2的内容相同 elseif string1 equalsIgnoreCase string3 System out println 字符串1和3的内容相同 if pareTo Answer 0 System out println 按字母 字符串1与Answer的内容相同 if string1 startsWith A System out println 以A开始 www aicsws 练习 创建两个字符串比较两个字符串是否是同一个字符串对象 判断两个字符串内容是否相同 www aicsws 9 1搜索字符串1 字符串1 情形1 indexOf character 方法 找到第一个匹配 索引 0123 情形2 如果没有找到匹配 则返回 1 返回找到的第一个匹配的位置索引 www aicsws 9 1搜索字符串2 示例 示例5 搜索字符串内有无指定的字符或字符串使用String类的方法 如indexOf classSearchString 构造方法 protectedSearchString 这是main方法 它演示在字符串内搜索 paramargs传递至main方法的参数 publicstaticvoidmain String args Stringname JohnSmith System out println EmailID是 name System out println 的索引是 name indexOf System out println 的索引是 name indexOf if name indexOf name indexOf System out println 该电子邮件地址有效 else System out println 该电子邮件地址无效 www aicsws 9 1更改字符串中字符的大小写1 Hello 使用toUpperCase 方法 HELLO HELLO 使用toLowerCase 方法 hello 语法 publicStringtoUpperCase 语法 PublicStringtoLowerCase www aicsws 演示 示例7 更改字符串中字符的大小写形式使用String类的方法 如toUpperCase 和toLowerCase publicclassStringTest 构造方法 protectedStringTest 这是main方法 它演示字符串的length 和UpperCase 方法 paramargs传递至main方法 publicstaticvoidmain String args Stringname newString George System out println 姓名是 name intlength name length System out println 姓名的长度为 length 个字符 System out println 姓名用大写形式表示为 StringnameUppercase name toUpperCase System out println nameUppercase 9 1更改字符串中字符的大小写2 www aicsws 9 1StringBuffer类1 StringBuffer用于表示可以修改的字符串使用连接运算符 的字符串会自动创建字符串缓冲对象 www aicsws 9 1StringBuffer类2 www aicsws 9 1String不变性1 String类 创建后 直接修改 不变性的概念 解决方法 StringBuffer类 String的对等类 表示可增加和可编写字符的可变序列 将字符插入到字符串中间或附加到字符串末尾 www aicsws 9 1String不变性2 演示 示例8 StringBuffer类的用法使用StringBuffer类的方法append StringBuilder类除了线程不同步以外和StringBuffer类完全一样 classStringBuf 构造方法 protectedStringBuf StringBufferbuf newStringBuffer Java buf append GuideVer1 buf append 3 System out println buf StringBuilderbuild newStringBuilder String build append Builder System out println build www aicsws 9 2内容索引 字符串 常用类字符串常用方法Object类常用方法日期 数字和货币格式正则表达式 Pattern和Matcher类进行模式匹配分解字符串 www aicsws 9 2Object类 所有类的父类默认情况下 用户定义的类扩展自Object类 www aicsws 9 2equals与 equals比较内容 比较引用对象 www aicsws 9 2为什么要重写equals方法 为了实现查找 查找需要索引号 比如查找汽车的主人 我们可以使用机动车标识号来查找 这时程序需要对标识号进行比较 比如机动车标识号为8的车是张三的车 只有当标识号能够进行比较时才能实现查找功能 程序EqualsTest2 java Car2 java演示 www aicsws 9 2HashCode www aicsws 9 2为什么要重写HashCode方法 如果两个对象被认为是相等的 它们的散列码也必须相等 不然永远不能找到对象 因为Object类中默认散列码方法总是为每个对象产生一个惟一号 www aicsws 9 2为什么要重写HashCode方法 只要重写了equals方法 就需要重写hashCode方法 publicinthashCode return x 17 www aicsws 9 3内容索引 日期格式 常用类字符串常用方法Object类常用方法日期 数字和货币格式正则表达式 Pattern和Matcher类进行模式匹配分解字符串 www aicsws 9 3Date类 Date类表示日期和时间提供操纵日期和时间各组成部分的方法Date类的最佳应用之一是获取系统当前时间 www aicsws 9 3Date类构造方法 演示 示例1 voiddisplay StringstrDate strTime System out println 今天的日期是 objDate longtime objDate getTime System out println 自1970年1月1日起 以毫秒为单位的时间 GMT time strDate objDate toString 提取GMT时间strTime strDate substring 11 19 按小时 分钟和秒提取时间System out println strTime Date对象用于输出日期 DateTimeDisplay objDate newDate 使用getTime 方法从Date对象获取时间 www aicsws 9 3DateFormat格式化日期 voiddisplay StringstrDate strTime longtime objDate getTime timeSystem out println From1970 1 1time time datestrDate objDate toString System out println Today strDate standardtimestrTime strDate substring 11 19 System out println Standardtime strTime formatStringmyString DateFormat getDateInstance format objDate System out println Afterformatteddate myString www aicsws 练习 创建一个Date类型对象 提取时间信息格式化Date类型对象 www aicsws 9 3Calendar类 www aicsws 9 3Calendar类 根据给定的Date对象 Calendar类可以以YEAR和MONTH等整型的形式检索信息它是抽象的 因此不能像Date类一样实例化GregorianCalendar 是Calendar的子类 实现Gregorian形式的日历 演示 示例2 CalendarComponents objCalendar Calendar getInstance 使用getInstance 方法获取Calendar类的实例 voiddisplay 显示Date和Time的组成部分System out println nDate和Time的组成部分 System out println 月 objCalendar get Calendar MONTH System out println 日 objCalendar get Calendar DATE System out println 年 objCalendar get Calendar YEAR System out println 小时 objCalendar get Calendar HOUR System out println 分钟 objCalendar get Calendar MINUTE System out println 秒 objCalendar get Calendar SECOND 向当前时间添加30分钟 然后显示日期和时间objCalendar add Calendar MINUTE 30 DateobjDate objCalendar getTime System out println n向当前时间添加30分钟后的日期和时间 n System out println objDate Calendar类定义某些用于获取或设置Calendar组成部分的整型 Math类别的常数 9 3Math类 Math类别的三角函数方法 9 3Math类 Math类别的其它常用方法 EX 9 11 java 9 3Math类 www aicsws 已知java lang Math类中有random 方法 random 返回带正号的double值 大于或等于0 0 小于1 0 写出随机生成19到41之间的偶数的表达式 获得n m之间的任意数表达式 int Math random m n n 9 3Math类练习 www aicsws 9 4内容索引 正则表达式 常用类字符串常用方法Object类常用方法日期 数字和货币格式正则表达式 Pattern和Matcher类进行模式匹配分解字符串 www aicsws 9 4正则表达式 正则表达式是含有具有特殊意义字符 元字符 的字符串 也称模式 与一个模式匹配的字符串称为模式匹配字符串 元字符模式 任何一个字符 d 0至9的任何一个数字 D 任何一个非数字字符 s 空格类字符 S 非空格类字符 w 可用于标识符的字符 W 不能用于标识符的字符 方括号模式 abc abc任意 abc 除abc任意 a d a至d任意 限定符模式X 0次或1次X 0次或多次X 1次或多次X n 恰好n次X n m n至m次 dth 匹配 4th 9th www aicsws 9 4模式匹配 建立模式对象 Ppile Stringpatter Patternp Ppile a d 包java util regex 建立匹配对象 Matcher matcher CharSequencemstr Stringmstr a1a2a3 Matcherm p matcher mstr www aicsws 9 4Matcher类常用方法 booleanfind 寻找匹配的下一子序列booleanfind intstart 若find 返回true start 和end 可以得到该匹配模式子序列在mstr中的开始位置和结束位置 group 返回一个匹配模式的字符串对象 www aicsws 0 6 like126 11 hate314 20 like5620 25 hate4 importjava util regex publicclassZstr publicstaticvoidmain String args Stringpatter like w 2 hate w 1 Stringmstr like12hate3no9like56hate4 Patternp Ppile patter Matcherm p matcher mstr while m find Stringstr m group System out print m start m end System out println str 演示 示例5 9 4Matcher类示例 www aicsws 练习 已知正则表达式 0 xX 0 9a fA F 和查找源 120 x0 x120Xf0 xg 执行start方法后的结果是什么 www aicsws www aicsws 9 5 常用类字符串常用方法Object类常用方法日期 数字和货币格式正则表达式 Pattern和Matcher类进行模式匹配分解字符串 www aicsws 9 5Scanner类 importjava util classScanIn publicstaticvoidmain String args System out print input try Scanners newScanner System in Stringtoken do token s findInLine args 0 System out println found token while token null catch Exceptione System out println scanexc 演示 示例6 www aicsws 9 5Scanner类 输出结果 www aicsws 练习 已知正则表达式 0 xX 0 9a fA F 和查找源 120 x0 x120Xf0 xg 使用Scanner类中的方法查找 www aicsws 9 5StringTokenizer类 分解字符串成可被独立使用的单词 java util包 创建StringTokenizer对象StringTokenizer Strings StringTokenizer Stri

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论