Java容器题库


一、    填空题

  1. Java集合框架提供了一套性能优良、使用方便的接口和类,包括Collection和Map两大类,它们都位于  java.util  包中
  2. 队列和堆栈有些相似,不同之处在于栈是先进后出,队列是先进先出
  3.   链表  结构是一种由多个节点组成的线性数据结构,并且每个节点包含有数据以及指向下一个节点的引用。
  4. ___LinkedList__是一种集合类,它 采用链表作为的存储结构,便于删除和添加元素,但是按照索引查询元素效率低下。
  5.   TreeSet  是一种Collection类型的集合类,其中元素唯一,并采用二叉树作为存储结构,元素按照自然顺序排列。
  6. 如果希望将自定义类Student的多个对象放入集合TreeSet,实现所有元素按照某个属性的自然顺序排列,则需要Student类实现__Comparable__接口。
  7. 在Java中  HashMap  集合的访问时间接近稳定,它是一种键值对映射的数据结构。这个数据结构是通过数组来实现的。
  8. 迭代器Iterator为集合而生,专门实现集合遍历,该接口有三个方法,分别是hasNext() 、__next()_、remove()。

二、    选择题

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;

                   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()

       

三、    判断题

  1. 数组和集合中的元素可以是任何数据类型,包括基本类型和引用类型。(  F  )
  2. Java集合中的Set接口和List接口都是从Collection接口派生出来的。(  T  )
  3. Collection 接口存储一组不唯一,有序的对象,它有两个子接口:List和Set。(  F  )
  4. Collection是Java集合顶级接口,其中的元素无序,唯一。Java平台不提供这个接口任何直接的实现。(  F  )
  5. List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引来访问List中的无素,这类似于Java的数组。(  T  )
  6. HashSet采用哈希表存储结构,特点是查询速度快,但是其中元素无序排列。(  T  )
  7. LinkedHashMap是一种有序的HashMap,查询速度快,便于添加删除操作。(  T  )
  8. 基本数据类型的值可以被直接存储在Vector对象中。(  F  )
  9. Dictionary建立了关键字和值的映射,只要提供一个关键字,Dictionary就会返回一个相应的值。(  T  )
  10. 泛型是JavaSE1.7的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。Java语言引入泛型的好处是安全简单。(  F  )
  11. Collection是专门操作集合的工具类,提供一系列静态方法实现对各种集合操作。(  F  )
  12. Iterator接口可以遍历任何Collection接口的实现类,可以从一个Collection中使用iterator( )方法来获取迭代器实例。迭代器取代了Java集合框架中的Enumeration。(  T  )

四、    简答题

  1. 集合和数组的比较
  2. 简述List、Set、Collection、Map的区别和联系。
  3. ArrayList和LinkedList的区别和联系。
  4. HashSet采用了哈希表作为存储结构,请说明哈希表的特点
  5. Vector和ArrayList的区别和联系。
  6. 请你简述HashMap和Hashtable的区别? 

五、    编码题

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

 

 1 public class Book  2 {  3     public int id;  4     public String name;  5     public double price;  6     public String press;  7     public Book()  8  {  9         super(); 10  } 11     public Book(int id, String name, double price, String press) 12  { 13         super(); 14         this.id = id; 15         this.name = name; 16         this.price = price; 17         this.press = press; 18  } 19     public int getId() 20  { 21         return id; 22  } 23     public void setId(int id) 24  { 25         this.id = id; 26  } 27     public String getName() 28  { 29         return name; 30  } 31     public void setName(String name) 32  { 33         this.name = name; 34  } 35     public double getPrice() 36  { 37         return price; 38  } 39     public void setPrice(double price) 40  { 41         this.price = price; 42  } 43     public String getPress() 44  { 45         return press; 46  } 47     public void setPress(String press) 48  { 49         this.press = press; 50  } 51  @Override 52     public String toString() 53  { 54         return "Book [id=" + id + ", name=" + name + ", press=" + press 55                 + ", price=" + price + "]"; 56  } 57 } 58 public class TestListMap 59 { 60     public static void main(String[] args) 61  { 62         Book b1 = new Book(1000, "b1", 30.5, "bjsxt"); 63         Book b1_1 = new Book(1000, "b1", 30, "bjsxt"); 64         Book b2 = new Book(1000, "b2", 50, "bjsxt"); 65         Book b3 = new Book(1001, "b3", 30.5, "bjsxt"); 66         Book b4 = new Book(1002, "b4", 30.5, "bjsxt"); 67         Book b5 = new Book(1003, "b5", 50, "bjsxt1"); 68         //使用HashSet存储图书并遍历
69         List<Book> bookList = new ArrayList<Book>(); 70  bookList.add(b1); 71  bookList.add(b1); 72  bookList.add(b2); 73  bookList.add(b3); 74  bookList.add(b4); 75  bookList.add(b5); 76  bookList.add(b1_1); 77         System.out.println("遍历输出hashset"); 78  System.out.println(bookList.size()); 79         for (Book book : bookList) 80  { 81  System.out.println(book.toString()); 82  } 83         //使用TreeSet存储图书并遍历
84         Map<Integer, Book> bookMap = new HashMap<Integer, Book>(); 85  bookMap.put(b1.getId(), b1); 86  bookMap.put(b1.getId(), b1); 87  bookMap.put(b2.getId(), b2); 88  bookMap.put(b3.getId(), b3); 89  bookMap.put(b4.getId(), b4); 90  bookMap.put(b5.getId(), b5); 91  bookMap.put(b1_1.getId(), b1_1); 92         System.out.println("遍历输出treeset"); 93         for (Entry<Integer, Book> entry : bookMap.entrySet()) 94  { 95             System.out.println(entry.getKey() + "----------->" + entry.getValue()); 96  } 97  } 98 }

2.使用HashSet和TreeSet存储多个商品信息,遍历并输出;其中商品属性:编号,名称,单价,出版社;要求向其中添加多个相同的商品,验证集合中元素的唯一性。

提示:向HashSet中添加自定义类的对象信息,需要重写hashCode和equals( )

          向TreeSet中添加自定义类的对象信息,需要实现Comparable接口,指定比较规则

 

 1 public class Book implements Comparable<Book>
 2 {  3     public int id;  4     public String name;  5     public double price;  6     public String press;  7     public Book()  8  {  9         super();  10  }  11     public Book(int id, String name, double price, String press)  12  {  13         super();  14         this.id = id;  15         this.name = name;  16         this.price = price;  17         this.press = press;  18  }  19     public int compareTo(Book o)  20  {  21         return this.id - o.id;  22  }  23  @Override  24     public int hashCode()  25  {  26         final int prime = 31;  27         int result = 1;  28         result = prime * result + id;  29         result = prime * result + ((name == null) ? 0 : name.hashCode());  30         result = prime * result + ((press == null) ? 0 : press.hashCode());  31         long temp;  32         temp = Double.doubleToLongBits(price);  33         result = prime * result + (int) (temp ^ (temp >>> 32));  34         return result;  35  }  36  @Override  37     public boolean equals(Object obj)  38  {  39         if (this == obj)  40  {  41             return true;  42  }  43         if (obj == null)  44  {  45             return false;  46  }  47         if (getClass() != obj.getClass())  48  {  49             return false;  50  }  51         Book other = (Book) obj;  52         if (id != other.id)  53  {  54             return false;  55  }  56         if (name == null)  57  {  58             if (other.name != null)  59  {  60                 return false;  61  }  62         } else if (!name.equals(other.name))  63  {  64             return false;  65  }  66         if (press == null)  67  {  68             if (other.press != null)  69  {  70                 return false;  71  }  72         } else if (!press.equals(other.press))  73  {  74             return false;  75  }  76         if (Double.doubleToLongBits(price) != Double  77  .doubleToLongBits(other.price))  78  {  79             return false;  80  }  81         return true;  82  }  83  @Override  84     public String toString()  85  {  86         return "Book [id=" + id + ", name=" + name + ", press=" + press  87                 + ", price=" + price + "]";  88  }  89 }  90 public class TestSet  91 {  92     public static void main(String[] args)  93  {  94         Book b1 = new Book(1000, "b1", 30.5, "bjsxt");  95         Book b1_1 = new Book(1000, "b1", 30, "bjsxt");  96         Book b2 = new Book(1000, "b2", 50, "bjsxt");  97         Book b3 = new Book(1001, "b3", 30.5, "bjsxt");  98         Book b4 = new Book(1002, "b4", 30.5, "bjsxt");  99         Book b5 = new Book(1003, "b5", 50, "bjsxt1"); 100         //使用HashSet存储图书并遍历
101         Set<Book> hashSet = new HashSet<Book>(); 102  hashSet.add(b1); 103  hashSet.add(b1); 104  hashSet.add(b2); 105  hashSet.add(b3); 106  hashSet.add(b4); 107  hashSet.add(b5); 108  hashSet.add(b1_1); 109         System.out.println("遍历输出hashset"); 110  System.out.println(hashSet.size()); 111         for (Book book : hashSet) 112  { 113  System.out.println(book.toString()); 114  } 115         //使用TreeSet存储图书并遍历
116         Set<Book> treeSet = new TreeSet<Book>(); 117  treeSet.add(b1); 118  treeSet.add(b1); 119  treeSet.add(b2); 120  treeSet.add(b3); 121  treeSet.add(b4); 122  treeSet.add(b5); 123  treeSet.add(b1_1); 124         System.out.println("遍历输出treeset"); 125         for (Book book : treeSet) 126  { 127  System.out.println(book.toString()); 128  } 129  } 130 }

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对象

 

 1 public class TestListToMap  2 {  3     public void listToMap()  4  {  5         //1.创建多个学生信息
 6         Student stu1 = new Student(110, "小明", 23, 98.0);  7         Student stu2 = new Student(111, "大刚", 21, 80.5);  8         Student stu3 = new Student(112, "小白", 12, 93.0);  9         //2.加入List
10         List<Student> list = new ArrayList<Student>(); 11  list.add(stu1); 12  list.add(stu2); 13  list.add(stu3); 14         //3.遍历List,输出每个Student信息
15         for (Student stu : list) 16  { 17  System.out.println(stu); 18  } 19         //4.将List中数据放入Map,使用Student的id属性作为key Map<Integer, Student> map = new HashMap<Integer, Student>();
20         Iterator<Student> it = list.iterator(); 21         while (it.hasNext()) 22  { 23             Student stu = it.next(); 24  map.put(stu.getId(), stu); 25  } 26         //5.遍历Map,输出每个Entry的key和value
27         Set<Entry<Integer, Student>> entrySet = map.entrySet(); 28         for (Entry<Integer, Student> entry : entrySet) 29  { 30             System.out.println(entry.getKey() + "---->" + entry.getValue()); 31  } 32  } 33 } 34 public class StudentEntry 35 { 36     private int key;//关键字
37     private Student stu;//学生
38     public int getKey() 39  { 40         return key; 41  } 42     public void setKey(int key) 43  { 44         this.key = key; 45  } 46     public Student getStu() 47  { 48         return stu; 49  } 50     public void setStu(Student stu) 51  { 52         this.stu = stu; 53  } 54 } 55 public class TestMapToList 56 { 57     public void mapToList() 58  { 59         //1.创建多个学生信息
60         Student stu1 = new Student(110, "小明", 23, 98.0); 61         Student stu2 = new Student(111, "大刚", 21, 80.5); 62         Student stu3 = new Student(112, "小白", 12, 93.0); 63         //2.使用Student的id属性作为key,存入Map
64         Map<Integer, Student> map = new HashMap<Integer, Student>(); 65  map.put(stu1.getId(), stu1); 66  map.put(stu2.getId(), stu2); 67  map.put(stu2.getId(), stu3); 68         //3.创建List对象,每个元素类型是StudentEntry
69         List<StudentEntry> list = new ArrayList<StudentEntry>(); 70         //4.将Map对象转化为List集合
71         for (Entry<Integer, Student> entry : map.entrySet()) 72  { 73             StudentEntry studentEntry = new StudentEntry(); 74             // 将map中的一个映射关系,封装为一个studentEntry对象
75  studentEntry.setKey(entry.getKey()); 76  studentEntry.setStu(entry.getValue()); 77             // 将studentEntry对象List集合
78  list.add(studentEntry); 79  } 80         //5.遍历Map
81         for (StudentEntry se : list) 82  { 83             System.out.println(se.getKey() + "\t" + se.getStu()); 84  } 85  } 86

六、    可选题

1.假如有以下email数据“aa@sohu.com,bb@163.com,cc@sina.com,..”现需要把email中的用户部分和邮件地址部分分离,分离后以键值对应的方式放入HashMap?

 

 1 public class EmailSplit  2 {  3     public static void main(String[] args)  4  {  5         String str = "aa@sohu.com,bb@163.com,cc@sina.com";  6         //得到每一个email
 7         String strs[] = str.split(",");  8         //存放分离后email的信息
 9         Map<String, String> emailMap = new HashMap<String, String>(); 10         for (String email : strs) 11  { 12             String temp[] = email.split("@"); 13             //分割email存入map
14             emailMap.put(temp[0], temp[1]); 15  } 16  System.out.println(emailMap.toString()); 17  } 18 }

2.由控制台按照固定格式输入学生信息,包括学号,姓名,年龄信息,当输入的内容为exit退出;将输入的学生信息分别封装到一个Student对象中,再将每个Student对象加入到一个集合中,要求集合中的元素按照年龄大小正序排序;最后遍历集合,将集合中学生信息写入到记事本,每个学生数据占单独一行。

推荐步骤:

a)        创建Student类,并指定按照年龄正序排列

b)        通过控制台输入多个不同Student信息。格式规定为:编号#姓名#年龄

c)         取出字符串中相应信息放入Student对象,并将Student加入到集合中

d)        遍历集合的过程中将学生的信息输入到记事本

难点:

e)        如何指定学生按照年龄正序排列

f)          如果从字符串“编号#姓名#年龄”中提取学生信息

g)        放入哪种集合后可以保证学生按照年龄大小正序排列

h)        如何将集合中学生信息写入记事本,每个学生数据占单独一行

 

 1 public class Student implements Comparable<Student>
 2 {  3     private Integer num;  4     private String name;  5     private Integer age;  6     //省略getter和setter方法  7     //省略构造方法 
 8     public int compareTo(Student stu)  9  { 10         return this.age - stu.age; 11  } 12     public String toString() 13  { 14         return "Student [age=" + age + ", name=" + name 15                 + ", num=" + num + "]"; 16  } 17 } 18 public class Test 19 { 20     public static void main(String[] args) 21  { 22         //保存输入信息到set中
23         Set<Student> stuSet = saveStudentInfo(); 24         //遍历set
25         Iterator<Student> it = stuSet.iterator(); 26         while (it.hasNext()) 27  { 28             String info = it.next().toString(); 29  System.out.println(info); 30  } 31  } 32     private static Set<Student> saveStudentInfo() 33  { 34         Scanner input = new Scanner(System.in); 35         // 保存学生信息的TreeSet集合对象
36         Set<Student> stuSet = new TreeSet<Student>(); 37         while (true) 38  { 39             // 输入提示
40             System.out.println("请输入学生信息:(学号#姓名#年龄)"); 41             String inputData = input.nextLine(); 42             // 判断是否退出 inputData.equals("exit")
43             if ("exit".equals(inputData)) 44  { 45                 break; 46  } 47             // 将用户输入的学生信息分割为String[]
48             String[] info = inputData.split("#"); 49             // 将输入信息封装到Student对象中
50  Student stu 51                     = new Student(Integer.parseInt(info[0]), info[1], 52                             Integer.parseInt(info[2])); 53             // 将学生对象加入集合
54  stuSet.add(stu); 55  } 56         return stuSet; 57  } 58 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM