实验九 字符串
【开发语言及实现平台或实验环境】
Windows2000 或XP,JDK1.6与Eclipse 【实验目的】
1. 掌握String中的常用方法与作用
2. 掌握StringBuffer中的常用方法与作用
【实验要求】
1. 掌握String和StringBuffer中的常用方法与作用。 2. 理解String和StringBuffer的区别
【实验内容】
1 String 是不可变的字符串 字符串操作练习
public class MathRandomTest{ public static void main(String [] args) {
String s=\ System.out.println(s); System.out.println(\ System.out.println(\ System.out.println(\ String alphabet=\ System.out.println(alphabet); System.out.println(\ System.out.println(\ System.out.println(\ String sbis=\ System.out.println(sbis); String sbislc=sbis.toLowerCase(); System.out.println(sbislc); String sbisup=sbis.toUpperCase(); System.out.println(sbisup); String first=\ String last=\ System.out.println(first+last); System.out.println(first+\ String str=\ System.out.println(str); int i=str.indexOf('s'); System.out.println(\ int j=str.indexOf('s',i+1); System.out.println(\ int k=str.indexOf('s',j+1); System.out.println(\
30
k=str.lastIndexOf('s'); System.out.println(\ System.out.println(str.substring(k)); String inventor=\ System.out.println(inventor); System.out.println(inventor.replace('B','C')); System.out.println(inventor); int n=44; System.out.println(\ String strn=String.valueOf(n); System.out.println(\ String today=\ String todaysDayString=today.substring(4,6); int todaysDayInt=Integer.parseInt(todaysDayString); int nextWeeksDayInt=todaysDayInt+7; String nextWeek=today.substring(0,4)+nextWeeksDayInt+today.substring(6); System.out.println(\ System.out.println(\ System.out.println(\ System.out.println(\ } }
4. 字符串(String)对象一旦创建,其内容不能再被修改 (read-only)。StringBuffer
对象的内容是可以被修改的除了字符的长度之外,还有容量的概念
【完成实验项目】
1. 编写一个java程序,完成以下功能:
(1) 声明一个名为s的字符串,并使它的内容为”Call me Ishmael.”; (2) 打印整个字符串
(3) 使用length()方法打印字符串的长度
(4) 使用charAt()方法打印字符串的第一个字符
(5) 使用chanrAt()和length()方法打印字符串的最后一个字符
(6) 使用indexOf()方法和substring()方法打印字符串中的第一个单词
31
2设有中英文单词对照表,输入中文单词,显示相应英文单词,输入英文单词显示相应中文单词。 String[][] x = { { \, \好\ }, { \, \坏\ }, { \, \工作\ } };
32
实验十 集合框架编程
【开发语言及实现平台或实验环境】
Windows2000 或XP,JDK1.6与Eclipse 【实验目的】
1.掌握List、Collection、Set、Map、HashTable等常用数据结构的使用 2.进一步熟悉基于接口编程的特点
3. 掌握迭代器的用法 【实验要求】
1. 掌握List、Collection、Set、Map、HashTable等常用数据结构的使用。 2. 掌握迭代器编程 【实验内容】
1 Collection接口定义了存取一组对象的方法,其子接口Set 和List分别定义了存储方式: Set中的数据对象没有顺序且不可以重复。List中的数据对象有顺序且可以重复。Map接口定义料存取“键(key)—值(value)映射对”的方法。
2 Collection接口提供的方法:
3 Set 接口提供的方法与Collection接口中的方法一样,没有添加新的方法,只是Set容器中装的元素无顺序和不可重复,例1
import java.util.HashSet; import java.util.Set;
33
public class TestSet {
public static void main(String[] args) { Set s1 = new HashSet(); Set s2 = new HashSet(); s1.add(\); s1.add(\); s1.add(\); s2.add(\); s2.add(\); s2.add(\);
Set sn = new HashSet(s1); sn.retainAll(s2);
Set su = new HashSet(s1); su.addAll(s2);
System.out.println(sn); System.out.println(su);
} }
4 List接口提供的方法除Collection接口中的方法外,还添加了一些新的方法。
在java.util.Collections提供了一些对于List常用的算法,这些方法都是静态的,例2
import java.util.Collections; import java.util.LinkedList; import java.util.List;
public class TestList { public static void main(String[] args){ List l1 = new LinkedList(); List l2 = new LinkedList(); for(int i=0;i<10;i++){
l1.add(\ + i);
34
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库java实验指导书(7)在线全文阅读。
相关推荐: