日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ArrayList 和 LinkedList 的自定义实现

發(fā)布時間:2025/6/17 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ArrayList 和 LinkedList 的自定义实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 前言
  • 一、ArrayList
  • 二、LinkedList
  • 總結(jié)


前言

??????下面給出 ArrayList 和 LinkedList 的自定義實現(xiàn),僅作為練習(xí)。

一、ArrayList

??????自己仿照著寫一個 SimpleArrayList 實現(xiàn)類,代碼也比較簡單,故不做說明了,其中 SimpleItr 內(nèi)部類實現(xiàn)了 Iterator 接口。如下:

public class SimpleArrayList<T> implements Iterable<T> {private static final int DEFAULT_CAPACITY=10;private int capacity=DEFAULT_CAPACITY;private int Size;private T[] Items;public SimpleArrayList(){doClear();}public void clear(){doClear();}private void doClear(){Size=0;ensureCapacity(DEFAULT_CAPACITY);}public int size(){return Size;}public boolean isEmpty(){return size()==0;}public void trimToSize(){ensureCapacity(size());}public T get(int idx){if(idx<0||idx>=size())throw new ArrayIndexOutOfBoundsException();return Items[idx];}public void set(int idx,T newValue){if(idx<0||idx>=size())throw new ArrayIndexOutOfBoundsException();Items[idx]=newValue;}public void ensureCapacity(int newCapacity){if(newCapacity<Size)return;T[] old=Items;Items=(T[])new Object[newCapacity];for(int i=0;i<size();i++)Items[i]=old[i];}public void add(T x){add(size(),x);}public void add(int idx,T x){if(Items.length==size())//Items.length為數(shù)組容量ensureCapacity(size()*2+1);// +1 是為了避免數(shù)組為空時無法添加元素for(int i=size()-1;i>idx;i--)Items[i]=Items[i-1];Items[idx]=x;Size+=1;}public void remove(int idx){if(idx<0||idx>=size())throw new ArrayIndexOutOfBoundsException();for(int i=idx;i<size()-1;i++)Items[i]=Items[i+1];Size-=1;}public Iterator<T> iterator(){return new SimpleItr();}private class SimpleItr implements Iterator<T>{//內(nèi)部類private int current=0;//當(dāng)前位置(從0開始)public boolean hasNext(){return current<size();}public T next(){if(!hasNext())throw new NoSuchElementException();return Items[current++];}public void remove(){SimpleArrayList.this.remove(--current);}}}

二、LinkedList

??????LinkedList 內(nèi)部采用雙鏈表來實現(xiàn),如果操作發(fā)生在已知的情況下(端點或由迭代器指定的位置上),從而保證每個操作花費常數(shù)時間。

??????1,SimpleLinkedList 類,包含雙鏈表和一些方法。
??????2,Node 靜態(tài)內(nèi)部類。實現(xiàn)鏈表中的節(jié)點。
??????3,SimpleItr 內(nèi)部類。實現(xiàn)了 Iterator 接口。

public class SimpleLinkedList<T> implements Iterable<T> {private static class Node<T>{//Node 靜態(tài)內(nèi)部類(改為非靜態(tài)內(nèi)部類也可以)public T value;public Node<T> pre;public Node<T> next;public Node(T value,Node<T> pre,Node<T> next){this.value=value;this.pre=pre;this.next=next;}}private int size;private Node<T> header,tail;//頭尾節(jié)點public SimpleLinkedList(){doClear();}public void clear(){doClear();}private void doClear(){header=new Node<T>(null,null,null);tail=new Node<T>(null,header,null);header.next=tail;size=0;}public int size(){return size;}public boolean isEmpty(){return size()==0;}//get 和 set 方法。private Node<T> getNode(int idx){ //工具方法,稍微加快了搜索效率。Node<T> tmp;if(idx<0||idx>=size()){throw new IndexOutOfBoundsException();}if(idx<size()/2){tmp=header.next;for(int i=0;i<idx;i++)tmp=tmp.next;}else{tmp=tail.pre;for(int i=size()-1;i>idx;i--)tmp=tmp.pre;}return tmp;}public T get(int idx){return getNode(idx).value;}public void set(int idx,T ele){Node<T> tmp=getNode(idx);tmp.value=ele;}//add 和 remove 方法。public void add(T x){//尾插法Node<T> newPre=tail.pre;Node<T> tmp=new Node<>(x,newPre,tail);tail.pre=tmp;newPre.next=tmp;size++;}public void add(int idx,T x){Node<T> oldNode=getNode(idx);Node<T> newPre=oldNode.pre;Node<T> tmp=new Node<>(x,newPre,oldNode);newPre.next=tmp;oldNode.pre=tmp;size++;}public void remove(int idx){Node<T> node=getNode(idx);Node<T> pre=node.pre;Node<T> next=node.next;pre.next=next;next.pre=pre;size--;}//iterator 方法。public Iterator<T> iterator(){return new SimpleItr();}private class SimpleItr implements Iterator<T>{private Node<T> current=header.next;private boolean okToRemove=false;public boolean hasNext(){return current!=tail;}public T next(){if(!hasNext())throw new NoSuchElementException();T nextItem=current.value;current=current.next;okToRemove=true;return nextItem;}public void remove(){if(!okToRemove)throw new IllegalStateException();Node<T> currentTmp=current.pre;currentTmp.pre.next=current;current.pre=currentTmp.pre;okToRemove=false;}} }

總結(jié)

??????完。

總結(jié)

以上是生活随笔為你收集整理的ArrayList 和 LinkedList 的自定义实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。