本文主要是介绍容器第四课,JDK源代码分析,自己实现LinkedList,双向链表的概念_节点定义,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package com.pkushutong.Collection;public class Test03 {private Test03_01 first; //第一个节点private Test03_01 last; //最后一个节点private int size;public void add(Object obj){Test03_01 t = new Test03_01();if(first == null){t.setPrevious(null);t.setObj(obj);t.setNext(null);first = t;last = t;}else{//直接往last借点后增加新的节点t.setPrevious(last);t.setObj(obj);t.setNext(null);last.setNext(t);last = t;}size++;}public int size(){return size;}public Object get(int index){Test03_01 temp = node(index);return temp.obj;}public void remove(int index){Test03_01 temp = node(index);if(temp != null){Test03_01 up = temp.previous;Test03_01 down = temp.next;up.next = down;down.previous = up;size--;}}public void add(int index,Object obj){Test03_01 temp = node(index);Test03_01 newTest03_01 = new Test03_01();newTest03_01.obj = obj;if(temp != null){Test03_01 up = temp.previous;up.next = newTest03_01;newTest03_01.previous = up;newTest03_01.next = temp;temp.previous = newTest03_01;size++;}}private Test03_01 node(int index) {Test03_01 temp = null;if(first != null){temp = first;for(int i=0; i<index; i++){temp = temp.next;}}return temp;}public static void main(String[] args) {Test03 list = new Test03();list.add("123");list.add("234");list.add("345");//list.remove(1);list.add(1, "aaaa");System.out.println(list.get(1));}
}
package com.pkushutong.Collection;/*** 用来表示一个节点* @author dell**/
class Test03_01{Test03_01 previous;Object obj;Test03_01 next;public Test03_01() {}public Test03_01(Test03_01 previous, Object obj, Test03_01 next) {super();this.previous = previous;this.obj = obj;this.next = next;}public Test03_01 getPrevious() {return previous;}public void setPrevious(Test03_01 previous) {this.previous = previous;}public Object getObj() {return obj;}public void setObj(Object obj) {this.obj = obj;}public Test03_01 getNext() {return next;}public void setNext(Test03_01 next) {this.next = next;}}
这篇关于容器第四课,JDK源代码分析,自己实现LinkedList,双向链表的概念_节点定义的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!