java-实验七-对象集合-实验报告

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验七对象集合的组织实验目标:1.了解Collection、List、Set、Map等常用数据结构的层次结构2.掌握LinkList、Vector、ArrayList、HashTable、HashMap等类的用法3.掌握Enumeration(枚举)及Iterator(迭代器)的遍历用法4.了解自学HashSet、TreeSet这两个Set接口下的重要实现类5.了解自学比较器接口实验任务:1.填空List接口的特点是元素__有__(有|无)顺序,___可以___(可以|不可以)重复;Set接口的特点是元素__无___(有|无)顺序,__不可以____(可以|不可以)重复;Map接口的特点是元素是键、值对象,其中__键__可以重复,_值对象__不可以重复2.(List)有如下代码:importjava.util.*;publicclassTestList{publicstaticvoidmain(Stringargs[]){Listlist=newArrayList();list.add(“Hello”);list.add(“World”);list.add(1,“Learn”);list.add(1,“Java”);printList(list);}publicstaticvoidprintList(Listlist){//1}}要求:1)把//1处的代码补充完整,要求输出list中所有元素的内容,请使用三种方式来实现(课本227页,Iterator、增强for循环,普通循环)。2)三种代码与执行结果截图1、用Iterator实现的代码如下:packagezi;importjava.util.*;publicclassEx7_2{publicstaticvoidmain(Stringargs[]){Listlist=newArrayList();list.add(Hello);list.add(World);list.add(1,Learn);list.add(1,Java);printList(list);}publicstaticvoidprintList(Listlist){//1Iteratoritor=list.iterator();while(itor.hasNext()){System.out.println(itor.next());}}}其运行结果截图如下:2、用增强for循环的代码如下:packagezi;importjava.util.*;publicclassEx7_2{publicstaticvoidmain(Stringargs[]){Listlist=newArrayList();list.add(Hello);list.add(World);list.add(1,Learn);list.add(1,Java);printList(list);}publicstaticvoidprintList(Listlist){//1for(Objecto:list){System.out.println(o);}}}其运行结果截图如下:3、用普通循环的代码如下:packagezi;importjava.util.*;publicclassEx7_2{publicstaticvoidmain(Stringargs[]){Listlist=newArrayList();list.add(Hello);list.add(World);list.add(1,Learn);list.add(1,Java);printList(list);}publicstaticvoidprintList(Listlist){//1for(inti=0;ilist.size();i++){System.out.println(list.get(i));}}}其运行结果截图如下:3)如果要把实现类由ArrayList换为LinkedList,应该改哪一句?怎么修改?ArrayList和LinkedList使用上有什么区别?请书面回答答:Listlist=newLinkedList();ArrayList:查询数据速度较快,插入和删除比较慢,线程非安全。LinkedList:查询数据速度较慢,插入和删除比较快,线程安全。4)如果要把实现类由ArrayList换为Vector,应该改哪一句?怎修改?ArrayList和Vector使用上有什么区别?请书面回答。答:Listlist=newVector();Vector:以线性结构存储,查询速度较慢,线程安全。3.(List)已知有一个Worker类如下:publicclassWorker{privateintage;privateStringname;privatedoublesalary;publicWorker(){}publicWorker(Stringname,intage,doublesalary){this.name=name;this.age=age;this.salary=salary;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicdoublegetSalary(){returnsalary;}publicvoidsetSalary(doublesalary){this.salary=salary;}publicvoidwork(){System.out.println(name+“work”);}}完成下面的要求1)创建一个List,在List中增加三个工人,基本信息包括:姓名年龄工资。答:增加三个工人赵一,钱二,孙三及其他们的年龄和工资,其代码如下:packagezi;importjava.util.ArrayList;importjava.util.List;classWorker{privateintage;privateStringname;privatedoublesalary;publicWorker(){}publicWorker(Stringname,intage,doublesalary){this.name=name;this.age=age;this.salary=salary;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicdoublegetSalary(){returnsalary;}publicvoidsetSalary(doublesalary){this.salary=salary;}publicvoidwork(){System.out.println(name+work);}}publicclassEx7_3_1{publicstaticvoidmain(Stringargs[]){ListWorkerlist=newArrayListWorker();Workerw1=newWorker(赵一,30,5600);Workerw2=newWorker(钱二,21,3800);Workerw3=newWorker(孙三,28,4200);list.add(w1);list.add(w2);list.add(w3);}}2)在第二个记录之前插入一个工人信息答:插入后代码如下:packagezi;importjava.util.ArrayList;importjava.util.List;classWorker{privateintage;privateStringname;privatedoublesalary;publicWorker(){}publicWorker(Stringname,intage,doublesalary){this.name=name;this.age=age;this.salary=salary;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicdoublegetSalary(){returnsalary;}publicvoidsetSalary(doublesalary){this.salary=salary;}publicvoidwork(){System.out.println(name+work);}@OverridepublicStringtoString(){returnWorker[age=+age+,name=+name+,salary=+salary+];}}publicclassEx7_3_3{publicstaticvoidmain(Stringargs[]){ListWorkerlist=newArrayListWorker();Workerw1=newWorker(赵一,30,5600);Workerw2=newWorker(钱二,21,3800);Workerw3=newWorker(孙三,28,4200);list.add(w1);list.add(1,w2);list.add(w2);list.add(w3);Workerw2=newWorker(孙三,28,4200);list.remove(3);for(inti=0;ilist.size();i++){System.out.println(list.get(i));}}}运行结果如下,不知道为啥会出现乱码。2)删除一个记录的信息答:其代码如下:packagezi;importjava.util.ArrayList;importjava.util.List;classWorker{privateintage;privateStringname;privatedoublesalary;publicWorker(){}publicWorker(Stringname,intage,doublesalary){this.name=name;this.age=age;this.salary=salary;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicdoublegetSalary(){returnsalary;}publicvoidsetSalary(doublesalary){this.salary=salary;}publicvoidwork(){System.out.println(name+work);}@OverridepublicStringtoString(){returnWorker[age=+age+,name=+name+,salary=+salary+];}}publicclassEx7_3_3{publicstaticvoidmain(Stringargs[]){ListWorkerlist=newArrayListWorker();Workerw1=newWorker(赵一,30,5600);Workerw2=newWorker(钱二,2

1 / 21
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功