zoukankan      html  css  js  c++  java
  • java 集合基础1 学习笔记

    集合特点:

      1.用于存储对象的容器。

      2.集合的长度是可变的。

      3.集合中不可以存储基本数据类型值。

    Collection接口常见方法:

    1.添加

      boolean add(obj);

      boolean addAll(Collection coll);

    2.删除

      boolean remove(obj);

      boolean removeAll(Collection coll);

      void clear();//清空集合

    3.判断

      boolean contains(obj);

      boolean containsAll(Collection coll);

      boolean isEmpty();判断集合中是否有元素

    4.获取:

      int size();

      Iterator iterator();取出元素的方式:迭代器

      该对象必须依赖具体容器,因为每一个容器的数据结构都不同。

      所以该迭代器对象是在容器中进行内部实现的。

      调用iterator()方法,获取集合中的迭代器对象。

      Iterator it =coll.iterator();

      while(it.hasNext()){

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

      }  

      for(Iterator it = coll.iterator();it.hasNext();){

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

      }

    在迭代器使用过程中,使用集合操作元素,会出现异常。

    可以使用Iterator接口的子接口ListIterator来完成在迭代中对元素进行操作。

    ListIterator it = list.listIterator();

    5.其他

      boolean retainAll(Collection coll);取交集。

      Object toArray();将集合转成数组。

    List:

      1.Vector:内部是数组数据结构,是同步的,线程安全。增删查询都很慢。

      Vector v= new Vector();

      v.addElement("aa");  //v.add("aa");

      2.ArrayList:内部是数组数据结构,不同步,替代了Vector,查询速度快。

        初始化时,容量为10。

      3.LinkedList:内部是链表数据结构,是不同步的。增删元素的速度很快。

        LinkedList link=new LinkedList();

        link.addFirst("ab1");    

        link.addLast("ab2");

        link.getFirst();  //获取第一个但不删除

        link.removeFirst();   //获取元素并删除

        while(!link.isEmpty){

          System.out.println(link.removeFirst());

        }

        addFirst();

    1.6:  offerFirst();

        addLast();

    1.6:  offerLast();

        getFrist();   //如果链表为空,抛出异常NoSuchElementException

    1.6:  peekFirst(); //如果链表为空,返回null

        getLast();  //如果链表为空,抛出异常NoSuchElementException

    1.6:  peekLast();//如果链表为空,返回null

        

        removeFirst();//获取并移除,如果链表为空,抛出异常NoSuchElementException

    1.6:  pollFirst(); //获取并移除,如果链表为空,返回null

        removeLast();//获取并移除,如果链表为空,抛出异常NoSuchElementException

    1.6:  pollLast(); //获取并移除,如果链表为空,返回null

    Coiiection 

      1.List  有序,存入和取出的顺序一致,元素都有索引,元素可以重复

      2.Set  元素不能重复,无序。

    List:常见方法

    1.添加

      void add();

    2.删除

      Object remove();

    3.修改

      Object set(index,element);

    4.获取

      Object get(index);

      int indexOf(Object);

      int lastIndexOf(Object);

      List subList(from,to);

  • 相关阅读:
    gearman简介及安装使用
    Linux下进程的建立
    Linux中无缓冲文件I/O API
    XMLRPC for PHP简介及使用
    数据库水平切分方法
    LVS简介及使用
    进程通信之消息队列
    install docker/dockercompose
    centOS7 下安装 JDK
    iTerm2/firewall/pulsar
  • 原文地址:https://www.cnblogs.com/wangxh92/p/3708231.html
Copyright © 2011-2022 走看看