当前位置: 移动技术网 > IT编程>开发语言>Java > Java自学-集合框架 LinkedList

Java自学-集合框架 LinkedList

2019年11月26日  | 移动技术网IT编程  | 我要评论

java集合框架 linkedlist

序列分先进先出fifo,先进后出filo
fifo在java中又叫queue 队列
filo在java中又叫stack 栈

示例 1 : linkedlist 与 list接口

arraylist一样,linkedlist也实现了list接口,诸如add,remove,contains等等方法。

示例 2 : 双向链表 - deque

除了实现了list接口外,linkedlist还实现了双向链表结构deque,可以很方便的在头尾插入删除数据

什么是链表结构: 与数组结构相比较,数组结构,就好像是电影院,每个位置都有标示,每个位置之间的间隔都是一样的。 而链表就相当于佛珠,每个珠子,只连接前一个和后一个,不用关心除此之外的其他佛珠在哪里。
在这里插入图片描述

package collection;
 
import java.util.linkedlist;
 
import charactor.hero;
 
public class testcollection {
 
    public static void main(string[] args) {
         
        //linkedlist是一个双向链表结构的list
        linkedlist<hero> ll =new linkedlist<hero>();
         
        //所以可以很方便的在头部和尾部插入数据
        //在最后插入新的英雄
        ll.addlast(new hero("hero1"));
        ll.addlast(new hero("hero2"));
        ll.addlast(new hero("hero3"));
        system.out.println(ll);
         
        //在最前面插入新的英雄
        ll.addfirst(new hero("herox"));
        system.out.println(ll);
         
        //查看最前面的英雄
        system.out.println(ll.getfirst());
        //查看最后面的英雄
        system.out.println(ll.getlast());
         
        //查看不会导致英雄被删除
        system.out.println(ll);
        //取出最前面的英雄
        system.out.println(ll.removefirst());
         
        //取出最后面的英雄
        system.out.println(ll.removelast());
         
        //取出会导致英雄被删除
        system.out.println(ll);
         
    }
      
}

示例 3 : 队列 - queue

linkedlist 除了实现了list和deque外,还实现了queue接口(队列)。
queue是先进先出队列 fifo,常用方法:
offer 在最后添加元素
poll 取出第一个元素
peek 查看第一个元素
队列 - queue

package collection;
  
import java.util.linkedlist;
import java.util.list;
import java.util.queue;
  
import charactor.hero;
  
public class testcollection {
  
    public static void main(string[] args) {
        //和arraylist一样,linkedlist也实现了list接口
        list ll =new linkedlist<hero>();
          
        //所不同的是linkedlist还实现了deque,进而又实现了queue这个接口
        //queue代表fifo 先进先出的队列
        queue<hero> q= new linkedlist<hero>();
          
        //加在队列的最后面
        system.out.print("初始化队列:\t");
        q.offer(new hero("hero1"));
        q.offer(new hero("hero2"));
        q.offer(new hero("hero3"));
        q.offer(new hero("hero4"));
          
        system.out.println(q);
        system.out.print("把第一个元素取poll()出来:\t");
        //取出第一个hero,fifo 先进先出
        hero h = q.poll();
        system.out.println(h);
        system.out.print("取出第一个元素之后的队列:\t");
        system.out.println(q);
          
        //把第一个拿出来看一看,但是不取出来
        h=q.peek();
        system.out.print("查看peek()第一个元素:\t");
        system.out.println(h);
        system.out.print("查看并不会导致第一个元素被取出来:\t");
        system.out.println(q);
          
    }
       
}

练习使用linkedlist实现stack栈

与fifo(先入先出的)队列类似的一种数据结构是filo先入后出栈stack
根据接口stack :
实现类:mystack

public class mystack implements stack

并向这个栈中,压入5个英雄,接着弹出5个英雄

再解释一下栈: 栈的结构,就像给弹夹添加子弹一样,先添加的子弹,就放在了最下面,打手枪的时候,只能从最上面取子弹。

package collection;
 
import charactor.hero;
 
public interface stack {
 
    //把英雄推入到最后位置
    public void push(hero h);
    //把最后一个英雄取出来
    public hero pull();
    //查看最后一个英雄
    public hero peek();
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网