链表底层实现(LinkList)
ArrayList
LinkList
链表底层实现
Node.java (节点类)
package cn.Collection; //用来表示一个节点 public class Node{ Node previous; //上一个节点 Object obj; Node next; //下一个节点 public Node() { } public Node(Node previous, Object obj, Node next) { super(); this.previous = previous; this.obj = obj; this.next = next; } public Node getPrevious() { return previous; } public void setPrevious(Node previous) { this.previous = previous; } public Object getObj() { return obj; } public void setObj(Object obj) { this.obj = obj; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } }
Test01.java
package cn.Collection; public class Test01 { private Node first; private Node last; private int size; public void add(Object obj) { Node n=new Node(); if(first==null) { n.setPrevious(null); n.setObj(obj); n.setNext(null); first=n; last=n; } else { //直接往last节点后增加新节点 n.setPrevious(last); n.setObj(obj); n.setNext(null); last.setNext(n); last=n; } size++; } public int size() { return size; } public Object get(int index) { //2 //0,1,2,3,4 Node temp=null; if(first!=null) { temp=first; for(int i=0;i<index;i++) { temp=temp.next; } } return temp.obj; } public static void main(String[] args) { Test01 list=new Test01(); list.add("aaa"); list.add("dd"); list.add("d"); System.out.println(list.size()); System.out.println(list.get(1)); } }