集合练习题.docx

上传人:b****0 文档编号:9585080 上传时间:2023-05-20 格式:DOCX 页数:19 大小:287.96KB
下载 相关 举报
集合练习题.docx_第1页
第1页 / 共19页
集合练习题.docx_第2页
第2页 / 共19页
集合练习题.docx_第3页
第3页 / 共19页
集合练习题.docx_第4页
第4页 / 共19页
集合练习题.docx_第5页
第5页 / 共19页
集合练习题.docx_第6页
第6页 / 共19页
集合练习题.docx_第7页
第7页 / 共19页
集合练习题.docx_第8页
第8页 / 共19页
集合练习题.docx_第9页
第9页 / 共19页
集合练习题.docx_第10页
第10页 / 共19页
集合练习题.docx_第11页
第11页 / 共19页
集合练习题.docx_第12页
第12页 / 共19页
集合练习题.docx_第13页
第13页 / 共19页
集合练习题.docx_第14页
第14页 / 共19页
集合练习题.docx_第15页
第15页 / 共19页
集合练习题.docx_第16页
第16页 / 共19页
集合练习题.docx_第17页
第17页 / 共19页
集合练习题.docx_第18页
第18页 / 共19页
集合练习题.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

集合练习题.docx

《集合练习题.docx》由会员分享,可在线阅读,更多相关《集合练习题.docx(19页珍藏版)》请在冰点文库上搜索。

集合练习题.docx

集合练习题

1.填空

Collection接口的特点是元素是___对象______;

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){

System.out.prinltn(list);

}

}

要求:

1)把//1处的代码补充完整,要求输出list中所有元素的内容

2)写出程序执行的结果

HelloJavaLearnWorld

3)如果要把实现类由ArrayList换为LinkedList,应该改哪里?

ArrayList和

LinkedList使用上有什么区别?

实现上有什么区别?

Listlist=newLinkedList();

LinkedList(更适用于频繁的插入、删除操作)

4)如果要把实现类由ArrayList换为Vector,应该改哪里?

ArrayList和Vector使

用上有什么区别?

实现上有什么区别?

Listlist=newVector();

Vector(古老的实现类、线程安全的,但效率要低于ArrayList)

3.(List)写出下面程序的运行结果

importjava.util.*;

publicclassTestList{

publicstaticvoidmain(Stringargs[]){

Listlist=newArrayList();

list.add(“Hello”);

list.add(“World”);

list.add(“Hello”);

list.add(“Learn”);

list.remove(“Hello”);

list.remove(0);

for(inti=0;i

System.out.println(list.get(i));

}

}

}

WorldLearn

4.(Set,List)

importjava.util.*;

publicclassTestListSet{

publicstaticvoidmain(Stringargs[]){

Listlist=newArrayList();

list.add(“Hello”);

list.add(“Learn”);

list.add(“Hello”);

list.add(“Welcome”);

Setset=newHashSet();

set.addAll(list);

System.out.println(set.size());

}

}

选择正确答案A

A.编译不通过

B.编译通过,运行时异常

C.编译运行都正常,输出3

D.编译运行都正常,输出4

5.(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中增加三个工人,基本信息如下:

姓名年龄工资

zhang3183000

li4253500

wang5223200

ArrayListL=newArrayList();

L.add(newWorker(zhang3,18,3000));

L.add(newWorker(li4,25,3500));

L.add(newWorker(wang5,22,3200));

2)在li4之前插入一个工人,信息为:

姓名:

zhao6,年龄:

24,工资3300

L.add(1,newWorker(zhao6,24,330));

3)删除wang5的信息

L.remove(3);

4)利用for循环遍历,打印List中所有工人的信息

先在Worker类中重写toString方法

for(inti=0;i

Syso(L.get(i));

5)利用迭代遍历,对List中所有的工人调用work方法。

Iteratorit=L.iterator();

while(it.hasNext()){

System.out.println(it.next());

}

6)为Worker类添加equals方法

publicbooleanequals(Objectobj){

if(this==obj)

returntrue;

if(obj==null)

returnfalse;

if(getClass()!

=obj.getClass())

returnfalse;

Workerother=(Worker)obj;

if(age!

=other.age)

returnfalse;

if(name==null){

if(other.name!

=null)

returnfalse;

}elseif(!

name.equals(other.name))

returnfalse;

returntrue;

6.(Set,Hash算法)为上一题的Worker类,在添加完equals方法的基础上,添加一个

hashCode方法。

publicinthashCode(){

//1

}

有几种写法:

1)return0;

2)

intresult=0;

if(name!

=null)result=name.hashCode();

returnresult+age;

3)returnsuper.hashCode();

现在要把Worker类放入HashSet中,并希望在HashSet中没有重复元素,则下面说法正

确的是:

C

A.三种写法都正确

B.1),2)写法正确,2)效率更高

C.2)写法正确,1),3)写法都不正确

7.(Set,Hash算法,方法覆盖)代码改错

importjava.util.*;

classWorker{

Stringname;

intage;

doublesalary;

publicWorker(){}

publicWorker(Stringname,intage,doublesalary){

this.name=name;

this.age=age;

this.salary=salary;

}

publicinthashCode(){//重写父类方法,权限不能小于父类

returnname.hashCode()+age+salary;

}

publicbooleanequals(Workerw){

if(w.name==name&&w.salary==salary&&w.age==age){

returntrue;

}else

returnfalse;

}

}

publicclassTestWorker{

publicstaticvoidmain(Stringargs[]){

Setset=newHashSet();

set.add(newWorker(“tom”,18,2000));

set.add(newWorker(“tom”,18,2000));

set.add(0,newWorker(“jerry”,18,2000));//HashSet是无序的集合,不能在指定位置添加

System.out.println(set.size());

}

}

8.(Set,Hash算法)在前面的Worker类基础上,为Worker类增加相应的方法,使得Worker放入HashSet中时,Set中没有重复元素。

并编写相应的测试代码。

9.(Set,Comparable接口)在前面的Worker类基础上,为Worker类添加相应的代码,

使得Worker对象能正确放入TreeSet中。

并编写相应的测试代码。

注:

比较时,先比较工人年龄大小,年龄小的排在前面。

如果两个工人年龄相同,则再

比较其收入,收入少的排前面。

如果年龄和收入都相同,则根据字典顺序比较工人姓名。

如:

有三个工人,基本信息如下:

姓名年龄工资

zhang3181500

li4181500

wang5181600

zhao6172000

放入TreeSet排序后结果为:

zhao6li4zhang3wang5

10.(Map)关于下列Map接口中常见的方法

put方法表示放入一个键值对,如果键已存在则________________,如果键不存在则

______________。

remove方法接受___个参数,表示__________________。

get方法表示_______________________,get方法的参数表示______,返回值表示_____。

要想获得Map中所有的键,应该使用方法___________,该方法返回值类型为_______。

要想获得Map中所有的值,应该使用方法___________,该方法返回值类型为_______。

要想获得Map中所有的键值对的集合,应该使用方法___________,该方法返回一个

_______类型所组成的Set。

11.(Map)利用Map,完成下面的功能:

从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。

如果该

年没有举办世界杯,则输出:

没有举办世界杯。

附:

世界杯冠军以及对应的夺冠年份,请参考本章附录。

12.(Map)已知某学校的教学课程内容安排如下:

完成下列要求:

1)使用一个Map,以老师的名字作为键,以老师教授的课程名作为值,表示上述

课程安排。

2)增加了一位新老师Allen教JDBC

3)Lucy改为教CoreJava

4)遍历Map,输出所有的老师及老师教授的课程

5)*利用Map,输出所有教JSP的老师。

13.(泛型)使用泛型,改写第5题

14.(泛型)使用泛型和Map.Entry接口,改写第12题的前4问

15.*(List)写出下面程序的输出结果

importjava.util.*;

classMyClass{

intvalue;

publicMyClass(){}

publicMyClass(intvalue){this.value=value;}

publicStringtoString(){

return“”+value;

}

}

publicclassTestList{

publicstaticvoidmain(Stringargs[]){

MyClassmc1=newMyClass(10);

MyClassmc2=newMyClass(20);

MyClassmc3=newMyClass(30);

Listlist=newArrayList();

list.add(mc1);

list.add(mc2);

list.add(mc3);

MyClassmc4=(MyClass)list.get

(1);

mc4.value=50;

for(inti=0;i

System.out.println(list.get(i));

}

}

}

16.*(Set,HashSet,空指针)有下面代码

importjava.util.*;

classStudent{

intage;

Stringname;

publicStudent(){}

publicStudent(Stringname,intage){

this.name=name;

this.age=age;

}

publicinthashCode(){

returnname.hashCode()+age;

}

publicbooleanequals(Objecto){

if(o==null)

returnfalse;

if(o==this)

returntrue;

if(o.getClass()!

=this.getClass())

returnfalse;

Studentstu=(Student)o;

if(stu.name.equals(name)&&stu.age==age)

returntrue;

else

returnfalse;

}

}

publicclassTestHashSet{

publicstaticvoidmain(Stringargs[]){

Setset=newHashSet();

Studentstu1=newStudent();

Studentstu2=newStudent(“Tom”,18);

Studentstu3=newStudent(“Tom”,18);

set.add(stu1);

set.add(stu2);

set.add(stu3);

System.out.println(set.size());

}

}

下列说法正确的是:

A.编译错误

B.编译正确,运行时异常

C.编译运行都正确,输出结果为3

D.编译运行都正确,输出结果为2

17.*(Set)有如下两个类(只写了类的属性,请自行添加相应的构造方法和get/set方法)

要求,完善Worker和Address类,使得Worker对象能够正确放入HashSet中:

即将

Worker放入HashSet中时不会出现重复元素。

并编写相应测试代码。

18.*(Map)在原有世界杯Map的基础上,增加如下功能:

读入一支球队的名字,输出该球队夺冠的年份列表。

例如,读入“巴西”,应当输出

19581962197019942002

读入“荷兰”,应当输出

没有获得过世界杯

19.*(Map)设计Account对象如下:

要求完善设计,使得该Account对象能够自动分配id。

给定一个List如下:

Listlist=newArrayList();

list.add(newAccount(10.00,“1234”));

list.add(newAccount(15.00,“5678”));

list.add(newAccount(0,“1010”));

要求把List中的内容放到一个Map中,该Map的键为id,值为相应的Account对象。

最后遍历这个Map,打印所有Account对象的id和余额。

20.*(List)写一个函数reverseList,该函数能够接受一个List,然后把该List倒序排列。

例如:

Listlist=newArrayList();

list.add(“Hello”);

list.add(“World”);

list.add(“Learn”);//此时list为HelloWorldLearn

reverseList(list);

//调用reverseList方法之后,list为LearnWorldHello

21.**(Map,Hash算法)有如下代码:

importjava.util.*;

classMyKey{

intkeyValue;

publicMyKey(){}

publicMyKey(intvalue){

this.keyValue=value;

}

}

classMyValue{

Stringvalue;

publicMyValue(){}

publicMyValue(Stringvalue){

this.value=value;

}

publicStringtoString(){

returnvalue;

}

}

publicclassTestMap{

publicstaticvoidmain(Stringargs[]){

Mapmap=newHashMap();

MyKeykey1=newMyKey(10);

map.put(key1,newMyValue(“abc”));

map.put(newMyKey(10),newMyValue(“cde”));

System.out.println(map.get(key1));

System.out.println(map.size());

}

}

写出该代码的输出结果。

22.**(Id,hashCode,equals)为Worker类增加id属性,用来唯一标识一个员工。

即:

如果员工的id不同,则不管其姓名、年龄、工资是否相同,都认为是不同的员工。

部分代

码如下:

classWorker{

privatefinalLongid;

privateStringname;

privatedoublesalary;

privateintage;

//构造方法…

//get/set方法…

publicbooleanequals(Objectobj){

//1此处仅判断id是否相同

}

publicinthashCode(){

//2此处返回hashCode

}

}

要求:

1)完善构造方法和get/set方法。

要求自动分配Worker的id

2)完善equals方法。

要求仅判断id是否相同

3)//2处,如果写成

return(int)(name.hashCode()+id.hashCode()+age+salary);

是否正确?

为什么?

23.**(综合)有如下Student对象

其中,classNum表示学生的班号,例如“class05”。

有如下List

Listlist=newArrayList();

list.add(newStudent(“Tom”,18,100,“class05”));

list.add(newStudent(“Jerry”,22,70,“class04”));

list.add(newStudent(“Owen”,25,90,“class05”));

list.add(newStudent(“Jim”,30,80,“class05”));

list.add(newStudent(“Steve”,28,66,“class06”));

list.add(newStudent(“Kevin”,24,100,“class04”));

在这个list的基础上,完成下列要求:

1)计算所有学生的平均年龄

2)计算各个班级的平均分

24.**(综合)已知有十六支男子足球队参加2008北京奥运会。

写一个程序,把这16支球队随机分为4个组。

注:

参赛球队列表见附录

注2:

使用Math.random来产生随机数。

25.**(综合)写一个MyStack类,表示“栈”这种数据结构。

栈在表示上,就如同一个单向开口的盒子,每当有新数据进入时,都是进入栈顶。

其基

本操作为push和pop。

push表示把一个元素加入栈顶,pop表示把栈顶元素弹出。

示意图如下:

栈的特点:

先进后出。

栈的基本操作:

1)push(Objecto):

表示把元素放入栈

2)Objectpop():

返回栈顶元素,并把该元素从栈中删除。

如果栈为空,则返回null值

3)Objectpeek():

返回栈顶元素,但不把该元素删除。

如果栈为空,则返回null值。

4)booleanisEmpty():

判断该栈是否为空

5)intsize():

返回该栈中元素的数量

要求:

1)利用List,实现栈。

2)讨论:

应当用ArrayList作为实现类还是用LinkedList?

为什么?

附录

1.截止到2009年为止,历届世界杯冠军

2.2008北京奥运会男足参赛国家:

科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚、日本,美国,中国,新西

兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2