一、自己動手實現一個------------“動態數組”

參考文章:

https://blog.csdn.net/u012152619/article/details/41774325     數據結構之數組

https://blog.csdn.net/u012736685/article/details/83029006  二、數組

https://blog.csdn.net/u012152619/article/details/42059675  數據結構與算法概念解析

https://github.com/liuyubobobo/Play-with-Algorithms  

https://www.jianshu.com/p/3a49875ce580        Java實現動態數組


        上面介紹的是一些基本知識,我下面就直接上代碼了,如果要了解基本知識請看上面《數據結構之數組》連接,或者自己搜索其他博客文章。

一下內容爲代碼部分,參考了很多的 波波老師的代碼,我是代碼搬運工O(∩_∩)O!

 

動態數組代碼部分:


package com.dynamic.array;

public class MyArray<E> {
	private E[] data;
	private int size;
	
	// 構造函數,傳入數組的容量capacity構造Array
	public MyArray(int capacity){
		data = (E[])new Object[capacity];
		size =0;
	}
	
	 // 無參數的構造函數,默認數組的容量capacity=10
	public MyArray(){
		this(10);
	}
	
	// 獲取數組的容量
	public int getCapacity(){
		return data.length;
	}
	
	// 獲取數組中的元素個數
	public int getSize(){
		return size;
	}
	
	// 返回數組是否爲空
	public boolean isEmpty(){
		return size == 0;
	}
	
	// 在index索引的位置插入一個新元素e
	public void add(int index, E e){
		if(index < 0 || index > size){
			throw new IllegalArgumentException("Add failed. Require index >0 and index < size.");
		}
		
		if(size == data.length){
			resize(2*data.length);
		}
		
		for(int i=size-1; i>=index;i--){
			data[i+1] = data[i];
		}
		data[index] = e;
		
		size++;
	}
	
	// 向所有元素後添加一個新元素
	public void addLast(E e){
		add(size, e);
	}
	
	// 在所有元素前添加一個新元素
	public void addFirst(E e){
		add(0, e);
	}
	
	// 獲取index索引位置的元素
	public E get(int index){
		if(index < 0 || index > size)
			throw new IllegalArgumentException("Get failed. Required index >0 and index < size.");
		return data[index];
	}
	
	// 修改index索引位置的元素爲e
	public void set(int index, E e){
		if(index <0 || index > size)
			throw new IllegalArgumentException("Set failed. Required index >0 and index < size.");
		data[index] = e;
	}
	
	// 查找數組中是否有元素e
	public boolean contains(E e){
		for(int i = 0; i < size; i++){
			if(data[i].equals(e))
				return true;
		}
		return false;
	}
	
	// 查找數組中元素e所在的索引,如果不存在元素e,則返回-1
	public int find(E e){
		for (int i = 0; i < data.length; i++) {
			if(data[i].equals(e))
				return i;
		}
		return -1;
	}
	
	// 從數組中刪除index位置的元素, 返回刪除的元素
	public E remove(int index){
		if(index < 0 || index >= size){
			throw new IllegalArgumentException("Remove failed. Required index >0 and index <size");
		}
		E ret = data[index];
		for (int i = index+1; i < size; i++) {
			data[i-1] = data[i];
		}
		size--;
		data[size]=null;  // // loitering objects != memory leak
		
		if(size==data.length/4  && data.length/2 !=0 )
			resize(data.length/2);
		
		return ret;
		
	}
	
	// 從數組中刪除第一個元素, 返回刪除的元素
	public E removeFirst(){
		return remove(0);
	}
	
	// 從數組中刪除最後一個元素, 返回刪除的元素
	public E removeLast(){
		return remove(size-1);
	}
	
	// 從數組中刪除元素e
	public void removeElement(E e){
		int index = find(e);
		if(index != -1)
			remove(index);
	}
	
		
	@Override
	public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d , capacity = %d\n", size,data.length));
        res.append('[');
        for (int i = 0; i < size; i++) {
        	res.append(data[i]);
        	if(i != size -1)
        		res.append(",");
		}
        res.append(']');
        return res.toString();
	}


    // 將數組空間的容量變成newCapacity大小
	public void resize(int newCapacity) {
		E[] newData = (E[])new Object[newCapacity];
		for(int i = 0; i < size; i++){
			newData[i] = data[i];
		}
		data = newData;
	}
}

 

 

上面的代碼相對來講較爲完善了,如果大家有什麼疑問,或者有什麼相關建議,歡迎留言呀♥(ˆ◡ˆԅ)

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章