手写ArrayList,LinkedList,
手写ArrayList,LinkedList,
虽然学习Java很长时间了,但是对数据结构一直很痛恨,更何况手写Collections之类的了,这次实在是没办法,所以看看书,上网看看别人怎么写的,扒了过来了,记个笔记方便以后复习,\(^o^)/~
ArrayList是比较好写,比较容易看懂的......
package com.juan;
public class MyArrayList<AnyType>{
private static final int DEFAULT_CAPACITY = 10;
private int theSize;
private AnyType[] theItems;
/**
* 返回ArrayList的长度
* @return
*/
public int size(){
return theSize;
}
public boolean isEmpty(){
return size() == 0;
}
public void ensureCapacity(int newCapacity){
if(newCapacity < theSize){
return;
}
AnyType[] old = theItems;
theItems = (AnyType[]) new Object[newCapacity];
for(int i = 0; i < size(); i++){
theItems[i] = old[i];
}
}
public MyArrayList(){
clear();
}
public void clear(){
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
public void trimToSize(){
ensureCapacity(size());
}
public AnyType get(int idx){
if(idx < 0 || idx >= size()){
return null;
}else{
return theItems[idx];
}
}
public AnyType set(int idx, AnyType newVal){
if(idx < 0 || idx >= size()){
return null;
}else{
AnyType old = theItems[idx];
theItems[idx] = newVal;
return old;
}
}
public boolean add(AnyType x){
add(size(), x);
return true;
}
public void add(int idx, AnyType x){
if(theItems.length == size()){
ensureCapacity(size()*2+1);
}
for(int i = theSize; i > idx; i++){
theItems[i] = theItems[i - 1];
}
theItems[idx] = x;
theSize++;
}
public AnyType remove(int idx){
AnyType removedItem = theItems[idx];
for(int i = idx; i < size()-1; i++){ //为什么不是size()
theItems[i] = theItems[i + 1];
}
theSize--;
return removedItem;
}
}
-----------------------------------------------------------------------------------------------------------------------
LinkedList折磨我很久,光看代码没办法消化,我就在纸上画了画,勉强过关了
package com.juan;
public class MyLinkedList<AnyType> {
private static class Node<AnyType>{
public AnyType data;
public Node<AnyType> prev;
public Node<AnyType> next;
public Node(AnyType d, Node<AnyType> p, Node<AnyType> n){
data = d;
prev = p;
next = n;
}
}
private int theSize;
private int modCount;
private Node<AnyType> beginMarker;
private Node<AnyType> endMarker;
public MyLinkedList(){
clear();
}
public void clear(){
beginMarker = new Node<AnyType>(null, null, null);
endMarker = new Node<AnyType>(null, beginMarker, null);
beginMarker.next = endMarker;
theSize = 0;
}
public int size(){
return theSize;
}
public boolean add(AnyType x){
add(size(), x);
return true;
}
public void add(int idx, AnyType x){
addBefore(getNode(idx), x);
}
public AnyType get(int idx){
return getNode(idx).data;
}
private void addBefore(Node<AnyType> p, AnyType x){
Node<AnyType> newNode = new Node<AnyType>(x, p.prev, p);
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;
modCount++;
}
private Node<AnyType> getNode(int idx){
Node<AnyType> p;
if(idx < 0 || idx > size()){
System.out.println("IndexOutOfBoundsException");
}
if(idx <= size()/2){
System.out.println(idx);
p = beginMarker.next;
for(int i = 0; i < idx; i++){
p = p.next;
}
}else{
p = endMarker;
for(int i = size(); i > idx; i--){
p = p.prev;
}
}
return p;
}
public boolean find(AnyType x){
Node<AnyType> p = beginMarker.next;
for(int i = 0; i < size(); i++){
if(p.data == x){
return true;
}
p = p.next;
}
return false;
}
/**这个方法还有点问题,打印前面多了个null,昨天实在太困了,还没来得及完善,待续......**/
public String toString(){
String s = null;
Node<AnyType> p = beginMarker.next;
for(int i = 0; i < size(); i++){
s += p.data +",";
p = p.next;
}
return s;
}
}
相关文章
- 暂无相关文章
用户点评