数组

public class MyArray {
    private long[] arr;
    private int elements;//数组长度

    public MyArray(){
        arr = new long[50];
    }

    public MyArray(int maxsize){
        arr = new long[maxsize];
    }



    /**
     * insert,增加一个数据,添加在末尾
     */
    public void insert(long value){
        arr[elements]=value;
        elements++;
    }

    /**
     * 增加一个数组,按照从小到大顺序
     * @param value
     */
    public void insert2(long value){
       int i;
       for(i=0;i<elements;i++){
           if (arr[i]>value){
               break;
           }
       }
       elements++;
       for(int j = elements-1;j>i;j--){
           arr[j]=arr[j-1];
       }
       arr[i]=value;
    }

    
    /**
     * 显示数组
     */
    public void display(){
        System.out.println("[");
        for (int i=0;i<elements;i++){
            System.out.println(arr[i]);
        }
        System.out.println("]");
    }


    /**
     * 根据值查索引
     */
    public int findIndex(long value){
        int i;
        for (i = 0; i < elements; i++) {
            if (arr[i]==value){
                break;
            }
        }
        return i;
    }


    /**
     * 根据索引值查值
     */
    public long findValue(int indexof){
        if (indexof>=elements||indexof<0){
            throw new ArrayIndexOutOfBoundsException();
        }
        return arr[indexof];
    }

    /**
     * 根据索引删除值
     * @param index
     */
    public void delete(int index){
        if (index>=elements||index<0){
            throw new ArrayIndexOutOfBoundsException();
        }
        for (int i = index;i<elements;i++){
            arr[i] = arr[i+1];
        }
        elements--;
    }

    /**
     * 更新数组
     * @param index
     * @param value
     */
    public void update(int index,long value){
        if (index>=elements||index<0){
            throw new ArrayIndexOutOfBoundsException();
        }
        arr[index]=value;
    }
    
    public int findSearch(int value){
        int start = 0;
        int end = elements-1;
        int middle;
        while (true){
            middle = (start+end)/2;
            if (start>end){
                return -1;
            }
            
        }
    }




}

posted @ 2020-03-10 13:06  姚狗蛋  阅读(77)  评论(0编辑  收藏  举报