百战程序员8-容器

  1、容器指的是“可以容纳其他对象的对象”,这种说法对吗?

    对的

  2、Collection/Set/List的联系跟区别?

collection接口(list、set)和map接口的区别

  3、Set和List的特点跟区别?

   Set集合是无序的,元素不允许重复。HashSet, LinkedHashSet,TreeSet 可以实现Set接口。

  List可以通过index知道元素的位置,它允许元素的重复。ArrayList, LinkedList, Vector可以实现List接口。

  4、【上机】练习Collection接口中常用的方法

   

  5、【上机】下面的代码,效果一致吗? 分析说明之。

Collection c = new HashSet();

              Collection c2 = new HashSet();

              Apple a = new Apple();

              c.add(a);

              c2.addAll(c); 

//增加另一个容器中所有的元素!      

              Collection c = new HashSet();

              Collection c2 = new HashSet();

              Apple a = new Apple();

              c.add(a);

              c2.add(c);

 

  6、想取两个容器中元素的交集,使用哪个方法?

 

  7、说明isEmpty的作用,并说明下面代码有问题吗?

Collection c = null;

System.out.println(c.isEmpty());

   集合不为空才可以判断

  8、我想定义一个数组。该数组既可以放:Dog对象、也可以放Cat对象、还可以放
        Integer对象,怎么定义?

     数组类型为object

Object[] objArr=new Object[5];
objArr[0]=56;
objArr[1]=123;
objArr[2]="Hello world";
objArr[3]=newDog();
objArr[4]=newCat();

  9、List接口中增加了一些与顺序相关的操作方法,下面两个方法的作用是什么?

add(int index, E element) , get(int index)

   add(int index, E element): 在指定索引添加元素

  get(int index) : 获取指定索引的元素

  10、ArrayList底层使用什么来实现的? LinkedList是用什么实现的?

  11、说出ArrayLIst、LinkedList、Vector的区别。

     List接口一共有三个实现类,分别是ArrayList、Vector和LinkedList。

    ArrayList内部是由数组实现的,适合随机查找和遍历,不适合插入和删除。

    Vector与ArrayList一样,也是通过数组实现的,不同的是它支持线程的同步,即某一时刻只有一个线程能够写Vector,避免多线程同时写而引起的不一致性,但实现同步需要很高的花费,因此,访问它比访问ArrayList慢。

    LinkedList是用链表结构存储数据的,很适合数据的动态插入和删除,随机访问和遍历速度比较慢。另外,他还提供了List接口中没有定义的方法,专门用于操作表头和表尾元素,可以当作堆栈、队列和双向队列使用。

  12、我有一些数据,需要频繁的查询,插入和删除操作非常少,并且没有线程之间的共
        享,使用List下面的哪个实现类好一些?

   Arraylist

  13、【上机】针对List中新增的有关顺序的方法,每个都进行测试。并且使用debug
         来帮助我们理解程序运行。

 

  14、定义Computer类,使用价格排序。(使用Comparable接口)

    定义computer类

    

package collection;

public class Computer implements Comparable{
    private double price;

    @Override
    public String toString() {
        return "Computer{" +
                "price=" + price +
                '}';
    }

    public Computer(double price) {
        super();
        this.price = price;
    }

    @Override
    public int compareTo(Object o) {
        Computer c = (Computer) o;
        //方法调用对象与实参对象比较,即this与c对象比较
        if (this.price>c.price){
            return 1;
        }else if(this.price<c.price){
            return -1;
        }else{
            return 0;
        }
    }
}
View Code

    测试类

 

package collection;

import java.util.Iterator;
import java.util.TreeSet;

public class ComputerTest {
    public static void main(String[] args) {
//       // 创建treeset
        TreeSet<Computer> treeSet = new TreeSet<Computer>();
        //创建computer对象
        Computer c1 = new Computer(12);
        Computer c2 = new Computer(10);
        Computer c3 = new Computer(50);
        Computer c4 = new Computer(5);
        Computer c5 = new Computer(889);
        //把computer对象放入treeset
        treeSet.add(c1);
        treeSet.add(c2);
        treeSet.add(c3);
        treeSet.add(c4);
        treeSet.add(c5);
        //创建迭代器
        Iterator<Computer> it = treeSet.iterator();
        //遍历treeset
        while (it.hasNext()){
            System.out.println(it.next());
        }

    }
}
View Code

                           

  15、equals返回true,hashcode一定相等吗?

     是的

  16、HashSet和TreeSet的区别

     HashSet:

      •  存储结构:采用hashtable哈希表存储结构
      • 优点: 添加,查询,删除快
      • 缺点: 无序     

     TreeSet:

      • 存储结构:二叉树
      • 优点:有序,查询速度快于list,慢于HashSet        

  17、使用HashSet存储自定义对象,为什么需要重写hashCode()和equals()?

     

  18、使用TreeSet存储多个学生数据,实现按照不同属性值进行排序?

  

  19、【上机】说明Comparable接口作用。并定义一个学生类,使用分数来比较大小。

 

  20、Map中,key能否重复?如果重复,会有什么现象?

     key 无序且唯一

    添加重复的key不报错,会直接覆盖

  21、Set和Map的集合类名称相似,有没有内在的联系?

     Hashmap和HashSet 都采用哈希表结构,需要用到hashCode哈希码和equals方法

  22、【上机】综合使用List、Map容器存放如下数据, 并从map中取出“李四”。

     姓名:张三 年龄:18 体重:90 地址:北京

     姓名:李四 年龄:28 体重:50 地址:上海

注:不能使用Javabean封装!

   23、【上机】使用JavaBean封装,完成上个题目的练习

    

  24、【上机】写出List、Set、Map中使用泛型的例子。

 

  25、使用泛型有什么好处?

 

  26、【上机】用代码写出遍历List的四种方式

   

  27、【上机】用代码写出遍历Set的两种方式

   

  28、【上机】用代码写出遍历map的方式

     

  29、采用增强for循环遍历List或者Set,如果List或者Set没有加泛型,能遍历吗?

         能;

  30、如果我想在遍历时删除元素,采用哪种遍历方式最好?

     Iterator接口,接口有remove方法

  31、Iterator是一个接口还是类?

     接口

  32、Collection和Collections有什么区别?

     

  33、资源文件有什么作用?

 

  34、【上机】在src下建立一个资源文件(不包含中文),尝试使用Property类读取里
        面的属性。

 

  35、上机】使用entrySet方法遍历Map。

 

  36、Vector和ArrayList的区别联系

   线程安全、

  非线程安全

  37、Hashtable和HashMap的区别联系

 

  38、Java主要容器的选择依据和应用场合

 

猜你喜欢

转载自www.cnblogs.com/lshaoyu/p/10452799.html
今日推荐