容器做业java
1、 填空题面试
2、 选择题算法
1.数组 |
如下选项中关于Java集合的说法错误的是( AC )。(选择二项)安全 |
|
|
|
|
|
A.数据结构 |
List接口和Set接口是Collections接口有两个子接口框架 |
|
B.ide |
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; List list=null; for(int i=0;i<f.size( );i++){ 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"); s.add("abc"); s.add("abcd"); s.add("ABC"); 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() |
|
3、 判断题
4、 简答题 (首先熟练掌握笔记上 与集合相关的面试简答题)
1.熟练掌握Collection集合和Map集合的体系图。
2.简述List、Set、Collection、Map的特色和区别及何时使用该集合。
Collection
List(存取有序,有索引,能够重复)
ArrayList
底层是数组实现的,线程不安全,查找和修改快,增和删比较慢
LinkedList
底层是链表实现的,线程不安全,增和删比较快,查找和修改比较慢
Vector
底层是数组实现的,线程安全的,不管增删改查都慢
若是查找和修改多,用ArrayList
若是增和删多,用LinkedList
若是都多,用ArrayList
Set(存取无序,无索引,不能够重复)
HashSet
底层是哈希算法实现
LinkedHashSet
底层是链表实现,可是也是能够保证元素惟一,和HashSet原理同样
TreeSet
底层是二叉树算法实现
通常在开发的时候不须要对存储的元素排序,因此在开发的时候大多用HashSet,HashSet的效率比较高
TreeSet在面试的时候比较多,问你有几种排序方式,和几种排序方式的区别
Map
HashMap
底层是哈希算法,针对键
LinkedHashMap
底层是链表,针对键
TreeMap
底层是二叉树算法,针对键
开发中用HashMap比较多
3.Vector和ArrayList的区别和联系。
ArrayList 底层是数组实现的,线程不安全,查找和修改快,增和删比较慢
Vector 底层是数组实现的,线程安全的,不管增删改查都慢
4.请你简述HashMap和Hashtable的区别?
HashMap是非synchronized 效率高
Hashtable是线程安全的也是synchronized ,效率低
5、 编码题 (首先熟练掌握笔记上 与集合相关的需求小案例)
package com.zuikc.bean; public class Book { private int id; private String name; private double price; private String publisher; public Book() { super(); } public Book(int id, String name, double price, String publisher) { super(); this.id = id; this.name = name; this.price = price; this.publisher = publisher; } 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 double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } @Override public String toString() { return "Books_Message [id=" + id + ", name=" + name + ", price=" + price + ", publisher=" + publisher + "]"; } } package com.zuikc.demo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import com.zuikc.bean.Book; public class Books { public static void main(String[] args) { Book b1 = new Book(1000, "b1", 30.5, "adddf"); Book b1_1 = new Book(1000, "b1", 30, "fnf"); Book b2 = new Book(1000, "b2", 50, "hfh"); Book b3 = new Book(1001, "b3", 30.5, "fnnf"); Book b4 = new Book(1002, "b4", 30.5, "drtt"); Book b5 = new Book(1003, "b5", 50, "rtyh"); // 使用HashSet存储图书并遍历 List<Book> bookList = new ArrayList<Book>(); bookList.add(b1); bookList.add(b1); bookList.add(b2); bookList.add(b3); bookList.add(b4); bookList.add(b5); bookList.add(b1_1); System.out.println("遍历输出hashset"); System.out.println(bookList.size()); for (Book book : bookList) { System.out.println(book.toString()); } // 使用TreeSet存储图书并遍历 Map<Integer, Book> bookMap = new HashMap<Integer, Book>(); bookMap.put(b1.getId(), b1); bookMap.put(b1.getId(), b1); bookMap.put(b2.getId(), b2); bookMap.put(b3.getId(), b3); bookMap.put(b4.getId(), b4); bookMap.put(b5.getId(), b5); bookMap.put(b1_1.getId(), b1_1); System.out.println("遍历输出treeset"); for (Entry<Integer, Book> entry : bookMap.entrySet()) { System.out.println(entry.getKey() + "----------->" + entry.getValue()); } } }
提示:向HashSet中添加自定义类的对象信息,须要重写hashCode和equals( )
向TreeSet中添加自定义类的对象信息,须要实现Comparable接口,指定比较规则
package com.zuikc.bean; public class Book implements Comparable<Book> { private int id; private String name; private double price; private String publisher; public Book() { super(); } public Book(int id, String name, double price, String publisher) { super(); this.id = id; this.name = name; this.price = price; this.publisher = publisher; } public int getId() { return id; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + ((publisher == null) ? 0 : publisher.hashCode()); return result; } @Override public boolean equals(Object obj) { 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 (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) return false; if (publisher == null) { if (other.publisher != null) return false; } else if (!publisher.equals(other.publisher)) return false; return true; } public void setId(int 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 getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } @Override public String toString() { return "Books_Message [id=" + id + ", name=" + name + ", price=" + price + ", publisher=" + publisher + "]"; } @Override public int compareTo(Book o) { return this.id - o.id; } } package com.zuikc.demo; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; import com.zuikc.bean.Book; public class Books { public static void main(String[] args) { Book b1 = new Book(1000, "b1", 30.5, "adddf"); Book b1_1 = new Book(1000, "b1", 30, "fnf"); Book b2 = new Book(1000, "b2", 50, "hfh"); Book b3 = new Book(1001, "b3", 30.5, "fnnf"); Book b4 = new Book(1002, "b4", 30.5, "drtt"); Book b5 = new Book(1003, "b5", 50, "rtyh"); // 使用HashSet存储图书并遍历 Set<Book> hashSet = new HashSet<Book>(); 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 (Book book : hashSet) { System.out.println(book.toString()); } // 使用TreeSet存储图书并遍历 Set<Book> treeSet = new TreeSet<Book>(); 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"); for (Book book : treeSet) { System.out.println(book); } } }
功能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 com.zuikc.bean; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import java.util.Set; public class Student { public int id; String name; int age; String sex; public Student() { super(); } public Student(int id, String name, int age, String sex) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; } public void listToMap() { Student x1 = new Student(1, "张三", 12, "男"); Student x2 = new Student(9, "李四", 22, "男"); Student x3 = new Student(10, "王五", 25, "男"); Student x4 = new Student(16, "张六", 22, "男"); Student x5 = new Student(17, "赵六", 19, "男"); Student x6 = new Student(20, "钱七", 22, "男"); List<Student> al = new ArrayList<Student>(); al.add(x1); al.add(x2); al.add(x3); al.add(x4); al.add(x4); al.add(x5); al.add(x6); for (Student x : al) { System.out.println(x); } HashMap<Integer, Student> hm = new HashMap<Integer, Student>(); Iterator<Student> it = al.iterator(); while (it.hasNext()) { Student x = it.next(); hm.put(x.id, x); } Set<Entry<Integer, Student>> en = hm.entrySet(); for (Entry<Integer, Student> e : en) { System.out.println("key:" + e.getKey() + "valuey:" + e.getValue()); } } } package com.zuikc.bean; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import java.util.Set; public class StudentEntry extends Student { public StudentEntry() { super(); } public StudentEntry(int id, String name, int age, String sex) { super(id, name, age, sex); } @Override public String toString() { return "StudentEntry [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; } public void mapToList() { StudentEntry x1 = new StudentEntry(1, "张三", 12, "男"); StudentEntry x2 = new StudentEntry(9, "李四", 22, "男"); StudentEntry x3 = new StudentEntry(10, "王五", 25, "男"); StudentEntry x4 = new StudentEntry(16, "张六", 22, "男"); StudentEntry x5 = new StudentEntry(17, "赵六", 19, "男"); StudentEntry x6 = new StudentEntry(20, "钱七", 22, "男"); HashMap<Integer, StudentEntry> hm = new HashMap<Integer, StudentEntry>(); hm.put(x1.id, x1); hm.put(x2.id, x2); hm.put(x3.id, x3); hm.put(x4.id, x4); hm.put(x5.id, x5); hm.put(x6.id, x6); Set<Entry<Integer, StudentEntry>> en = hm.entrySet(); List<StudentEntry> al = new ArrayList<StudentEntry>(); for (Entry<Integer, StudentEntry> e : en) { System.out.println(e); al.add(e.getValue()); } for (StudentEntry x : al) { System.out.println(x); } } } package com.zuikc.demo; import com.zuikc.bean.Student; import com.zuikc.bean.StudentEntry; public class AA { public static void main(String[] args) { Student x = new Student(); x.listToMap(); StudentEntry x1 = new StudentEntry(); x1.mapToList(); } }