首页 > 其他分享 >LinkedList

LinkedList

时间:2024-09-07 21:36:36浏览次数:1  
标签:LinkedList linkedList System Student new out

public static void main(String[] args) {
//实例化对象
LinkedList linkedList=new LinkedList();
//1添加元素
Student student=new Student("刘德华",20);
Student student1=new Student("郭富城",30);
Student student2=new Student("张三",33);
linkedList.add(student);
linkedList.add(student1);
linkedList.add(student2);

    System.out.println("元素个数:"+linkedList.size());
    System.out.println(linkedList.toString());

    //2删除元素

    linkedList.remove(new Student("张三",33));

    System.out.println("删除之后的元素个数:"+linkedList.size());
    //linkedList.clear();//清空

    //遍历
    //3.1for遍历
    System.out.println("--------------for遍历-----------");
    for (int i = 0; i <linkedList.size() ; i++) {
        System.out.println(linkedList.get(i));
    }

    //3.2增强for
    System.out.println("--------------增强for遍历-----------");
    for (Object object:linkedList) {
      Student s=(Student) object;
        System.out.println(s.toString());
    }
    //3.3使用迭代器
    System.out.println("--------------迭代器遍历-----------");
   Iterator it =linkedList.iterator();
   while (it.hasNext()){
       Student s=(Student) it.next();
       System.out.println(s.toString());
   }
   //3.4使用列表迭代器
    System.out.println("--------------列表迭代器遍历-----------");
    ListIterator lit=linkedList.listIterator();
    while (lit.hasNext()){
        Student s=(Student) lit.next();
        System.out.println(s.toString());
    }
    //4判断
    System.out.println(linkedList.contains("郭富城"));
    System.out.println(linkedList.isEmpty());
    //获取
    System.out.println(linkedList.indexOf(student1));




    }

标签:LinkedList,linkedList,System,Student,new,out
From: https://www.cnblogs.com/anonymity12/p/18402188

相关文章

  • ArrayList和LinkedList的区别
    >List子体系特点:A:有序的(存储和读取的顺序是一致的)B:有整数索引C:允许重复的 <!--more-->**List的特有功能**````voidadd(intindex,Eelement):将元素添加到index索引位置上Eget(intindex):根据index索引获取元素Eremove(intindex):根据index索引删除元素Es......
  • Linkedlist源码详解
    介绍LinkedList同时实现了List接口和Deque接口,也就是说它既可以看作一个顺序容器,又可以看作一个队列(Queue),同时又可以看作一个栈(Stack)。这样看来,LinkedList简直就是个全能冠军。当你需要使用栈或者队列时,可以考虑使用LinkedList,一方面是因为Java官方已经声明不建议使用Stack类,......
  • Linkedlist源码详解
    介绍LinkedList同时实现了List接口和Deque接口,也就是说它既可以看作一个顺序容器,又可以看作一个队列(Queue),同时又可以看作一个栈(Stack)。这样看来,LinkedList简直就是个全能冠军。当你需要使用栈或者队列时,可以考虑使用LinkedList,一方面是因为Java官方已经声明不建议使用Stack类......
  • linkedlist
    data=newLinkedList[BASE];这行代码的意思是初始化一个LinkedList对象的数组。具体解释如下:数组声明:LinkedList[]表示data是一个数组,这个数组将存储LinkedList类型的对象。在这个上下文中,它将存储LinkedList<Integer>对象,用于存储整数。大小指定:newLinkedList......
  • 8-LinkedList
    LinkedList实现类常用方法及使用/*LinkedList常用方法增加:addFirst(Ee)addLast(Ee)offer(Ee)offerFirst(Ee)offerLast(Ee)删除:poll()pollFirst()pollLast()removeFirst()remove......
  • 集合及数据结构第七节————LinkedList的模拟实现与使用
    系列文章目录集合及数据结构第七节————LinkedList的模拟实现与使用LinkedList的模拟实现与使用无头双向链表实现什么是LinkedListLinkedList的使用LinkedList的遍历ArrayList和LinkedList的区别文章目录系列文章目录集合及数据结构第七节————LinkedList的模......
  • ArrayList 和 LinkedList 的区别是什么
    数据结构实现:ArrayList是动态数组的数据结构实现,而LinkedList是双向链表的数据结构实现。随机访问效率:ArrayList比LinkedList在随机访问的时候效率要高,因为LinkedList是线性的数据存储方式,所以需要移动指针从前往后依次查找。增加和删除效率:在非首尾的增加和删除操......
  • LinkedList模拟栈数据结构的集合,并测试
    packagecom.shujia.day13;importjava.util.LinkedList;/*LinkedList请用LinkedList模拟栈数据结构的集合,并测试栈:先进后出题目的要求是:自己创建一个类,将LinkedList作为成员变量,将来创建自己的类对象,使用自己的方法,但是底层用的是LinkedList中的方法......
  • LinkedList
    packagecom.shujia.day13;importjava.util.Iterator;importjava.util.LinkedList;/*Collection:-List(有序【指的是存储和取出的顺序是一致的】且可以发生重复,且有索引的概念)-ArrayList:底层数据结构是数组,查询快,增删慢,线程不安全的,效率高......
  • 【数据结构】LinkedList与链表
    目录链表1、链表的概念及结构 2、LinkedList的使用2、1什么是LinkedList2、2LinkedList的使用3、LinkedList的遍历4、LinkedList的模拟实现 5、ArrayList和LinkedList的区别上篇已经熟悉了ArrayList的使用,ArrayList底层使用数组来存储元素。由于其底层是一段连续......