容器作业
一、 填空题
二、 选择题
1. |
以下选项中关于Java集合的说法错误的是( AC )。(选择二项) |
|
|
|
|
|
A. |
List接口和Set接口是Collections接口有两个子接口 |
|
B. |
List接口中存放的元素具有有序,不唯一的特点 |
|
C. |
Set接口中存放的元素具有无序,不唯一的特点 |
|
D. |
Map接口存放的是映射信息,每个元素都是一个键值对 |
2. |
如下Java代码,输出的运行结果是( A )。(选择一项) |
|
|
public class Test { public static void main(String[ ] args) { List<String> list=new ArrayList<String>(); list.add("str1"); list.add(2, "str2"); String s=list.get(1); System.out.println(s); } 下标越界异常 } |
|
|
|
|
|
A |
运行时出现异常 |
|
B. |
正确运行,输出str1 |
|
C. |
正确运行,输出str2 |
|
D. |
编译时出现异常 |
3. |
以下Java代码的作用是首先将一个数组的内容存入集合,然后判断集合中是否有指定的元素存在,其中共有( D )处错误。(选择一项) |
|
|
import java.util.List; public class Test { public int getIndexofArray(float[] f){ int rtn=-1; float objf=3.4;1 List list=null;2 for(int i=0;i<f.size( );i++){3 list.add(f[i]); } for(int i=0;i<list.size( );i++){ float tmp=(float)list.get(i); if(objf==tmp){ rtn=i; } } return rtn; } } |
|
|
|
|
|
A |
0 |
|
B. |
1 |
|
C. |
2 |
|
D. |
3 |
4. |
分析如下Java 代码,编译运行后将输出( B )。(选择一项) |
|
|
public class Test { public Test() { } static void print(List<Integer> al) { al.add(2); al = new ArrayList<Integer>(); al.add(3); al.add(4); } public static void main(String[] args) { List<Integer> al = new ArrayList<Integer>(); al.add(1); print(al); System.out.println(al.get(1)); } } |
|
|
|
|
|
A |
1 |
|
B. |
2 |
|
C. |
3 |
|
D. |
4 |
5. |
在Java中,下列集合类型可以存储无序、不重复的数据的是( D )。(选择一项) |
|
|
|
|
|
A |
ArrayList |
|
B. |
LinkedList |
|
C. |
TreeSet |
|
D. |
HashSet |
6. |
以下代码的执行结果是( C )。(选择一项) |
|
|
Set<String> s=new HashSet<String>(); s.add("abc");1 s.add("abc");1 s.add("abcd");2 s.add("ABC");3 System.out.println(s.size()); |
|
|
|
|
|
A. |
1 |
|
B. |
2 |
|
C. |
3 |
|
D. |
4 |
7. |
给定如下Java代码,编译运行的结果是( C )。(选择一项) |
|
|
public class Test { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); String s = "code"; map.put(s, "1"); map.put(s, "2"); System.out.println(map.size()); }元素的个数 } |
|
|
|
|
|
A |
编译时发生错误 |
|
B. |
运行时引发异常 |
|
C. |
正确运行,输出:1 |
|
D. |
正确运行,输出:2 |
8. |
下面集合类中属于非线程安全,且结构采用了哈希表的是( C )。(选择一项) |
||
|
|
|
|
|
A. |
Vector |
|
|
B. |
ArrayList |
|
|
C. |
HashMap |
|
|
D. |
Hashtable |
|
9. |
在Java中,LinkedList类与ArrayList类同属于集合框架类,下列( CD )选项中是LinkedList类有而ArrayList类没有的方法。(选择两项) |
||
|
|
|
|
|
A |
add(Object o) |
|
|
B. |
add(int index,Object o) |
|
|
C. |
getFirst() |
|
|
D. |
removeLast() |
三、 判断题
四、 简答题 (首先熟练掌握笔记上 与集合相关的面试简答题)
Collection是Java集合顶级接口,存储一组不唯一,无序的对象;
Map集合是存储键值对的集合,特点:key 唯一 value 允许重复
List接口和Set接口是Collections接口有两个子接口;
List 接口存储一组不唯一,有序的对象;
Set 接口存储一组唯一,无序的对象;
实现原理相同,功能相同,都是长度可变的数组结构,很多情况下可以互用
两者的主要区别如下
Vector是早期JDK接口,ArrayList是替代Vector的新接口
Vector线程安全,效率低下;ArrayList重速度轻安全,线程非安全
实现原理相同,功能相同,底层都是哈希表结构,查询速度快,在很多情况下可以互用
两者的主要区别如下
Hashtable是早期JDK提供的接口,HashMap是新版JDK提供的接口
Hashtable继承Dictionary类,HashMap实现Map接口
Hashtable线程安全,HashMap线程非安全
Hashtable不允许null值,HashMap允许null值
五、 编码题 (首先熟练掌握笔记上 与集合相关的需求小案例)
package test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Set; public class Books { String id; // 编号 String name; // 名称 double price; // 价格 String press; // 出版社 // 全参构造方法 public Books(String id, String name, double price, String press) { super(); this.id = id; this.name = name; this.price = price; this.press = press; } // 重写toString() @Override public String toString() { return "Books [id=" + id + ", name=" + name + ", price=" + price + ", press=" + press + "]"; } public static void main(String[] args) { // new5个图书对象 Books b1 = new Books("001", "第1本书", 55.8, "第1出版社"); Books b2 = new Books("002", "第2本书", 49.9, "第2出版社"); Books b3 = new Books("003", "第3本书", 55.0, "第3出版社"); Books b4 = new Books("004", "第4本书", 48.5, "第4出版社"); Books b5 = new Books("005", "第5本书", 55.8, "第5出版社"); Books b6 = new Books("006", "第6本书", 53.6, "第6出版社"); // new一个List对象 List<Books> l = new ArrayList<Books>(); // 添加图书对象到l l.add(b1); l.add(b2); l.add(b3); l.add(b4); l.add(b5); l.add(b6); // 遍历输出 for (Books s : l) { System.out.println(s); } System.out.println("======================================================"); // new一个HashMap对象 HashMap<String, Books> hm = new HashMap<String, Books>(); // 以编号做为key添加图书对象到hm hm.put(b1.id, b1); hm.put(b2.id, b2); hm.put(b3.id, b3); hm.put(b4.id, b4); hm.put(b5.id, b5); hm.put(b6.id, b6); // 获得所有key Set<String> ss = hm.keySet(); // 遍历输出 for (String s : ss) { System.out.println(hm.get(s)); } } }
提示:向HashSet中添加自定义类的对象信息,需要重写hashCode和equals( )
向TreeSet中添加自定义类的对象信息,需要实现Comparable接口,指定比较规则
package test; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; import bean.TestSet; public class TestSett { public static void main(String[] args) { TestSet b1 = new TestSet(1994, "b1", 15.5, "wty"); TestSet b1 = new TestSet(1994, "b1", 30, "wty"); TestSet b2 = new TestSet(1994, "b2", 50, "wty"); TestSet b3 = new TestSet(1993, "b3", 15.5, "wty"); TestSet b4 = new TestSet(1992, "b4", 15.5, "wty"); TestSet b5 = new TestSet(1991, "b5", 50, "wty1"); //使用HashSet存储图书并遍历 Set<TestSet> hashSet = new HashSet<TestSet>(); hashSet.add(b1); hashSet.add(b1); hashSet.add(b2); hashSet.add(b3); hashSet.add(b4); hashSet.add(b5); hashSet.add(b1_1); System.out.println("遍历输出hashset"); System.out.println(hashSet.size()); for(TestSet book:hashSet){ System.out.println(book.toString()); } //使用TreeSet存储图书并遍历 Set<TestSet> treeSet = new TreeSet<TestSet>(); treeSet.add(b1); treeSet.add(b1); treeSet.add(b2); treeSet.add(b3); treeSet.add(b4); treeSet.add(b5); treeSet.add(b1_1); System.out.println("遍历输出treeset"); System.out.println(treeSet.size()); for(TestSet book:treeSet){ System.out.println(book.toString()); } } } package bean; public class TestSet implements Comparable<TestSet> { public int id; public String name; public double price; public String press; public TestSet() { super(); } public TestSet(int id, String name, double price, String press) { super(); this.id = id; this.name = name; this.price = price; this.press = press; } public int compareTo(TestSet o) { return this.id-o.id; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((press == null) ? 0 : press.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; TestSet other = (TestSet) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (press == null) { if (other.press != null) return false; } else if (!press.equals(other.press)) return false; if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) return false; return true; } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", press=" + press+ ", price=" + price + "]"; } }
功能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对象
六、 可选题
package test; import java.util.HashMap; import java.util.Map; public class Email { public static void main(String[] args) { String str = "aa@sohu.com,bb@163.com,cc@sina.com"; System.out.println(str); //得到每一个email String strs[] = str.split(","); //存放分离后email的信息 Map<String,String> emailMap = new HashMap<String, String>(); for(String email:strs){ String temp[] = email.split("@"); //分割email存入map emailMap.put(temp[0], temp[1]); } System.out.println(emailMap.toString()); } }
推荐步骤:
a) 创建Student类,并指定按照年龄正序排列
b) 通过控制台输入多个不同Student信息。格式规定为:编号#姓名#年龄
c) 取出字符串中相应信息放入Student对象,并将Student加入到集合中
d) 遍历集合的过程中将学生的信息输入到记事本
难点:
e) 如何指定学生按照年龄正序排列
f) 如果从字符串“编号#姓名#年龄”中提取学生信息
g) 放入哪种集合后可以保证学生按照年龄大小正序排列
h) 如何将集合中学生信息写入记事本,每个学生数据占单独一行
原文:https://www.cnblogs.com/wty1994/p/9383190.html