2 1 Collection Map List Set Hash Set

  • Slides: 86
Download presentation

2. 1 体系结构 Collection Map List Set Hash. Set Vector Array. List Hast. Map

2. 1 体系结构 Collection Map List Set Hash. Set Vector Array. List Hast. Map Hashtable 14

2. 2 List、Set、Map 通用方法 : boolean contains(Object a) boolean equals(Object a) Iterator iterator() int

2. 2 List、Set、Map 通用方法 : boolean contains(Object a) boolean equals(Object a) Iterator iterator() int size() void clear() boolean add(Object a) 15

2. 3 示例 public static void main(String[] args){ Array. List list = new Array.

2. 3 示例 public static void main(String[] args){ Array. List list = new Array. List(); list. add("苹果"); list. add("香蕉"); list. add("梨子"); list. add("香蕉"); // 允许重复 list. add("菠萝"); list. remove(“菠萝”); // 删除元素 System. out. println(list); // 元素的顺序与加入的顺序有关 } 元素是可以重复的 17

2. 3 示例 public class Iterator. Demo { public static void main(String[] args) {

2. 3 示例 public class Iterator. Demo { public static void main(String[] args) { Array. List list = new Array. List(); for(int i=0; i<=10; i++){ list. add(new Integer(i)); } Iterator it = list. iterator(); while(it. has. Next()){ System. out. println(it. next()); } } } 18

3. 1 字符串长度 随机输入你心中想到的一个名字,然后输出它的字 符串长度 package s 1 java. sg. chap 9; import java.

3. 1 字符串长度 随机输入你心中想到的一个名字,然后输出它的字 符串长度 package s 1 java. sg. chap 9; import java. util. *; public class Hello. Accp 1 { public static void main(String[ ] args) { 要想打印输出引号 System. out. print("请随机输入你想到的名字: "); (”),必须在字符串 Scanner input = new Scanner(System. in); 中使用转义序列” String name = input. next(); System. out. println("这个名字的长度是: " " + name. length()+ "" "); } 调用字符串的length()方法可 以获得字符串的长度

3. 2 字符串比较4 -3 使用equals. Ignore. Case()方法解决 if(fav. Course 1. equals. Ignore. Case(fav. Course

3. 2 字符串比较4 -3 使用equals. Ignore. Case()方法解决 if(fav. Course 1. equals. Ignore. Case(fav. Course 2)){ System. out. println("最喜欢的课程相同"); }else{ System. out. println("最喜欢的课程不相同"); } 比较时忽略大小写形式

3. 2 字符串比较 结合to. Upper. Case()方法解决 if(fav. Course 1. to. Upper. Case(). equals(fav. Course

3. 2 字符串比较 结合to. Upper. Case()方法解决 if(fav. Course 1. to. Upper. Case(). equals(fav. Course 2. to. Upper. Case())){ System. out. println("最喜欢的课程相同"); }else{ System. out. println("最喜欢的课程不相同"); } Java 使用 to. Upper. Case( ) 方法 JAVA Java 使用 to. Lower. Case( ) 方法 java

3. 3 字符串连接 令狐冲的Java成绩如表所示,输出他的成绩单 学科 成绩 SQL 80 Java 90 public static void main(String[

3. 3 字符串连接 令狐冲的Java成绩如表所示,输出他的成绩单 学科 成绩 SQL 80 Java 90 public static void main(String[ ] args) { HTML 86. 7 int sql. Score = 80; //sql成绩 数值型变量自 int java. Score = 90; //java成绩 动转换成 double html. Score = 86. 7; //html成绩 String类型 //创建成绩单 String score. Sheet = = "SQL: t" "SQL: " ++sql. Score Java: " + java. Score String sql. Score++" "n. Java: t" + java. Score ++""n. HTML: t" HTML: " + html. Score; “+”连接字符串 //打印成绩单 System. out. println("*****令狐冲成绩单*****"); System. out. println(score. Sheet); }

3. 3 字符串连接 • 方法 1:使用“+” • 方法 2:使用String类的concat()方法 String s = new String("你好,");

3. 3 字符串连接 • 方法 1:使用“+” • 方法 2:使用String类的concat()方法 String s = new String("你好,"); String name = new String("张三!"); String sentence = s. concat(name); System. out. println(sentence); 输出结果 你好,张三! A. concat(B): B字符串将被连接到A 字符串后面

3. 4 字符串常用提取方法 方法 说明 public String substring(int index) 提取从位置索引开始的字符串部分 public String substring(int beginindex,

3. 4 字符串常用提取方法 方法 说明 public String substring(int index) 提取从位置索引开始的字符串部分 public String substring(int beginindex, int endindex) 提取beginindex和endindex之间的 字符串部分 public String trim() 返回一个前后不含任何空格的调用字 符串的副本 beginindex: 字符串的位置从0开始算; endindex: 字符串的位置从1开始算

3. 4 字符串常用提取方法 作业提交系统 //检查Java文件名 //�� 你的� 箱格式 int index = file. Name. last.

3. 4 字符串常用提取方法 作业提交系统 //检查Java文件名 //�� 你的� 箱格式 int index = file. Name. last. Index. Of(". "); if (email. index. Of('@') !=- 1 && email. index. Of('. ') > if(index!=-1 && index!=0 && file. Name. substring(index+1, email. index. Of('@')){ file. Name. length()). equals("java")){ email. Correct = true; }else{ file. Correct = true; }else{ System. out. println("Email无效。"); System. out. println("文件名无效。"); } }

3. 4 小结 如果要打印输出“小鱼儿”,应填入的代码是什么? String word = "Hello, "; word = word. trim(); String

3. 4 小结 如果要打印输出“小鱼儿”,应填入的代码是什么? String word = "Hello, "; word = word. trim(); String s = word. concat("小鱼儿!"); int index 1 = s. index. Of(', '); int index 2 = s. index. Of('!'); System. out. println(s. substring(______, _______)); index 1+1 index 2

3. 5 字符串与基本数据的相互转化 • 使用java. lang包中的Byte、Short、Integer 、 Long、Float、Double类调相应的类方法: public static public static byte parse.

3. 5 字符串与基本数据的相互转化 • 使用java. lang包中的Byte、Short、Integer 、 Long、Float、Double类调相应的类方法: public static public static byte parse. Byte(String s) throws Number. Format. Exception short parse. Short(String s) throws Number. Format. Exception short parse. Int(String s) throws Number. Format. Exception long parse. Long(String s) throws Number. Format. Exception float parse. Float(String s) throws Number. Format. Exception double parse. Double(String s) throws Number. Format. Exception 可以将“数字”格式的字符串,转化为相应的基本数据类型。

3. 5 字符串与基本数据的相互转化 • 数字转换为字符串 • • Public static String value. Of(int n) Public

3. 5 字符串与基本数据的相互转化 • 数字转换为字符串 • • Public static String value. Of(int n) Public static String value. Of(float n) 例如: String str=String. value. Of(123. 56)

3. 5 例子 public class Test 10 { public static void main(String args[]) {

3. 5 例子 public class Test 10 { public static void main(String args[]) { byte d[]="你我他". get. Bytes(); System. out. println("数组d的长度是(一个汉字占两个字节 ): "+d. length+"个字节"); String s=new String(d, 0, 2); System. out. println(s); } }

3. 6 String. Buffer • String. Buffer:String增强版 • String. Buffer声明 创建空字符串 String. Buffer sb

3. 6 String. Buffer • String. Buffer:String增强版 • String. Buffer声明 创建空字符串 String. Buffer sb = new String. Buffer(); String. Buffer sb = new String. Buffer("aaa"); • String. Buffer的使用 sb. to. String(); //转化为String类型 sb. append("**"); //追加字符串 创建一个变量存储 字符串aaa

3. 6 String. Buffer的用武之地 从控制台接收课程信息,不断累加直到输入“#”键 结束,并输出全部课程信息 //声明课程信息字符串 String. Buffer course = new String. Buffer();

3. 6 String. Buffer的用武之地 从控制台接收课程信息,不断累加直到输入“#”键 结束,并输出全部课程信息 //声明课程信息字符串 String. Buffer course = new String. Buffer(); System. out. println("请输入S 1课程信息: "); Scanner sc = new Scanner(System. in); 定义String. Buffer的实 例,存储课程字符串 //循环从键盘接收字符串 String input; while(!(input = sc. next()). equals("#")){ course. append(input); 循环追加字符,使用 course. append("n"); String. Buffer,效率高! } System. out. println("s 1的课程信息是:" + course);

例子 5. 12 String regex="\w{1, }@\w{1, }56\w{1, }" ; String str 1="zhangsan@sina. com"; String

例子 5. 12 String regex="\w{1, }@\w{1, }56\w{1, }" ; String str 1="zhangsan@sina. com"; String str 2="li@si@dl. cn"; if(str 1. matches(regex)) { System. out. println(str 1+"是一个Email地址"); } else { System. out. println(str 1+"不是一个Email地址"); } if(str 2. matches(regex)) { System. out. println(str 2+"是一个Email地址"); } else { System. out. println(str 2+"不是一个Email地址"); }

定义类 public class Student { 成员变量 String name; //姓名 int age; //年龄 String class.

定义类 public class Student { 成员变量 String name; //姓名 int age; //年龄 String class. No; //班级 String hobby; //爱好 成员方法 //输出信息方法 public void show(){ System. out. println(name + "n年龄:" + age + "n就读于:" + class. No + "n爱好:" + hobby); } }

创建和使用对象 创建“张浩”对象 public class Initial. Student { 创建对象 public static void main(String args[]){ Student

创建和使用对象 创建“张浩”对象 public class Initial. Student { 创建对象 public static void main(String args[]){ Student student = new Student(); student. name = "张浩"; 给每个属性赋值 student. age = 10; student. class. No = "S 1班"; student. hobby = "篮球"; 调用方法 student. show(); } }

方法调用 public class Auto. Lion { String color = "黄色"; //颜色 /*跑*/ public class

方法调用 public class Auto. Lion { String color = "黄色"; //颜色 /*跑*/ public class Test. Lion { public void run(){ public static void main(String[ ] args) { System. out. println("正在以 0. 1米/秒的速度向前奔跑。"); Auto. Lion lion = new Auto. Lion(); } System. out. println(lion. show. Lion()); /*叫*/ lion. run(); public String bark(){ System. out. println(lion. bark()); String sound = "大声吼叫" ; } return sound; } } 在main()方法中 /*获得颜色*/ 调用类的方法 public String get. Color(){ return color; 在类的方法中调用 } 该类另一个方法 /*显示狮子特性*/ public String show. Lion(){ return "这是一个" + get. Color() + "的玩具狮子!"; } }

常见错误 4 -1 public class Student{ public void show. Info(){ return "我是一名学生"; } }

常见错误 4 -1 public class Student{ public void show. Info(){ return "我是一名学生"; } } 方法的返回类型为void,方法中不能有return返回值!

常见错误 4 -2 public class Student{ public double get. Info(){ double weight = 95.

常见错误 4 -2 public class Student{ public double get. Info(){ double weight = 95. 5; double height = 1. 69; return weight, height; } } 方法不能返回多个值!

常见错误 4 -3 public class Student{ public String show. Info(){ return "我是一名学生"; public double

常见错误 4 -3 public class Student{ public String show. Info(){ return "我是一名学生"; public double get. Info(){ double weight = 95. 5; double height = 1. 69; return weight; } } } public class Student{ public String show. Info(){ return "我是一名学生"; } public double get. Info(){ double weight = 95. 5; double height = 1. 69; return weight; } } 多个方法不能相互嵌套定义!

常见错误 4 -4 public class Student{ int age=20; if(age<20){ System. out. println("年龄不符合入学要求!"); } public

常见错误 4 -4 public class Student{ int age=20; if(age<20){ System. out. println("年龄不符合入学要求!"); } public void show. Info(){ return "我是一名学生"; } } 不能在方法外部直接写程序逻辑代码!

如何使用带参数的方法 3 -1 • 定义带参数的方法 public class Zhazhi{ public String zhazhi ( String fruit

如何使用带参数的方法 3 -1 • 定义带参数的方法 public class Zhazhi{ public String zhazhi ( String fruit ) { String juice = fruit + "汁"; return juice; } 参数列表: (数据类型 参数 1,数 } 据类型 参数 2…) • 调用带参数的方法 调用方法,传递的参数要 /*调用zhazhi方法*/ 与参数列表一一对应 Zhazhi my. Zhazhi = new Zhazhi(); String my. Fruit = "苹果"; String my. Juice = my. Zhazhi. zhazhi(my. Fruit); System. out. println(my. Juice);

如何使用带参数的方法 3 -3 调用带参数的方法 实参列表 对象名. 方法名(实参1, 实参2, ……,实参n) 先实例化对象, public static void main(String[]

如何使用带参数的方法 3 -3 调用带参数的方法 实参列表 对象名. 方法名(实参1, 实参2, ……,实参n) 先实例化对象, public static void main(String[] args) { 再使用方法 Customer. Biz st = new Customer. Biz(); Scanner input = new Scanner(System. in); for(int i=0; i<5; i++){ System. out. print(“请输入客户的姓名:"); String new. Name = input. next(); st. add. Name(new. Name); } st. show. Names(); 实参的类型、数量、顺序 } 都要与形参一一对应

常见错误 4 -1 //方法定义 public boolean search. Name(int start , int end , String

常见错误 4 -1 //方法定义 public boolean search. Name(int start , int end , String name){ //方法体 } //方法调用 String s="开始"; int e=3; String name="张三"; boolean flag=对象名. search. Name(s , e , name); 形参和实参数据类型不一致!

常见错误 4 -2 //方法定义 public boolean search. Name(int start, int end, String name){ //方法体

常见错误 4 -2 //方法定义 public boolean search. Name(int start, int end, String name){ //方法体 } //方法调用 int s=1; int e=3; boolean flag= 对象名. search. Name(s, e); 形参和实参数量不一致!

常见错误 4 -3 //方法定义 public boolean search. Name(int start, int end, String name){ //方法体

常见错误 4 -3 //方法定义 public boolean search. Name(int start, int end, String name){ //方法体 } //方法调用 int s=1; int e=3; String name="张三"; 对象名. search. Name(s, e, name); 调用方法后没有对返回值作任何处理!

巩固练习 1. 在以下Java程序类中,语法正确的是()。 A) public int uf. Test(int num) int sum=num+100; return sum; }

巩固练习 1. 在以下Java程序类中,语法正确的是()。 A) public int uf. Test(int num) int sum=num+100; return sum; } B) public String uf. Test(int num) int sum=num+100; return sum; } C) public void uf. Test(int num) int sum=num+100; return sum; } D) public int uf. Test(int num) int sum=num+100; }

巩固练习 1. 在Java中,类Test的源代码如下所示,改程序的编译运行结果是() 。 public class Test(){ public int add(int a, intb){ return a+b;

巩固练习 1. 在Java中,类Test的源代码如下所示,改程序的编译运行结果是() 。 public class Test(){ public int add(int a, intb){ return a+b; } public int length(inta, int b){ return add(a, b)*2; } public static void main(String[] args){ Test test=new Test(); System. out. println("结果为:“ + test. length(10, 20)); } } A)第 6行出现编译错误,提示无法找到add方法 B)第 10行出现编译错误,提示无法将int类型自动转换成String类型 C)输出:结果为: 60 D)输出:结果为:null