「大話データ構造」Javaは順序記憶の挿入と削除を実現する


前言
「大話データ構造」という本はC言語で書かれており,本で挙げた例を練習するためにJavaを用いて著者の考えに従ってシミュレーションを行っている.私のここは最も良い方法ではないかもしれませんが、もしあなたが見終わった後、私が書いたことに何か正しくないところや最適化できるところがあれば、伝言区で指摘してください.私は必ず真剣に研究します.
1.順次保存の挿入と削除
以下は実装コード
package com.hry.datastructure;

/**
 * @Author    
 * @Date 2019/11/14 14:01
 * @Desc
 **/
public class MyArray {

    /**
     *          (     P50)
     *
     * @param index            
     * @param array      
     * @return        
     */
    public Object getByIndex(int index, Object[] array) {
        //         
        return array[index];
    }

    /**
     *           (     P51)
     *
     * @param index   
     * @param item       
     * @param array       
     * @return         
     */
    public Object[] add(int index, Object item, Object[] array) {
        //               ,           ,           
        if (isEmpty(index, array)) {
            array[index] = item;
        }
        //         
        Object[] newArray = new Object[array.length + 1];
        //                
        for (int i = 0; i < index; i++) {
            newArray[i] = array[i];
        }
        //              
        newArray[index] = item;
        //                 
        for (int j = index + 1; j < newArray.length; j++) {
            newArray[j] = array[j - 1];
        }
        return newArray;
    }

    /**
     *              
     *
     * @param index   
     * @param array       
     * @return true-    | false-  
     */
    public boolean isEmpty(int index, Object[] array) {
        return array[index] == null;
    }

    /**
     *            (     P52)
     *
     * @param index   
     * @param array       
     * @return         
     */
    public Object[] deleteByIndex(int index, Object[] array) {
        //                
        if (isEmpty(index, array)) {
            throw new RuntimeException("    !       ");
        }
        //                   
        int nextValue = index + 1;
        array[index] = null;
        for (int i = index; i < array.length - 1; i++) {
            //         
            array[i] = array[nextValue++];
        }
        //       1
        //         
        Object[] newArray = new Object[array.length - 1];
        for (int i = 0; i < array.length - 1; i++) {
            //          
            newArray[i] = array[i];
        }
        return newArray;

    }

    public static void main(String[] args) {
        String[] array = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
        MyArray myArray = new MyArray();
        System.out.println("    3    :" + myArray.getByIndex(3, array));
        System.out.println("===================      ========================");
        Object[] add = myArray.add(3, 10, array);
        for (int i = 0; i < add.length; i++) {
            System.out.println("      [" + i + "]     :" + add[i]);
        }
        System.out.println("===================      ========================");
        Object[] objects = myArray.deleteByIndex(5, array);
        for (int i = 0; i < objects.length; i++) {
            System.out.println("      [" + i + "]     :" + objects[i]);
        }

    }

}