速(尚)学堂第九章Java容器章末作业自写答案

速(尚)学堂第九章Java容器章末作业自写答案

第三次在CSDN发文章,正在学JAVA,学习了第九章后,对章末作业做了解答,全部手工自己写的,选择题也有一些自己的看法,如有错误欢迎指正,大家共同进步,高手勿喷,见笑了嘻嘻!

选择题
1.AC //List接口和Set接口是Collection接口的子接口。List有ArrayList、LinkedList和Vector三个常见实现类,Set接口有HashSet、TreeSet两个常见实现类,A错。List接口的元素有序、可重复,Set接口的元素无序、不可重复,B对C错。Map用来存储“键(key)-值(value) 对”的。 Map类中存储的“键值对”通过键来标识,D对。故错误的选AC。

2.A //运行时异常和编译时异常的区别,编译时异常的特点是Java编译器会对其进行检查,如果出现异常就必须对异常进行处理,否则程序无法通过编译,也就是运行的时候会报错。运行时异常的特点是Java编译器不会对其进行检查,也就是说,不会报错,程序也能编译通过,一般是由程序中的逻辑错误引起的。ArrayList类中有add()和add(index,vaule)两个重载,所以不会出现报错,但是因为list中只有一个元素,故索引为2超出范围了,故选A,运行时异常。

3.D //虽然Set接口存储元素是无序的,但是TreeSet底层使用了TreeMap而TreeMap则实现了comparable接口对元素进行了排序,所以最后就是有序的了。故选择D。

4.C //HashSet在添加元素时,底层会调用hashCode和equals方法,当两个对象的hashCode相同并且equals返回为true时说明这两个对象值相同,也就说明添加过重复的对象,故不再添加,但添加以后若再改变值就不会再调用hashCode方法和equals来判断了,这时就会出现Set内的元素重复了,补充:hashCode相同equals不一定为true,equals为true,hashCode一定相同。综上所述,选C。

5.C //同样地,Map中的key转换为hash也是和Set添加时一样,会调用hashCode和equals判断是否为同一对象值,当两个对象的hashCode相同并且equals返回true,则说明该键(key)已经有了,故元素个数为1个,选C。

编码题

  1. 使用List和Map存放多个图书信息,遍历并输出。其中商品属性:编号,名称,单价,出版社;使用商品编号作为Map中的key。
package 第八章容器包;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

/**
 * 使用List和Map存放多个图书信息,遍历并输出。
 * 其中商品属性:编号,名称,单价,出版社;使用商品编号作为Map中的key。
 * 我自己又加了storeMap02方法,把Goods作为value,对value中的price排序
 * @author 皮
 *
 */
public class FinalTest01 {
	public static void main(String[] args) {
		System.out.println("Goods作为key,用comparable对price进行排序");
		storeMap();
		System.out.println("这是用List和Collections工具类实现了price排序:");
		storeList();
		System.out.println("Goods作为value,用comparable对price排序:");
		storeMap02();
	}
	
	//用Map存储,Map键值对为<Goods,Integer>
	public static void storeMap() {
		Map<Goods01,Integer> m = new TreeMap<>();
		
		Goods01 goods1 = new Goods01(1003, "语文", 49, "清华大学");
		Goods01 goods2 = new Goods01(1002, "数学", 59, "清华大学");
		Goods01 goods3 = new Goods01(1004, "英语", 90, "上海交通大学");
		Goods01 goods4 = new Goods01(1001, "物理", 30, "同济大学");
		Goods01 goods5 = new Goods01(1005, "化学", 59, "北京大学");
		
		m.put(goods1,goods1.getId());
		m.put(goods2,goods2.getId());
		m.put(goods3,goods3.getId());
		m.put(goods4,goods4.getId());
		m.put(goods5,goods5.getId());
		
		Set<Entry<Goods01, Integer>> s1 = m.entrySet();
		for(Iterator<Entry<Goods01, Integer>> iter=s1.iterator();iter.hasNext();) {
			Entry<Goods01, Integer> entry = iter.next();
			System.out.println(entry);
		}	
	}

	//用List存储
	public static void storeList() {
		List<Goods01> list = new ArrayList<>();
		
		list.add(new Goods01(1003, "语文", 49, "清华大学"));
		list.add(new Goods01(1002, "数学", 59, "清华大学"));
		list.add(new Goods01(1004, "英语", 90, "上海交通大学"));
		list.add(new Goods01(1001, "物理", 30, "同济大学"));
		list.add(new Goods01(1005, "化学", 59, "北京大学"));
		
		Collections.sort(list);
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i));
		}
	}

	//用Map存储,Map键值对为<Integer,Goods>
	public static void storeMap02() {
		Map<Integer,Goods01> m = new TreeMap<>();
		
		Goods01 goods1 = new Goods01(1003, "语文", 49, "清华大学");
		Goods01 goods2 = new Goods01(1002, "数学", 59, "清华大学");
		Goods01 goods3 = new Goods01(1004, "英语", 90, "上海交通大学");
		Goods01 goods4 = new Goods01(1001, "物理", 30, "同济大学");
		Goods01 goods5 = new Goods01(1005, "化学", 59, "北京大学");
		
		m.put(goods1.getId(), goods1);
		m.put(goods2.getId(), goods2);
		m.put(goods3.getId(), goods3);
		m.put(goods4.getId(), goods4);
		m.put(goods5.getId(), goods5);	
		//使用了匿名内部类实现compare方法的重写
		Map<Integer,Goods01> m1 = new TreeMap<>(new Comparator<Integer>() {
			
			@Override
			public int compare(Integer o1, Integer o2) {
				if(m.get(o1)!=m.get(o2)) {
					return m.get(o1).compareTo(m.get(o2));
				}else {
					return m.get(o1).compareTo(m.get(o2));
				}
			}			
		});
		
		m1.putAll(m);
		
		Set<Entry<Integer, Goods01>> ss = m1.entrySet();
		for(Iterator<Entry<Integer,Goods01>> iter=ss.iterator();iter.hasNext();) {
			Entry<Integer,Goods01> ee = iter.next();
			System.out.println(ee);
		}				
	}
}

class Goods01 implements Comparable<Goods01>{
	private int id;
	private String name;
	private int price;
	private String company;
	
	public Goods01(int id, String name, int price, String company) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.company = company;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public Goods01() {
	}	
	//实现comparable接口先对价格进行排序,价格相同再根据ID排序
	@Override
	public int compareTo(Goods01 o) {
		if(this.price<o.price) {
			return -1;
		}else if(this.price>o.price) {
			return 1;
		}else
			if(this.id<o.id) {
				return -1;
			}else if(this.id>o.id) {
				return 1;
			}else {
				return 0;
			}
	}
	
	@Override
	public String toString() {
		return "编号:"+id+",名称:"+name+",单价:"+price+",出版社:"+company;
	}
}

运行结果:
运行结果
2. 使用HashSet和TreeSet存储多个商品信息,遍历并输出;其中商品属性:编号,名称,单价,出版社;要求向其中添加多个相同的商品,验证集合中元素的唯一性。
提示:向HashSet中添加自定义类的对象信息,需要重写hashCode和equals( )。
向TreeSet中添加自定义类的对象信息,需要实现Comparable接口,指定比较 规则。

package 第八章容器包;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
/**
 * 用HashSet和TreeSet存储多个商品信息,遍历并输出;其中商品属性:编号,名称,单价,出版社;
 * 要求向其中添加多个相同的商品,验证集合中元素的唯一性。
    提示:向HashSet中添加自定义类的对象信息,需要重写hashCode和equals( )。
向TreeSet中添加自定义类的对象信息,需要实现Comparable接口,指定比较 规则。
 * @author 皮
 *
 */
public class FinalTest02 {
	public static void main(String[] args) {
		storeTreeSet();
		System.out.println("###############################");
		storeHashSet();
	}
	
	public static void storeTreeSet() {		
		Set<Goods02> set = new TreeSet<>(new Comparator<Goods02>() {
			@Override
			public int compare(Goods02 o1, Goods02 o2) {
				//直接用匿名内部类,用id来处理插入重复的东西
				return ((Integer)o1.getId()).compareTo((Integer)o2.getId());
			}			
		});
		
		Goods02 goods1 = new Goods02(1003, "语文", 49, "清华大学");
		Goods02 goods2 = new Goods02(1002, "数学", 59, "清华大学");
		Goods02 goods3 = new Goods02(1004, "英语", 90, "上海交通大学");
		Goods02 goods4 = new Goods02(1001, "物理", 30, "同济大学");
		Goods02 goods5 = new Goods02(1005, "化学", 59, "北京大学");
		Goods02 goods6 = new Goods02(1000, "化学", 59, "北京大学");//该条就可以添加,因为编号没重复
		
		//这两条就不能添加进去
		Goods02 goods7 = new Goods02(1005, "化学", 59, "北京大学");
		Goods02 goods8 = new Goods02(1005, "化学", 59, "北京大学");
		
		set.add(goods1);
		set.add(goods2);
		set.add(goods3);
		set.add(goods4);
		set.add(goods5);
		set.add(goods6);
		set.add(goods7);
		set.add(goods8);
		
		for(Iterator<Goods02> iter=set.iterator();iter.hasNext();) {
			Goods02 goods = iter.next();
			System.out.println(goods);
		}				
	}
	public static void storeHashSet() {
		Set<Goods02> set = new HashSet<>();
		
		Goods02 goods1 = new Goods02(1003, "语文", 49, "清华大学");
		Goods02 goods2 = new Goods02(1002, "数学", 59, "清华大学");
		Goods02 goods3 = new Goods02(1004, "英语", 90, "上海交通大学");
		Goods02 goods4 = new Goods02(1001, "物理", 30, "同济大学");
		Goods02 goods5 = new Goods02(1005, "化学", 59, "北京大学");
		Goods02 goods6 = new Goods02(1000, "化学", 59, "北京大学");//该条就可以添加,因为编号没重复
		
		//这两条就不能添加进去
		Goods02 goods7 = new Goods02(1005, "化学", 59, "北京大学");
		Goods02 goods8 = new Goods02(1005, "化学", 59, "北京大学");
		
		set.add(goods1);
		set.add(goods2);
		set.add(goods3);
		set.add(goods4);
		set.add(goods5);
		set.add(goods6);
		set.add(goods7);
		set.add(goods8);
		//用迭代器遍历Set
		for(Iterator<Goods02> iter=set.iterator();iter.hasNext();) {
			Goods02 good = iter.next();
			System.out.println(good);
		}		
	}
}

class Goods02 {
	private int id;
	private String name;
	private int price;
	private String producer;	
	//无参构造
	public Goods02() {
		super();
	}
	
	public Goods02(int id, String name, int price, String producer) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.producer = producer;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public String getProducer() {
		return producer;
	}
	public void setProducer(String producer) {
		this.producer = producer;
	}
	
	@Override
	public String toString() {
		return "编号:"+id+",名称:"+name+",单价:"+price+",出版社:"+producer;
	}
	//重写equals方法的同时必须也重写hashCode方法,因为必须保证hashCode和equals的使用的规范性
	@Override
	public boolean equals(Object obj) {
		//判断该对象是不是咱们的Goods02对象
		if(obj instanceof Goods02) {
			//若是则直接将该对象强制转换为Goods02对象,并逐一判断对象的每一个字符是否相等
			Goods02 newGoods = (Goods02) obj;
			if(newGoods.getId()!=this.getId()) {
				return false;
			}else {
				return true;
			}
		}else {
			return false;
		}
	}
	@Override
	public int hashCode() {
		//hashCode方法返回的是int类型,并且保证返回值要与Goods02对象的属性有关
		return this.getId();
	}	
}

运行结果:
运行结果
3. 实现List和Map数据的转换。具体要求如下:
功能1:定义方法public void listToMap( ){ }将List中Student元素封装到Map中
1) 使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息并加入List;
2) 遍历List,输出每个Student信息;
3) 将List中数据放入Map,使用Student的id属性作为key,使用Student对象信息作为value;
4) 遍历Map,输出每个Entry的key和value。
功能2:定义方法public void mapToList( ){ }将Map中Student映射信息封装到List
1) 创建实体类StudentEntry,可以存储Map中每个Entry的信息;
2) 使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息,并使用Student的id属性作为key,存入Map;
3) 创建List对象,每个元素类型是StudentEntry;
4) 将Map中每个Entry信息放入List对象。
功能3:说明Comparable接口的作用,并通过分数来对学生进行排序。

package 第八章容器包;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 实现List和Map数据的转换。具体要求如下:
    功能1:定义方法public void listToMap( ){ }将List中Student元素封装到Map中
         1) 使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息并加入List;
          2) 遍历List,输出每个Student信息;
          3) 将List中数据放入Map,使用Student的id属性作为key,使用Student对象信息作为value;
      功能2:定义方法public void mapToList( ){ }将Map中Student映射信息封装到List
         1) 创建实体类StudentEntry,可以存储Map中每个Entry的信息;
          2) 使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息,并使用Student的id属性作为key,存入Map;
          3) 创建List对象,每个元素类型是StudentEntry;
          4) 将Map中每个Entry信息放入List对象。
    功能3:说明Comparable接口的作用,并通过分数来对学生进行排序。
 * @author 皮
 *
 */
public class FinalTest03 {
	public static void main(String[] args) {
		FinalTest03 ft = new FinalTest03();
		ft.listToMap();
		ft.mapToList();
			
	}
	public void listToMap() {
		List<Student01> list = new ArrayList<>();
		
		Student01 stu1 = new Student01(1001, "科比", 41, "男");
		Student01 stu2 = new Student01(1002, "詹姆斯", 36, "男");
		Student01 stu3 = new Student01(1003, "奥尼尔", 48, "男");
		Student01 stu4 = new Student01(1004, "乔丹", 57, "男");
		
		list.add(stu1);
		list.add(stu2);
		list.add(stu3);
		list.add(stu4);
		
		System.out.println("List遍历的结果为:");
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i));
		}
		
		Map<Integer,Student01> m = new HashMap<>();
		//用于获得List元素(即Student01对象)的id作为Map的key
		int result;
		
		for(int i=0;i<list.size();i++) {
			result = list.get(i).getId();
			m.put(result, list.get(i));
		}
		
		Set<Integer> keySet = m.keySet();
		System.out.println("List转换为Map结果为:");
		for(Iterator<Integer> iter=keySet.iterator();iter.hasNext();) {
			Integer key = iter.next();
			System.out.println(key+"="+m.get(key));
		}
	}
	
	public void mapToList() {
		Map<Integer,StudentEntry> m = new HashMap<>();
		
		StudentEntry se1 = new StudentEntry(1001, "科比", 41, "男");
		StudentEntry se2 = new StudentEntry(1002, "詹姆斯", 36, "男");
		StudentEntry se3 = new StudentEntry(1003, "奥尼尔", 48, "男");
		StudentEntry se4 = new StudentEntry(1004, "乔丹", 57, "男");
		
		m.put(se1.getId(), se1);
		m.put(se2.getId(), se2);
		m.put(se3.getId(), se3);
		m.put(se4.getId(), se4);
		
		List<StudentEntry> list = new ArrayList<>();
		
		Set<Entry<Integer, StudentEntry>> entrySet = m.entrySet();
		for(Entry<Integer,StudentEntry> e:entrySet) {
			list.add(e.getValue());
			
		}
		System.out.println("Map转换为List的结果为:");
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i));
		}
	}
}
class Student01{
	private int id;
	private String name;
	private int age;
	private String sex;
	
	public Student01() {
	}
	
	public Student01(int id, String name, int age, String sex) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	@Override
		public String toString() {
			return "学号:"+id+",姓名:"+name+",年龄:"+age+",性别:"+sex;
		}
}
//继承了Student01类,就拥有了Student01类的成员属性
class StudentEntry extends Student01{

	public StudentEntry() {
		super();
	}
	//父类的构造方法不能继承,所以要重新写
	public StudentEntry(int id, String name, int age, String sex) {
		super(id, name, age, sex);
		// TODO Auto-generated constructor stub
	}				
}

运行结果
运行结果

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值