简单实现ArrayList以及LinkedList

1.手工编写一个arrayList

public Class MyArrayList{
     //初始化容量
     private int initCapacity = 8; 
     private int oldCapacity = initCapacity;
     // 数组元素的个数  
     private int size=0;
     private Object[] list;
     public MyArrayList(){
           list = new Object[initCapacity];
    }
     public int size(){
           return size;
      }
     //扩容方法
     public int ensureCapacity(){
             oldCapacity = (size*3)/2+1;
             Object[] temp = new Object[oldCapacity]();
             for(int i=0;i<list.length;i++){
                    temp[i] = list[i];
              }
              list = temp;
          }
      //添加元素
      public void add(Object o){
              if(size>=oldCapacity){
                   ensureCapacity();
              }
              list[size]=o;
               size++;
      }
      //修改元素
      public void set(int index,int value){  
            if(index<0||index>=size){
                 throw new Exception("超出范围");
           }
           list[index] = value;
      }
      //获取元素
      public Object get(int index){
         if(index<0||index>=size){
                 throw new Exception("超出范围");
         }
         return list[index];
      }
   //清空整个集合
  public void clear(){
          size=0;
          list = new Object[initCapacity];
    }
 //删除指定位置的元素
   public void removeAt(int index){
          
        if(index<0||index>=size){
                 throw new Exception("超出范围");
         }
          for(int i = index-1;i<size-1;i++){
                list[i+1]=list[i];
           }

          size--;

     }
}

2.手工编写一个linkedList

public class MyLinkedList {
	private Node head;
	private int size = 0;

	public int size() {
		return size;
	}

	public void add(Object value) {
		Node tempNode = new Node();
		tempNode.setData(value);
		if (head == null) {
			head = tempNode;
		} else {
			Node currentNode = head;
			while (currentNode.getNext() != null) {
				currentNode = currentNode.getNext();
			}
			currentNode.setNext(tempNode);
		}
		size++;
	}

	public void set(int index, Object value) {
		Node currentNode = head;
		for (int i = 0; i < index; i++) {
			currentNode = currentNode.getNext();
		}
		currentNode.setData(value);
	}

	public Object get(int index) {
		Node currentNode = head;
		for (int i = 0; i < index; i++) {
			currentNode = currentNode.getNext();
		}
		return currentNode.getData();
	}

	public void clear() {
		size = 0;
		head = null;
	}

	public void removeAt(int index) {
		if (index == 0) {
			head = head.getNext();
		} else {
			Node currentNode = head;
			for (int i = 0; i < index - 1; i++) {
				currentNode = currentNode.getNext();
			}
			currentNode.setNext(currentNode.getNext().getNext());
		}
		size--;
	}

	private class Node {
		private Object data;
		private Node next;

		public Object getData() {
			return data;
		}

		public void setData(Object data) {
			this.data = data;
		}

		public Node getNext() {
			return next;
		}

		public void setNext(Node next) {
			this.next = next;
		}

	}
	public static void main(String[] args) {
		MyLinkedList list = new MyLinkedList();
		list.add(1);
		list.add(2);
		list.add(3);
		list.set(1, 4);
		for(int i=0;i<list.size();i++){
			
			System.out.println(list.get(i));
		}
		list.removeAt(1);

	
	}

}

总结:上面只是简单的使用数据结构的知识实现了java里面较为常用的list集合,很多细节并没有具体的去实现,另外LinkedList是由双向循环链表实现的,在这里只是简单的使用单链表实现的。透过结构看本质大概可以清楚的看出ArrayList和LinkedList的几点主要区别:

1.arrayList底层是由数组来实现的,而LinkedList是由双向链表来实现的,并且都是有序集合

2.在性能方面,arraylist遍历性能更高,而linkedlist由于每次遍历都要移动指针(注意:java里面并没有指针这个概念,这里的指针指的是对对象的引用)性能较低,在获取、修改等操作的性能LinkedList比arrayList更加的高效

猜你喜欢

转载自blog.csdn.net/ljj1338/article/details/80368842
今日推荐