首页 > 编程知识 正文

机械制第7版参考答案,双语学习报参考答案

时间:2023-05-03 16:37:50 阅读:246502 作者:2618

一.选择题

1.AC

解析:A.注意题目是Collections不是 Collection,前者是一个until下的类,后者才是接口

       C.Set中的数据是无序且不能重复的

2.A

解析:将发生数组越界错误

3.D

解析:A和B都是list,有序,可以重复,C是使用二叉树实现的,也是有序的

4.C

解析:Set不可重复,重复会发生覆盖key,相当于没有添加

5.C

解析:重复会覆盖,但不会报错

二.简答题

1.数组和集合的比较

       数组不是面向对象的,存在明显的缺陷,集合弥补了数组的缺点,比数组更灵活更实用,而且不同的集合框架类可适用不同场合。如下:

· 1:数组存放基本数据类型和对象,而集合类存放的都是对象的引用,而非对象本身!
· 2:数组长度固定无法动态改变,集合类容量动态改变。 
· 3:数组无法判断其中实际存有多少元素,length只告诉了数组的容量,定义了多大的长度后,即使没有存放那么多的元素,没有存放的元素的空间就浪费了,而集合的size()可以确切知道元素的个数 
· 4:数组仅采用顺序表方式, 集合有多种实现方式和不同适用场合。
· 5:集合以类的形式存在,具有封装、继承、多态等类的特性,通过简单的方法和属性即可实现各种复杂操作,大大提高了软件的开发效率

2.https://blog.csdn.net/an341221/article/details/50514664

3.Arraylist底层是数组,所以每次扩容都是创建新数组,很耗资源.LinkedList底层是双向链表,所以删添很方便

4.哈希表其实就是数组+链表,用hashCode的值作为数组的编号(KEY),然后每个数组内部放入链表(VALUE)

5.泛型可以让程序在编写时灵活转换类型,提高封装性,并且在编译时就实行类型检查,更安全

三.程序

1.

package ch9_1;import java.util.*;public class ch9_1 { public static void main(String[] args) { List<Book> listBook = new ArrayList<Book>() ; Map<String, Book> mapBook = new HashMap(); listBook.add(new Book("0000001","老人与海",18.80,"上海教育出版社")); listBook.add(new Book("0000002","编程思想",180,"机械印刷出版社")); listBook.add(new Book("0000003","梦的解析",50,"开朗的小霸王")); //注意map的存储是散列的,是按hash值来算的,所以取出数据顺序不一定和我们写的时候一样,存同理 mapBook.put("0000000",new Book("0000000","红与黑",30.5,"开朗的小霸王")); mapBook.put("0000001",new Book("0000001","老人与海",18.8,"上海教育出版社")); mapBook.put("0000002",new Book("0000002","编程思想",180,"机械印刷出版社")); mapBook.put("0000003",new Book("0000003","梦的解析",50,"开朗的小霸王")); for (Book books: listBook) { System.out.println("ID:"+books.getId()+"tname:"+books.getName()+"t price:"+books.getPrice()+"t publishingHouse:"+books.getPublishingHouse()); } System.out.println("++++++++++++++++++++++++++++++++++++++++++");// for (Iterator<Map.Entry<String,Book>> it = mapBook.entrySet().iterator();it.hasNext();){// Map.Entry e = (Map.Entry) it.next();// System.out.println(e);// Book printBook = (Book) e.getValue();// System.out.println("ID:"+printBook.getId()+"tname:"+printBook.getName()+"t price:"+printBook.getPrice()+"t publishingHouse:"+printBook.getPublishingHouse());// } Set<String> keySet = mapBook.keySet(); for (String id : keySet) { System.out.println("ID:"+mapBook.get(id).getId()+"tname:"+mapBook.get(id).getName()+"t price:"+mapBook.get(id).getPrice()+"t publishingHouse:"+mapBook.get(id).getPublishingHouse()); } }}

2.

package ch9_2;import java.util.HashSet;import java.util.TreeSet;public class ch9_2 { public static void main(String[] args) { HashSet<Book> hashSet = new HashSet<Book>(); TreeSet<Book> treeSet = new TreeSet<Book>(); //hashSet需要重写hashcode方法和equals()方法 System.out.println("这里添加hashSet元素!"); hashSet.add(new Book("0000000","红与黑",30.5,"开朗的小霸王")); hashSet.add(new Book("0000000","老人与海",18.80,"上海教育出版社")); hashSet.add(new Book("0000002","编程思想",180,"机械印刷出版社")); hashSet.add(new Book("0000003","梦的解析",50,"开朗的小霸王")); System.out.println("hashSet:n"+hashSet); //treeSet需要实现Comparable<T>接口 System.out.println("+++++++++++++++++++++++++++++++++++++++++n这里添加treeSet元素!"); treeSet.add(new Book("0000000","红与黑",30.5,"开朗的小霸王")); treeSet.add(new Book("0000000","老人与海",18.8,"上海教育出版社")); treeSet.add(new Book("0000003","编程思想",180,"机械印刷出版社")); treeSet.add(new Book("0000002","梦的解析",50,"开朗的小霸王")); System.out.println("treeSet:n"+treeSet); }}package ch9_2;public class Book implements Comparable<Book> { private String id; private String name; private double price; private String publishingHouse; public Book(String id, String name, double price, String publishingHouse) { this.id = id; this.name = name; this.price = price; this.publishingHouse = publishingHouse; } @Override public int compareTo(Book b1){ System.out.println("这里调用了compareTo()方法"); return Integer.parseInt( this.id)-Integer.parseInt(b1.id); } @Override public int hashCode(){ System.out.println("我调用了hashcode()函数!"); return this.id.hashCode(); } @Override public boolean equals(Object obj) { System.out.println("我调用了equals()函数!"); if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Book other = (Book) obj; if (id != other.id) { return false; } if(id == other.id){ return true; } if (name == null) { if (other.name != null) { return false; } } else if (!name.equals(other.name)) { return false; } if (publishingHouse == null) { if (other.publishingHouse != null) { return false; } } else if (!publishingHouse.equals(other.publishingHouse)) { return false; } if (Double.doubleToLongBits(price) != Double .doubleToLongBits(other.price)) { return false; } return true; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getPublishingHouse() { return publishingHouse; } public void setPublishingHouse(String publishingHouse) { this.publishingHouse = publishingHouse; } @Override public String toString() { return "Book{" + "id='" + id + ''' + ", name='" + name + ''' + ", price=" + price + ", publishingHouse='" + publishingHouse + ''' + '}'+'n'; }}

3.

转载于:https://www.cnblogs.com/jeasion/p/10758364.html

win7电脑文件夹如何加密

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。