java程序设计ppt教学课件-第四章 常用类

上传人:aa****6 文档编号:54721927 上传时间:2018-09-18 格式:PPT 页数:68 大小:240KB
返回 下载 相关 举报
java程序设计ppt教学课件-第四章 常用类_第1页
第1页 / 共68页
java程序设计ppt教学课件-第四章 常用类_第2页
第2页 / 共68页
java程序设计ppt教学课件-第四章 常用类_第3页
第3页 / 共68页
java程序设计ppt教学课件-第四章 常用类_第4页
第4页 / 共68页
java程序设计ppt教学课件-第四章 常用类_第5页
第5页 / 共68页
点击查看更多>>
资源描述

《java程序设计ppt教学课件-第四章 常用类》由会员分享,可在线阅读,更多相关《java程序设计ppt教学课件-第四章 常用类(68页珍藏版)》请在金锄头文库上搜索。

1、1,第4章 常用类,2,本章导读,基本数据类型的封装类 Object类 String类 StringBuffer类 StringTokenizer类 Math类和BigInteger类 Date类和Calendar类 LinkedList和HashSet泛型类,3,本章导读,基本数据类型的封装类 Object类 String类 StringBuffer类 StringTokenizer类 Math类和BigInteger类 Date类和Calendar类 LinkedList和HashSet泛型类,4,基本数据类型的封装类,整数类型的封装类,5,基本数据类型的封装类,浮点类型的封装类,6,基本

2、数据类型的封装类,字符类型的封装类,布尔类型的封装类,7,Integer类,MAX_VALUE和MIN_VALUE Integer类的类成员变量,表示int数据的最大值和最小值 在长度为100的整数数组array中找出最大值 int max=Integer.MIN_VALUE; for(int i=0;i100;i+)if(maxarrayi)max=arrayi;,8,Integer类,构造方法 public Integer(int value) 用一个int类型的量生成一个Integer对象 public Integer(String s) 用一个字符串生成一个Integer对象,9,In

3、teger类,非静态转换方法 public int intValue() 将Integer对象转换成int类型的值 Integer i=new Integer(“05”); int value=i.intValue(); public String toString() 将Integer对象转换成一个字符串 Integer i=new Integer(5); String s=i.toString();,10,Integer类,静态转换方法 public static int parseInt(String s) 把字符串转换成int类型的值 int i=Integer.parseInt(“-

4、05”); public static Integer valueOf(String s) 把字符串转换成Integer对象 Integer i=Integer.valueOf(“2007”); int value=i.intvalue();,11,基本数据类型的封装类举例,其他基本数据类型的封装类的使用方法与Integer类似。 课本p99,程序4-1 TestCapsulation.java,12,Character类,构造方法 public Character(char c) 用一个char类型的量生成一个Character对象 Character类中的常用类方法 public stat

5、ic boolean isDigit(char ch) 如果ch是数字字符,返回true,否则返回false public static boolean isLetter(char ch) 如果ch是字母,返回true,否则返回false public static boolean isLetterOrDigit(char ch) 如果ch是数字字符或字母,返回true,否则返回false,13,Character类,Character类中的常用类方法 public static boolean isLowerCase(char ch) 如果ch是小写字母,返回true,否则返回false p

6、ublic static boolean isUpperCase(char ch) 如果ch是大写字母,返回true,否则返回false public static char toLowerCase(char ch) 返回ch的小写形式 public static char toUpperCase(char ch) 返回ch的大写形式 public static boolean isSpaceChar(char ch) 如果ch是空格,返回true,否则返回false,14,public class Example public static void main(String args ) c

7、har a =a,b,c,D,E,F;for(int i=0;ia.length;i+) if(Character.isLowerCase(ai) ai=Character.toUpperCase(ai);else if(Character.isUpperCase(ai) ai=Character.toLowerCase(ai);for(int i=0;ia.length;i+)System.out.printf(“%6c“,ai); ,15,本章导读,基本数据类型的封装类 Object类 String类 StringBuffer类 StringTokenizer类 Math类和BigInte

8、ger类 Date类和Calendar类 LinkedList和HashSet泛型类,16,Object类,主要方法 protected Object clone() 生成当前对象的一个备份,并返回这个复制对象的Object引用 public boolean equals(Object obj) 比较当前对象和另外一个Object对象是否相等,如果相等则返回true,否则返回false,17,Object类,主要方法 public String toString() 返回对象有关内存的信息 public final Class getClass() 获得当前所属类的信息,返回一个Class类的

9、对象,18,Object类常用方法举例,public boolean equals(Object obj) 课本p100,程序4-2,TestEquals.java public String toString() 课本p101,程序4-3,TestToString.java public final Class getClass() 课本p103,程序4-5,TestGetClass.java,19,Object类常用方法举例,protected Object clone() 浅复制 课本p103,程序4-6,TestClone.java 深复制 课本p104,程序4-7,TestClone

10、2.java,20,本章导读,基本数据类型的封装类 Object类 String类 StringBuffer类 StringTokenizer类 Math类和BigInteger类 Date类和Calendar类 LinkedList和HashSet泛型类,21,String类,构造方法 String(String original) String s=new String(“We are students.”); String tom=new String(s); String(char value) char a =b,o,y; String s=new String(a); String

11、(char value,int offset,int count) 利用字符数组value的一部分构造字符串对象,从offset开始共计count个字符 char a=a,b,c,d,e; String s=new String(a,3,4);,不能更改字符串的内容,22,字符串常量,字符串常量是对象,可以把它的引用赋值给字符串变量 String s1,s2; s1=“How are you!”; s2=“How are you!”; s1和s2具有相同的引用,0xAb28,0xAb28,How are you!,s1,s2,23,String类的常用方法,(1)public int leng

12、th() 返回当前字符串的长度 例如: String s=“We are students”; String tom=new String(“我是学生”); int n1=s.length(); int n2=tom.length(); int n3=“Learning Java”.length();,24,String类的常用方法,(2)public boolean equals(String s) 比较两个字符串的内容是否完全相同 例如: String tom=“we are students.”; String boy=new String(“We are students.”); St

13、ring jerry=new String(“we are students.”); tom.equals(boy)的值是false tom.equals(jerry)的值是true,25,String类的常用方法,(3)public boolean contains(String s) 比较当前字符串是否包含字符串s 例如: String tom=“we are students.”; tom.contains(“stu”)的值是true tom.contains(“We”)的值是false,26,String类的常用方法,(4)public int compareTo(String s)

14、将当前字符串按字典序与字符串s比较大小 如果当前字符串与s相同,返回0;如果当前字符串大于s,返回正值;否则返回负值 例如: String str=“abcde”; pareTo(“boy”)小于0 pareTo(“aba”)大于0 pareTo(“abcde”)等于0,27,String类的常用方法,(5)public String concat(String s) 与字符串连接符“+”是等价的 例如: String s1=“We”.concat(“are students.”); 等价于 String s1=“We ”+“are students.”;,28,String类的常用方法,(6

15、)public String substring(int start, int end) 得到当前字符串的子串,这个子串从当前字符串的start开始,截取到end(不包括end)之间的字符序列 例如: String tom=“I like tom.”; String s=tom.substring(2,5); s为“lik” “to Java world”.substring(3,7); 返回结果为“Java”,29,String类的常用方法,(7)public int indexOf(String s) 从当前字符串的头开始检索字符串s,并返回首次出现s的位置,如果没有检索到s,则返回-1

16、例如: String tom=“It is a good cat.”; tom.indexOf(“a”); 值为6 tom.indexOf(“good”); 值为8 tom.indexOf(“Good”); 值为-1,30,String类的常用方法,(8)public String split(String regex) 以regex为分隔符把当前字符串分割成若干个子字符串,以字符串数组的形式返回 例如: String tom=“It is a good cat.”; String a=tom.split(“ “); for(int i=0;ia.length;i+)System.out.println(ai);,31,String类的常用方法,(9)public char toCharArray() 将当前字符串中的所有字符按顺序存放在一个字符数组中 例如: Scanner reader=new Scanner(System.in); String s=reader.nextLine(); Char a =s.toCharArray();,

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号