资源预览内容
第1页 / 共17页
第2页 / 共17页
第3页 / 共17页
第4页 / 共17页
第5页 / 共17页
第6页 / 共17页
第7页 / 共17页
第8页 / 共17页
第9页 / 共17页
第10页 / 共17页
亲,该文档总共17页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
/*String 的特点: 1.是类而不是类型. 2.为不可变对象,一旦被创建,就不能修改它的值. 对于已经存在的 String 对象的修改都是重新创建一个新的对象,然后把新的值 保存进去. String 是 final 类,即不能被继承. 三者的区别: 1.StringBuffer 也是对一串字符进行操作,但是可变类。 StringBuider 也是对一串字符进行操作,也是可变类。 2.三者在执行速度方面的比较:StringBuilder StringBuffer String 下面这个程序经过运行 time1=20043,time2=7,time3=3。 */ class Demo public static void main(String args) String tempstr = “abcdefghijklmnopqrstuvwxyz“; int times = 10000; double start1 = System.currentTimeMillis(); String str = “; for (int i = 0; i max) max = stui .sJava; if (stui.sJava 10) | (int2 10) )NumberRangeException e = new NumberRangeException(“Numbersare not within the specified range.“); throw e; /抛出自定义异常对象 NumberRangeExceptione answer = int1 *int2; catch (NumberFormatException e) /捕获异常对象 NumberRangeException e System.out.println( e.toString() ); /*toString()是一个方法,它的作用是将变量由其它类型转变为字符 串类型。这里 System.out.println(e.toString()就是调用系统输入 (System.Out)的 println()方法,这个方法需要传入一个字符串类型的参数。 这时,e.toString(),即把捕获的错误异常转变为字符串类型,传给 println()方法,用以输入。若出现错误被 catch 到之后输出错误信息*/ return answer; / 在调用方法 getAnswer 中捕获异常 public void getAnswer() String answerStr; try /测试 1:将 num1、num2 的中的数字设置为小于 0 或大于 10,查看捕获异常结果。 String num1=“13“; String num2=“15“; int answer = CalcAnswer(num1, num2); /抛出异常对象 NumberRangeException e answerStr = String.valueOf(answer); /将 int 型的 answer 转换成字符串类型 catch (NumberRangeException e) /捕获异常对象 NumberRangeException e answerStr = e.getMessage(); System.out.println(answerStr); public static void main(String args) NumberRangeException t1 = new NumberRangeException(“test“); t1.getAnswer(); 机房作业/*编程实现 Student 类,其包含成员变量 name、age、classNum, 新建初始化 3 个 Student 对象,添加到集合类 HashSet 对象中, 并在屏幕上输出 3 个 Student 对象的属性值;为了简便起见, 可以在 Student 类中改写方法 toString, 输出格式为“姓名:xxx,年龄:xxx,班级:xxx” 。*/ import java.util.*; class Student Student() public String toString() return (“姓名“+name+“,年龄“+age+“,班级“+classNum); Student(String name,int age,String classNum) this.name=name; this.age=age; this.classNum=classNum; private String name,classNum; private int age; public class IteratorTest public static void main(String args) Student t1=new Student(“sally“,15,“208“); Student t2=new Student(“jane“,16,“209“); Student t3=new Student(“jenny“,17,“210“); Set s=new HashSet(); s.add(t1); s.add(t2); s.add(t3); Iterator it=s.iterator(); while(it.hasNext() System.out.println(it.next().toString(); /*有一字符串数组 “bb”, “aa”, “cc”, “dd”, “ee”, “bb”, “ee”, “dd”, 编写程序去除字符串组中重复的字符串,即在新的字符串没有重复字符串。 (要求:使用 List 实现 用方法 contains()判断是否有重复字符串)*/ import java.util.*; public class ListTest public static void main(String args) String s=new String “bb“,“aa“,“cc“,“dd“,“ee“,“bb“,“ee“,“dd“; List list=new LinkedList(); for(int i=0;i m=new TreeMap();int a=new int 100; for(int i=0;i s=new TreeSet(); int a=3,b=5; for(int i=0;i m=new TreeMap();int a=new int 100; for(int i=0;iList=new ArrayList(); int count=0; for(int i=1;i=500;i+)/初始化列表 List.add(i); while(List.size()!=1)/判断是否为空 for(int i=0;iList.size();i+) count+; if(count=3) count=0; System.out.println(List.get(i);/*输出列表列表项目编号从 0 开始用 list.get(i)函数获取列表第 i 项目输出*/ List.remove(i); i-; /*编写一个学生类,具有姓名,年龄,性别三个属性,创建 3 个学生对象并保存 到 HashSet 中, 利用迭代器 Iterator 将集合里所有学生姓名,年龄,性别打印出来。*/ import java.util.*; class Student private String name,sex; int age; Student(String name,String sex,int age) this.name=name; this.sex=sex; this.age=age; public class IteratorTest public static void main(String args) Set s=new HashSet(); Student t1=new Student(“sally“,“girl“,18); Student t2=new Student(“mary“,“girl“,16); Student t3=new Student(“ken“,“boy“,17); s.add(t1);/存入对象 s.add(t2); s.add(t3);Iterator it = s.iterator(); while(it.hasNext() System.out.println(it.next();/输出 HashHet 中的值
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号