首页 > 其他分享 >Day06集合-LinkedList

Day06集合-LinkedList

时间:2022-08-26 19:36:20浏览次数:71  
标签:Node LinkedList list Day06 println student 集合 new

LinkedList

1.特点:

  • 链表结构实现,查询慢,增删快

2.LinkedList使用

/*
LinkedList的使用
 */
public class linkedListDemo01 {
    public static void main(String[] args) {
        //创建ArrayList对象
        LinkedList list = new LinkedList();
        student s1 = new student("张三", 19);
        student s2 = new student("李四", 19);
        student s3 = new student("王五", 19);
        student s4 = new student("赵六", 22);
        //1.添加
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        System.out.println(list);

        /*
        2.删除
        remove方法底层调用了Object的equals方法,比较的是对象的引用
        arrayList.remove(new student("张三",19));无法删除new出来的新对象
        若想删除需要重写equals方法
         */
        list.remove(s1);
        list.remove(new student("张三", 19));//成功删除
        System.out.println("-------2.删除---------");
        System.out.println(list);

        //3.遍历
        //3.1使用迭代器
        Iterator it = list.iterator();
        System.out.println("-------3.1使用迭代器---------");
        while (it.hasNext()) {
            student s = (student) it.next();
            System.out.println(s);
        }
        //3.2使用列表迭代器
        ListIterator lit = list.listIterator();
        System.out.println("-------3.2使用列表迭代器--------");
        while (lit.hasNext()) {
            student s = (student) lit.next();
            System.out.println(s);
        }
        //3.3for循环遍历
        System.out.println("-------3.3for循环遍历--------");
        for (int i = 0; i <list.size() ; i++) {
            System.out.println(list.get(i));
        }

        //4.判断
        System.out.println("---------4.判断--------");
        System.out.println(list.contains(new student("赵六", 22)));

        //5.查找
        System.out.println("---------5.查找--------");
        System.out.println(list.indexOf(s2));
    }
}

//输出结果:
[student{name='张三', age=19}, student{name='李四', age=19}, student{name='王五', age=19}, student{name='赵六', age=22}]
-------2.删除---------
[student{name='李四', age=19}, student{name='王五', age=19}, student{name='赵六', age=22}]
-------3.1使用迭代器---------
student{name='李四', age=19}
student{name='王五', age=19}
student{name='赵六', age=22}
-------3.2使用列表迭代器--------
student{name='李四', age=19}
student{name='王五', age=19}
student{name='赵六', age=22}
-------3.3for循环遍历--------
student{name='李四', age=19}
student{name='王五', age=19}
student{name='赵六', age=22}
---------4.判断--------
true
---------5.查找--------
0

3.LinkedList源码分析:

  • add():(尾插法)
public boolean add(E e) {
    linkLast(e);
    return true;
}

void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
}
    
//核心方法Node<E>
private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

4.LinkedList和ArrayList的区别

标签:Node,LinkedList,list,Day06,println,student,集合,new
From: https://www.cnblogs.com/workplace-blog/p/16628951.html

相关文章

  • Day06集合-Set
    Set接口1.概述无序,五下标,元素不可重复2.Set接口使用/***set接口使用(同collection)*/publicclasssetDemo{publicstaticvoidmain(String[]args){......
  • Day06-泛型
    泛型1.泛型介绍泛型是JDK1.5引入的新特性,本质是参数化类型,把类型作为参数传递常见形式有:泛型类,泛型接口,泛型方法语法:<T,....>:T称为类型占位符,表示一种引用数据......
  • Day07集合-HashMap
    HashMap特点:jdk1.2,运行效率快,线程不安全,允许null作为key或者value使用:存储结构:哈希表(数组+链表+红黑树)使用key的hashcode和equals判重//创建HashMap<student,St......
  • Day07集合-Map父接口
    MapMap父接口存储一对数据(key—Value),无序,无下标,建不可以重复,值可以重复。方法方法摘要voidclear()从此映射中移除所有映射关系(可选操作)。bo......
  • Day05集合-Collection
    集合1.集合的概念对象的容器,实现了对对象的常用操作,类似数组功能位置:java.util.*2.集合和数组的区别集合长度不固定,数组长度固定集合只能存储引用数据类型,数组可......
  • C#集合:ICollection和IList接口
    虽然枚举接口IEnumerable提供了一种向前迭代集合的协议,但是它们并没有提供确定集合大小、根据索引访问成员、搜索以及修改集合的机制。为了实现这些功能,.NETCore定义了ICo......
  • 集合集成关系
    collection集合继承实现关系(单列集合)Collection有个重要子接口List和Set,他们的实现子类都是单列集合===========================================================......
  • 列表、元素、字典和集合
    Pythonlist`list`可存储任意类型,支持`list()`。list方法增删查appendpopindexextendremovecountinsertclear子串是Python序列操作的一......
  • 集合.Collection体系集合
    Collection父接口特点:代表一组任意类型的对象,无序,无下标,不能重复方法:booleanadd(Objectobj);添加一个对象booleanaddAll(Collectionc);将一个集合中的所有对象......
  • ArrayList集合存储基本数据类型
    ArrayList集合存储基本数据类型 如果希望向集合ArrayList当中存储基本类型数据,必须使用基本类型对应的“包装类”基本类型包装类byteByteshor......