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

  • 相关阅读:
    Java基础教程
    一个RDBMS左连接SQL执行计划解析
    hive时间日期函数及典型场景应用
    ETL数据采集方法
    数据仓库保存历史数据方法之拉链表
    NAS服务器局域网内IPad、手机、电视盒子等联网播放
    转:主流数据恢复软件——EasyRecovery/Ashampoo Undeleter/Wise Data Recovery/Recuva/Undelete 360
    [转]office2010一直卡在“正在受保护的视图中打开”
    [转]PROE传动链条的装配教程
    linux下svn定时更新项目
  • 原文地址:https://www.cnblogs.com/wangxh92/p/3708231.html
Copyright © 2011-2022 走看看