カスタムダイナミック配列の実装

7199 ワード

配列の最大の利点:クイッククエリー
ダイナミック配列:
public class Array {   //E    

    private E[] data;
    private int size;//          ,               

    //    ,       capacity   Array
    public Array(int capacity){
        data = (E[])new Object[capacity];
        size = 0;
    }

    //       ,       capacity=10
    public Array(){
        this(10);
    }

    //              
    public int getSize(){
        return size;
    }

    //      
    public int getCapacity(){
        return data.length;
    }

    //          
    public boolean isEmpty(){
        return size == 0;
    }

    //size                
    //           ,       size           
    //              
    //O(1)
    //    addLast  ,  2     ,      ,      O(1) 
    public void addLast(E e){

//        if(size == data.length){
//            //               ,        
//            throw new IllegalArgumentException("Array is full");
//        }
//
//        //       data[size++] = e;
//        data[size] = e;
//        size ++;

        //  add  
        add(size,e);
    }

    //             
    //O(n)
    public void addFirst(E e){
        add(0,e);
    }

    //            
    // index          e
    //O(n/2), O(n)
    public void add(int index, E e){
        //          index    
        if(index < 0 || index > size){
            throw new IllegalArgumentException("Required index >= 0 and index <= size");
        }

        //                        
        //  
        if(size == data.length){
            //               ,  
            resize(2 * data.length);
        }

        for(int i = size - 1; i >= index; i--){
            //          (size-1),         (index),
            //                 
            data[i + 1] = data[i];
        }

        data[index] = e;
        size ++;
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d , capacity = %d
", 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(); } // index //O(1) public E get(int index){ // , if(index < 0 || index >= size){ throw new IllegalArgumentException("Index is Illegal"); } return data[index]; } // index e //O(1) public void set(int index, E e){ // , if(index < 0 || index >= size){ throw new IllegalArgumentException("Index is Illegal"); } data[index] = e; } // e //O(n) public boolean contains(E e){ for(int i = 0; i < size; i++){ //if(data[i] == e){ if(data[i].equals(e)){ return true; } } return false; } // e , e, -1 //O(n) public int find(E e){ for(int i = 0; i < size; i++){ //if(data[i] == e){ if(data[i].equals(e)){ return i; } } return -1; } // index , //O(n/2) = O(n) public E remove(int index){ // , if(index < 0 || index >= size){ throw new IllegalArgumentException("Index is Illegal"); } E ret = data[index];// , for(int i = index + 1; i < size; i++){ // index , data[i - 1] = data[i]; } size--; data[size] = null; // // , Lazy //if(size == data.length / 2){ if(size == data.length / 2 && data.length / 2 != 0){ resize(data.length / 2); } return ret; } // , //O(n) public E removeFirst(){ return remove( 0); } // , //O(1) public E removeLast(){ return remove(size - 1); } // e public void removeElement(E e){ int index = find(e); if(index != -1){ remove(index); } } // //O(n) private void resize(int newCapacity){ E[] newData = (E[])new Object[newCapacity]; for(int i = 0; i < size; i++){ newData[i] = data[i]; } data = newData; } }
テストクラス1:テストの基本機能
public class Main1 {
    public static void main(String[] args) {
        Array arr = new Array(20);

        for(int i = 0; i < 10; i++){
            arr.addLast(i);
        }

        /*
            Array: size = 10 , capacity = 20
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);

        arr.add(1,100);
        /*
            Array: size = 11 , capacity = 20
            [0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);

        /*
            Array: size = 12 , capacity = 20
            [-1, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        arr.addFirst(-1);
        System.out.println(arr);

        arr.remove(2);
        /*
            Array: size = 11 , capacity = 20
            [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);

        arr.removeElement(4);
        /*
            Array: size = 10 , capacity = 20
            [-1, 0, 1, 2, 3, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);


        arr.removeFirst();
        /*
            Array: size = 9 , capacity = 20
            [0, 1, 2, 3, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);
    }
}
テストクラス2:拡張機能をテストする
public class Main {
    //      
    public static void main(String[] args) {
        Array arr = new Array();

        for(int i = 0; i < 10; i++){
            arr.addLast(i);
        }

        /*
            Array: size = 10 , capacity = 20
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);

        arr.add(1,100);
        /*
            Array: size = 11 , capacity = 20
            [0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        System.out.println(arr);

        /*
                  10    20
            Array: size = 11 , capacity = 20
            [0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        arr.addFirst(-1);
        System.out.println(arr);

        //arr.remove(2);
        /*
            Array: size = 11 , capacity = 20
            [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
         */
        //System.out.println(arr);

        //arr.removeElement(4);
        /*
            Array: size = 10 , capacity = 20
            [-1, 0, 1, 2, 3, 5, 6, 7, 8, 9]
         */
        //System.out.println(arr);


        //arr.removeFirst();
        /*
            Array: size = 9 , capacity = 20
            [0, 1, 2, 3, 5, 6, 7, 8, 9]
         */
        //System.out.println(arr);
    }
}