用類的思想封裝一個數組,實現其基本功能

package array;

import com.sun.istack.internal.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author hegx
 */
public class Array {

    /**
     * 日誌變量
     */
    private static Logger logger = LoggerFactory.getLogger(Array.class);

    /**
     *定義常用常量
     */
    private static String NULL_VALUES = "傳入的值爲空";

    /**
     * 數組長度
     */
    public int length;

    /**
     * 數組元素
     */
    public int elms;

    /**
     * 定義一個字符串數據類型的數組
     */
    public String[] strArray;


    /**
     * 默認構造器
     */
    public Array() {
        this.elms = 0;
        this.length = 30;
        strArray = new String[length];
    }

    /**
     * 給定數組長度
     * @param length
     */
    public Array(int length) {
        this.elms = 0;
        this.length = length;
        strArray = new String[length];
    }

    /**
     * @return
     */
    private int size(){
        return this.elms;
    }


    /**
     * 新增元素
     * @param value
     * @return
     */
    private boolean add(@NotNull String value){
        if (null == value){
            logger.info(NULL_VALUES);
            return  false;
        }
        if (length == elms){
            logger.info("數組已經滿,無法再裝入新元素");
            return false;
        }
        strArray[elms] = value;
          elms++;
        System.out.println("成功添加一個元素:"+value);
        return true;
    }

    /**
     * 查詢數組中的元素  找到就返回該元素的下標值,否則返回 -1
     * @return index or -1
     */
    private int findMeter(String value){
        if (null == value){
            System.out.println(NULL_VALUES);
            return -1;
        }
        if (elms !=0){
            for (int i = 0; i< elms; i++){
                if (strArray[i]!=null&&strArray[i].equals(value)){
                    return i;
                }
            }
        }
          return -1;
    }


    /**
     * 根據下標值獲取數組元素
     * @param index
     * @return
     */
    private String getByIndex(@NotNull int index){
        if (index>=length || index <=-1){
            System.out.println("訪問下標越界,查找失敗");
            return null;
        }
        return strArray[index];
    }

    /**
     * 根據數組index更新 數組的內容
     * @param ordValue
     * @param newValue
     * @return 更新成功返回 true ,否則 false
     */
    private boolean updateMeter(String ordValue,String newValue){
        int index = findMeter(ordValue);
        if (index==-1){
            System.out.println("不存在的字符值");
            return false;
        }
        strArray[index] = newValue;
        System.out.println("更新成功");
        return true;
    }

    /**
     * 刪除元素
     * @param value
     * @return
     */
    private boolean delete(String value){
        int index = findMeter(value);
        if (index!=-1){
            strArray[index] = null;
            elms--;
            return true;
        }
        return false;
    }

    /**
     * 刪除元素
     * @param index
     * @return
     */
    private boolean delete(int index){
        if (index!=-1){
            strArray[index] = null;
            elms--;
            System.out.println("delete... success!");
            return true;
        }
        return false;
    }

    /**
     * 打印數組的值
     */
    private void display(){
        System.gc();
        if (elms !=0){
            System.out.println("<----------------------開始遍歷數組-------------->");
            for (int i = 0; i < elms; i++) {
                System.out.println(strArray[i]+"\t");
            }
        }
    }

    public static void main(String[] args) {
        Array array = new Array();
        array.add("helloWord");
        array.add("li");
        array.add("laoHe");
        System.out.println(array.size());
        int li = array.findMeter("li");
        System.out.println(array.getByIndex(li));
        System.out.println(li);
        array.delete(li);
        array.updateMeter("li","老何");
        System.out.println(array.delete("老何"));
        array.display();
        System.out.println(array.length);
    }
}

輸出結果:

這裏我配置了Java虛擬機相關參數來測試GC現象


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