• List容器-LinkedList链表


    LinkedList--链表

    特点:

      删除,增加 用LinkedList性能高  层次查找不适合   

        查询用ArrayList  数组下标查找  插入和删除慢缺点是要做移位操作

      总结:LinkedList内部封装的是双向链表数据结构,每个节点是一个Node对象,Node对象封装的是你要添加的元素,

    还有一个指向上一个Node对象的应用和指向下一个Node对象的引用,

    不同的容器有不同的数据结构,不同的数据结构操作起来性能不一样

      扩展了AbstractSequentialList并实现List接口

          提供链表数据结构

    主要实现了Deque接口  Deque接口继承了Queue接口  一个线性collection,支持在两端插入和移除元素   队列(堆栈)操作
    

      

    //不能使用List 因为addFirst addLast  在List接口中没有
    LinkedList<String> lists = new LinkedList<String>();
    
    //不能使用List 因为addFirst addLast  在List接口中没有
    		LinkedList<String> lists = new LinkedList<String>();
    		lists.add("张三");
    		lists.add("李四");
    		lists.add("王五");
    		lists.addFirst("tanlei");// 头添加
    		lists.addLast("marry");// 尾添加
    		/*Iterator<String> ss = lists.iterator();
    		// 使用迭代器进行统一遍历
    		while (ss.hasNext()) {
    			String name = ss.next();
    			System.out.println(name);
    		}*/
    		/*for(Iterator<String> ss = lists.iterator();ss.hasNext();) {
    			String name = ss.next();
    			System.out.println(name);
    		}*/
    		// 增强for循环遍历
    		for (String name : lists) {
    			System.out.println(name);
    		}
    		lists.clear();
    		System.out.println(lists.removeFirst());//删除并返回第一个元素,容器中没有元素返回异常
    		System.out.println(lists.size());
    		System.out.println(lists.pollFirst());//容器中没有元素返回null
    

    栈和队列的功能  

    栈:后进先出(杯子)

    队列:先进先出

    package com.day1;
    
    import java.util.Iterator;
    import java.util.LinkedList;
    
    public class LinkedListDemo2 {
        public static void main(String[] args) {
            /*MyStack<String> myStack = new MyStack<String>();
            myStack.push("张三");
            myStack.push("李四");
            myStack.push("王五");
            myStack.push("周六");
            myStack.pop();// 出栈一个
            myStack.pop();// 出栈两个
            Iterator<String> it = myStack.iterator();
            while (it.hasNext()) {
                System.out.println(it.next());
            }*/
            
            MyQueue<Integer> myQueue=new MyQueue<Integer>();
            myQueue.push(1);
            myQueue.push(2);
            myQueue.push(3);
            myQueue.push(4);
            myQueue.pop();
            myQueue.pop();
            Iterator<Integer> it = myQueue.iterator();
            while (it.hasNext()) {
                System.out.println(it.next());
            }
        } 
    }
    
    class MyStack<T> {
        private LinkedList<T> data = null;
    
        public MyStack() {
            data = new LinkedList<T>();
        }
    
        // 压栈方法
        public void push(T obj) {
            data.addFirst(obj);
        }
    
        // 出栈
        public T pop() {
            return data.removeFirst();
        }
    
        public Iterator<T> iterator() {
    
            return data.iterator();
    
        }
    
    }
    
    class MyQueue<T> {
        private LinkedList<T> data = null;
    
        public MyQueue() {
            data = new LinkedList<T>();
        }
    
        public void push(T obj) {
            data.addFirst(obj);
        }
    
        public T pop() {
            return data.removeLast();
        }
    
        public Iterator<T> iterator() {
    
            return data.iterator();
    
        }
    
    }
  • 相关阅读:
    聊聊赚钱
    Java面试官最爱问的volatile关键字
    你适合副业挣钱吗?
    SpringBoot自定义starter及自动配置
    mybatis进阶--输入映射和输出映射
    mybatis入门--初识mybatis
    mybatis入门--#{}和${}的区别
    mybatis入门--mybatis和hibernate比较
    mybatis入门--单表的增删改操作
    mybatis进阶--mapper输入映射和输出映射
  • 原文地址:https://www.cnblogs.com/tanlei-sxs/p/9976946.html
Copyright © 2020-2023  润新知