配列javaコード実装

2596 ワード

public class MyArray {
    //      
    private int [] intArray;
    //           
    private int elems;
    //         
    private int length;

    //         50   
    public MyArray(){
        elems = 0;
        length = 50;
        intArray = new int[length];
    }
    //    ,        length    
    public MyArray(int length){
        elems = 0;
        this.length = length;
        intArray = new int[length];
    }

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

    /**
     *       
     */
    public void display(){
        for(int i = 0 ; i < elems ; i++){
            System.out.print(intArray[i]+" ");
        }
        System.out.println();
    }

    /**
     *     
     * @param value,               ,                   。
     * @return      true,            false
     */
    public boolean add(int value){
        if(elems == length){
            return false;
        }else{
            intArray[elems] = value;
            elems++;
        }
        return true;
    }

    /**
     *         
     * @param i
     * @return               ,          
     *              ,        
     */
    public int get(int i){
        if(i<0 || i>elems){
            System.out.println("      ");
        }
        return intArray[i];
    }
    /**
     *     
     * @param searchValue
     * @return               ,     ,   -1
     */
    public int find(int searchValue){
        int i ;
        for(i = 0 ; i < elems ;i++){
            if(intArray[i] == searchValue){
                break;
            }
        }
        if(i == elems){
            return -1;
        }
        return i;
    }
    /**
     *     
     * @param value
     * @return          ,     false;    true,    
     */
    public boolean delete(int value){
        int k = find(value);
        if(k == -1){
            return false;
        }else{
            if(k == elems-1){
                elems--;
            }else{
                for(int i = k; i< elems-1 ; i++){
                    intArray[i] = intArray[i+1];

                }
                elems--;
            }
            return true;
        }
    }
    /**
     *     
     * @param oldValue  
     * @param newValue  
     * @return      true,      false
     */
    public boolean modify(int oldValue,int newValue){
        int i = find(oldValue);
        if(i == -1){
            System.out.println("          ");
            return false;
        }else{
            intArray[i] = newValue;
            return true;
        }
    }

}