• day17作业



    容器作业
    一、填空题
    1.Java集合框架提供了一套性能优良、使用方便的接口和类,包括Collection和Map两大类,它们都位于 _____util________ 包中
    2.队列和堆栈有些相似,不同之处在于 _队列是先进先出,堆栈是先进后出___________ 。
    3. _____链表________ 结构是一种由多个节点组成的线性数据结构,并且每个节点包含有数据以及指向下一个节点的引用。
    4._LinkedList_____________是一种集合类,它?采用链表作为的存储结构,便于删除和添加元素,但是按照索引查询元素效率低下。
    5. ___TreeSet__________ 是一种Collection类型的集合类,其中元素唯一,并采用二叉树作为存储结构,元素按照自然顺序排列。
    6.如果希望将自定义类Student的多个对象放入集合TreeSet,实现所有元素按照某个属性的自然顺序排列,则需要Student类实现__Comparator________________接口。
    7.在Java中 _map____________ 集合的访问时间接近稳定,它是一种键值对映射的数据结构。这个数据结构是通过数组来实现的。
    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; //3.4f
    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
    }
    }
    return rtn;
    }
    }

    A 0
    B. 1
    C. 2
    D. 3

    4. 分析如下Java 代码,编译运行后将输出( D )。(选择一项)
    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中,下列集合类型可以存储无序、不重复的数据的是(C )。(选择一项)

    A ArrayList
    B. LinkedList
    C. TreeSet
    D. HashSet

    6. 以下代码的执行结果是( D )。(选择一项)
    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代码,编译运行的结果是(D )。(选择一项)
    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.数组和集合中的元素可以是任何数据类型,包括基本类型和引用类型。( T )
    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就会返回一个相应的值。( F )
    10.泛型是JavaSE1.7的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。Java语言引入泛型的好处是安全简单。(F )
    11.Collection是专门操作集合的工具类,提供一系列静态方法实现对各种集合操作。( T )
    12.Iterator接口可以遍历任何Collection接口的实现类,可以从一个Collection中使用iterator( )方法来获取迭代器实例。迭代器取代了Java集合框架中的Enumeration。( T )

    四、简答题 (首先熟练掌握笔记上 与集合相关的面试简答题)
    1.熟练掌握Collection集合和Map集合的体系图
    Collection集合
    collection 接口 子接口为list和set
    list接口下子类有ArrayList 和LinkedList以及vector
    set接口下子类有HashSet 和TreeSet 以及Hashtable
    HashSet类下有LinkedHashSet子类

    Map集合
    HashMap和TreeMap


    2.简述List、Set、Collection、Map的特点和区别及什么时候使用该集合。
    List 列表 特点:存取有序 有索引 可以重复
    Set 存取无序 没有索引 唯一
    Collection为接口,List 和Set是其子接口 单列集合
    Map 双列集合 包括键值对
    3.Vector和ArrayList的区别和联系。
    联系:底层都是数组实现
    区别:ArrayList线程不安全,查找和修改块,增删比较慢
    vector 线程安全,增删改查都慢 一般不用
    4.请你简述HashMap和Hashtable的区别?
    HashMap 底层实现是哈希算法,针对键,跟值无关 可以存null
    Hashtable 不能存null


    五、编码题 (首先熟练掌握笔记上 与集合相关的需求小案例)

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

    * 使用List存放图书信息

    package com.zuikc.bean;
    
    public class Book {
        private int num;
        private String name;
        private float price;
        private String press;
        public Book() {
            super();
            
        }
        public Book(int num, String name, float price, String press) {
            super();
            this.num = num;
            this.name = name;
            this.price = price;
            this.press = press;
        }
        public int getNum() {
            return num;
        }
        public void setNum(int num) {
            this.num = num;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public float getPrice() {
            return price;
        }
        public void setPrice(float price) {
            this.price = price;
        }
        public String getPress() {
            return press;
        }
        public void setPress(String press) {
            this.press = press;
        }
        @Override
        public String toString() {
            return "Book [num=" + num + ", name=" + name + ", price=" + price + ", press=" + press + "]";
        }
        
        
    }
    
    package com.zuikc.test;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    import com.zuikc.bean.Book;
    
    public class Demo_Test1 {
    /*
     * 使用List存放图书信息
     * 2.创建List集合
     * 3.遍历并输出集合
     */
        public static <E> void main(String[] args) {
            List list = new ArrayList();
            list.add(new Book(1111,"平凡的世界",58.8f,"人民出版社"));
            list.add(new Book(1112,"活着",48.8f,"江苏出版社"));
            list.add(new Book(1113,"檀香刑",56.8f,"山东出版社"));
            list.add(new Book(1114,"一只特立独行的猪",38.8f,"北京出版社"));
            list.add(new Book(1115,"红玫瑰与白玫瑰",28.8f,"上海出版社"));
            list.add(new Book(1116,"笑傲江湖",58.8f,"陕西出版社"));
            
            Iterator<E> it = list.iterator();
            while(it.hasNext()) {
                System.out.println(it.next());
            }
        }
    
    }
    
    import java.security.KeyStore.Entry;
    import java.util.HashMap;
    
    import com.zuikc.bean.Book;
    
    public class Demo_Book {
    
        public static void main(String[] args) {
            HashMap<Integer, Book> hm = new HashMap<>();
            hm.put(1111,new Book(1111,"平凡的世界",58.8f,"人民出版社"));
            hm.put(1112,new Book(1112,"活着",48.8f,"江苏出版社"));
            hm.put(1113,new Book(1113,"檀香刑",56.8f,"山东出版社"));
            hm.put(1114,new Book(1114,"一只特立独行的猪",38.8f,"北京出版社"));
            hm.put(1115,new Book(1115,"红玫瑰与白玫瑰",28.8f,"上海出版社"));
            hm.put(1116,new Book(1116,"笑傲江湖",58.8f,"陕西出版社"));
            
            for(Integer Key: hm.keySet()) {
                System.out.println(Key + "=" + hm.get(Key));
            }
        }
    
    }


    * 2.创建List集合
    * 3.遍历并输出集合
    */

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

    package com.zuikc.bean;
    
    public class Book {
        private int num;
        private String name;
        private float price;
        private String press;
        public Book() {
            super();
            
        }
        public Book(int num, String name, float price, String press) {
            super();
            this.num = num;
            this.name = name;
            this.price = price;
            this.press = press;
        }
        public int getNum() {
            return num;
        }
        public void setNum(int num) {
            this.num = num;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public float getPrice() {
            return price;
        }
        public void setPrice(float price) {
            this.price = price;
        }
        public String getPress() {
            return press;
        }
        public void setPress(String press) {
            this.press = press;
        }
        @Override
        public String toString() {
            return "Book [num=" + num + ", name=" + name + ", price=" + price + ", press=" + press + "]";
        }
        @Override
        public int hashCode() {
            return 1;
        }
        @Override
        public boolean equals(Object obj) {
            Book bo = (Book)obj;
            return this.num == bo.num && this.name == bo.getName() && this.price == bo.getPrice() && this.press.equals(bo.press);
            
        }
        
        
    }
    
    package com.zuikc.test;
    
    import java.util.HashSet;
    
    import com.zuikc.bean.Book;
    
    public class Test2_Book {
    
        public static void main(String[] args) {
            HashSet<Book> hs = new HashSet<>();
            hs.add(new Book(1111,"平凡的世界",58.8f,"人民出版社"));
            hs.add(new Book(1111,"平凡的世界",58.8f,"人民出版社"));
            hs.add(new Book(1112,"活着",48.8f,"江苏出版社"));
            hs.add(new Book(1111,"平凡的世界",58.8f,"人民出版社"));
            hs.add(new Book(1114,"一只特立独行的猪",38.8f,"北京出版社"));
            hs.add(new Book(1112,"活着",48.8f,"江苏出版社"));
            hs.add(new Book(1116,"笑傲江湖",58.8f,"陕西出版社"));
            
            System.out.println(hs);
        }
    
    }


    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

    package com.zuikc.bean;
    
    public class Student {
        private int id;
        private String name;
        private int age;
        private 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;
        }
        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 "Student [id=" + id +",name=" + name + ", age=" + age + ", sex=" + sex + "]";
        }
        
        
        
        
    }
    package com.zuikc.test;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    
    import com.zuikc.bean.Student;
    
    public class Demo_ListToMap {
    
        public static void main(String[] args) {
            List<Student> list = new ArrayList<>();
            list.add(new Student(201801,"张三",23,""));
            list.add(new Student(201802,"李四",24,""));
            list.add(new Student(201803,"王五",25,""));
            list.add(new Student(201804,"赵六",26,""));
            list.add(new Student(201805,"周七",23,""));
            
            for (Student student : list) {
                System.out.println(student);
            }
        
            HashMap<Integer, Student> hm = new HashMap<>();
            hm.put(201801, new Student(201801,"张三",23,""));
            hm.put(201802, new Student(201802,"李四",24,""));
            hm.put(201803, new Student(201803,"王五",25,""));
            hm.put(201804, new Student(201804,"赵六",26,""));
            hm.put(201805, new Student(201805,"周七",23,""));
            
            for(Integer key:hm.keySet()) {
                System.out.println(key + "=" + hm.get(key));
            }
            
        }
        
    }


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

    2.由控制台按照固定格式输入学生信息,包括学号,姓名,年龄信息,当输入的内容为exit退出;将输入的学生信息分别封装到一个Student对象中,再将每个Student对象加入到一个集合中,要求集合中的元素按照年龄大小正序排序;最后遍历集合,将集合中学生信息写入到记事本,每个学生数据占单独一行。
    推荐步骤:
    a)创建Student类,并指定按照年龄正序排列
    b)通过控制台输入多个不同Student信息。格式规定为:编号#姓名#年龄
    c)取出字符串中相应信息放入Student对象,并将Student加入到集合中
    d)遍历集合的过程中将学生的信息输入到记事本
    难点:
    e)如何指定学生按照年龄正序排列
    f)如果从字符串“编号#姓名#年龄”中提取学生信息
    g)放入哪种集合后可以保证学生按照年龄大小正序排列
    h)如何将集合中学生信息写入记事本,每个学生数据占单独一行

  • 相关阅读:
    python调用函数
    python递归函数的执行过程
    linux rwx 权限说明
    linux ssh scp免密码
    linux的bash特性
    python3常用的内置函数
    linux清理系统缓存
    vim常用命令
    公司项目安装禅道
    jquery 自定义动画
  • 原文地址:https://www.cnblogs.com/zhangzheng1989/p/9387856.html
Copyright © 2020-2023  润新知