首页 > 学院 > 开发设计 > 正文

基础复习之容器

2019-11-06 08:27:55
字体:
来源:转载
供稿:网友

一、 Collection接口

/**

 *

 * 容器:

 * --interface--|1. Collection 单列的元素集合

 * ------------------|1.1 List 有序、可以重复

 * ------------------|1.2 Set  无序、不可重复

 * ------------------|Queue 不是重点,暂时理解为数据结构中的队列!

 *

 * @author Genuine

 *  学习collection:集合的核心api还是离不开curd,有区别的地方就是多了迭代。还少了查找!

 *  核心Api学习:

 *      增:

 *          add(E e)

 *          addAll(Collection<? extends E> c)

 *      删:

 *          clear()

 *          remove(Object o)

 *          removeAll(Collection<?> c)

 *          retainAll(Collection<?> c)

 *      查:

 *          contains(Object o) :关注下!

 *          containsAll(Collection<?> c)

 *          equals(Object o)

 *          isEmpty()

 *          size()

 *      迭代:

 *          iterator()

 *          toArray()

 */

         //2.方法练习汇总

         //a. add()         no.1 ArrayList到什么时候才扩容呢?

     //   c.add("小李");

         c.add("小王");

         c.add("小黄");

         //b.addAll()

         Collection c2 = newArrayList();

         c2.add("小王");

         c2.add("大黄");

         c.addAll(c2);

         //c. clear()     no.2什么时候才回收null对象呢??

         //c2.clear();  //所有元素清空,然后让GC回收null

         //d. remove()

         //c2.remove("大黄");

         //e. removeAll()  移除交集

         //c.removeAll(c2);

         //f. retainAll(c)  保留交集,且复制c集合到原集合中

         c.retainAll(c2);    //    no.3这个方法源代码没有看懂

         System.out.PRintln(c);

        

     }

/**

   @authorGneuine

  

   Collection 剩下三个方法的练习:

   1.hasNext():当前游标有元素吗?

   2.next():获取当前游标元素,游标往下移动

   3.remove():获取最后一个返回的元素。

 

 */

public class Demo4 {

    

    

     @Test

     public void testIterator() throws Exception {

        

         Collection c = newArrayList();

        

         c.add("老贾");

         c.add("老黄");

         c.add("小玉");

         c.add("小强");

        

         //获取迭代器

         Iterator it = c.iterator();

         /**

            hasNext()方法源码解读:

                   int cursor;游标默认指向第一个元素

                    public boolean hasNext() {

                     return cursor != size;

               }

                非常巧妙的获取到当前游标是否元素。

          */

         //1)hasNext()

//       System.out.println(it.hasNext());

         /**

            next()方法源码解读:    

                public E next() {          

                     int i = cursor;  //先保存当前游标

                     cursor = i + 1;  //游标下移

                     return (E) elementData[i];  //但是获取到的是当前游标。

                }

          */

         //2)next()

//       System.out.println(it.next());

        

         //3)hasNext()与next()组合使用

//       while(it.hasNext()) {

//            System.out.print(it.next()+" ");

//       }

         /**

            remove()方法源码解读:

            public void remove() {

                   //return (E) elementData[lastRet = i]; //从这个可以看出每获取一个元素,就把最后游标移动到这个位置

                ArrayList.this.remove(lastRet);  //移除最后一个返回的元素

                cursor = lastRet;                //游标指向最后这个元素的位置

                lastRet = -1;                     //记录最后这个位置的游标重置

                expectedModCount = modCount;   

           }

          */

         //4)remove()方法的使用

//       it.next();

//       it.remove();

//       while(it.hasNext()) {

//       System.out.print(it.next()+" ");

//       }

     }

}

大三下学期开始了,应该为面试而准备了。白天练习项目的同时,要把基础也巩固巩固~

这些知识内容是以前看过传智的视频加上编程思想书里的内容,重在练习~小强加油!

 

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表