• java数据结构表的学习


    1、表ADTabstract data type

        1)数组

        2)链表

    2、java Collections API中的表(位于java.util)

    1Collection接口

    Collection接口扩展了iterable接口,实现Iterable接口的那些类可以有增强的for循环,该循环施于这些类之上以观察他们的所有项。  

    (表一)

    例:(在Iterable类型上使用for循环)

     

    public static <AnyType> void print(Collection<AnyType> coll){

    for(AnyType item:coll)

    System.out.println(item);

    }

     

     

      2Iterator接口(位于java.util);Iterator的思路是,通过iterator方法,每个集合均可以创建并返回给客户一个实现的Iterator接口对象,并把当前位置的概念在对象内部存储下来。

    (表二)

    表一中的代码会被编译器重写为:

     

    public static <AnyType> void print(Collection<AnyType> coll){

    Iterator<AnyType> itr=coll.iterator();

    while(itr.hasNext()){

    AnyType item=itr.next();

    System.out.println(item);

    }

    }

     

     

    注意:【1】如果对正在被迭代的集合进行结构上的改变(add,removeclear方法),那么迭代器就不合法(并且其后使用迭代器是会抛出ConcurrentModificationException异常)。

         【2Collection有一个remove方法,使用Iteratorremove方法可能会有更多的优点。且迭代过程中可以使用iteratorremove方法。

    3、List接口、ArrayList类和LinkedList

       1List接口,继承与Collection接口。ArrayList类提供了List ADT的一种可增长数组的实现,getset方法调用花费常数时间。LinkedList类提供了List ADT的双链表实现,addFirstremoveFirstaddLastremoveLast,以及getFirstgetLast的调用开销为常数时间。

    2remove方法对LinkedList类的使用。

    例:将一个表中所有具有偶数值得项删除;

    这里要用到迭代器,不能用get方法,因为LinkedListget方法的时间复杂度是O(n)

    表三

    public static void removeEvensVer(List<Integer> lst){

    Iterator<Integer> itr=lst.iterator();

     

    while(itr.hasNext()){

    if(itr.next()%2==0)

    itr.remove();

    }

    }

     

         3) ListIterator接口,ListIterator扩展了ListIterator的功能。增加了一些方法。

    表四

    public interface ListIterator<AnyType> extends Iterator<AnyType>{

    boolean hasPrevious();

    AnyType previous();

     

    void add(AnyType x);

    void set(AnyType newVal);

    }

     

     

     

  • 相关阅读:
    什么是CMS
    TP3.2项目—微信推广页
    《实用技巧》——让你的网站变成响应式的3个简单步骤
    thinkphp分页带数据
    tp框架表单验证 及ajax
    tp框架做留言板
    随时修改添加,thinkphp小知识
    thinkphp使用ajax
    thinkphp修改和删除数据
    tp框架查询
  • 原文地址:https://www.cnblogs.com/redlight/p/2853967.html
Copyright © 2020-2023  润新知