java集合框架(习题与答案)资料 下载本文

内容发布更新时间 : 2024/6/24 21:11:20星期一 下面是文章的全部内容请认真阅读。

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + age;

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Worker1 other = (Worker1) obj;

if (age != other.age)

return false;

return true;

}

@Override

public String toString() {

// TODO Auto-generated method stub

return name+\

}

}

14. (泛型)使用泛型和Map.Entry 接口,改写第12 题的前4 问 15. *(List)写出下面程序的输出结果 import java.util.*; class MyClass{ int value;

public MyClass(){}

public MyClass(int value){ this.value = value; } public String toString(){ return “”+value; } }

public class TestList{

public static void main(String args[]){ MyClass mc1 = new MyClass(10);

MyClass mc2 = new MyClass(20);//实例化的对象实际上就是一个对象的地址,

//list中保存的是对象的引用,因此mc4,mc1,和list下标为1的对象都是指向的同一个对象

MyClass mc3 = new MyClass(30); List list = new ArrayList(); list.add(mc1); list.add(mc2); list.add(mc3);

MyClass mc4 = (MyClass) list.get(1)//这句话实际上就是把mc4指向了mc2对象的那个地址 MyClass mc4=(MyClass)mc2; mc4.value = 50;

for(int i = 0; i

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

class Student { int age;

String name;

public Student(){}

public Student(String name, int age){ this.name = name; this.age = age; }

public int hashCode(){

return name.hashCode() + age; }

public boolean equals(Object o){ if (o == null) return false; if (o == this) return true;

if (o.getClass() != this.getClass()) return false; Student stu = (Student) o;

if (stu.name.equals(name) && stu.age == age) return true; else return false; } }

public class TestHashSet{

public static void main(String args[]){ Set set = new HashSet();

Student stu1 = new Student();

Student stu2 = new Student(“Tom”, 18); Student stu3 = new Student(“Tom”, 18);

set.add(stu1);在添加stu1的时候会自动调用hashcode和equals方法,而在这两方法中,name的值为空,所以会出现空指针异常。 set.add(stu2); set.add(stu3);

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

下列说法正确的是: A. 编译错误

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

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

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

集合框架(习题)\o:spid=\o:button=\target=\href=\mg.cn/orignal/714a8371t9dbaa4c923c6\集合框架(习题)\src=\

>

要求,完善Worker 和Address 类,使得Worker 对象能够正确放入HashSet 中:即将 Worker 放入HashSet 中时不会出现重复元素。并编写相应测试代码。 18. *(Map)在原有世界杯Map 的基础上,增加如下功能: 读入一支球队的名字,输出该球队夺冠的年份列表。 例如,读入“巴西”,应当输出 1958 1962 1970 1994 2002 读入“荷兰”,应当输出 没有获得过世界杯

19. *(Map)设计Account 对象如下: 集合框架(习题)\o:button=\target=\href=\mg.cn/orignal/714a8371t9dba9df86404\集合框架(习题)\src=\>

要求完善设计,使得该Account 对象能够自动分配id。 给定一个List 如下: List list = new ArrayList();

list.add(new Account(10.00, “1234”)); list.add(new Account(15.00, “5678”)); list.add(new Account(0, “1010”));

要求把List 中的内容放到一个Map 中,该Map 的键为id,值为相应的Account 对象。 最后遍历这个Map,打印所有Account 对象的id 和余额。

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Random;

import java.util.Set;

public class BK20 {

public static void main(String[] args) {

Random ran=new Random();

System.out.println(ran.nextLong());

List list=new ArrayList();

list.add(new Account(10.00, \

list.add(new Account(15.00, \

list.add(new Account(0.0, \

Map map=new HashMap();

for(int i=0;i

Account account=(Account)list.get(i);

map.put(account.getId(), account);

}

Set> set=map.entrySet();

for(Map.Entryobj:set){

Account acc=(Account)obj.getValue();

System.out.println(obj.getKey()+\

} } }

class Account{

private long id;

private double balance;

private String password;