数组_增删查遍历

public class Array {
    private long[] a;
    private int nElems;
    public Array(int maxSize) {
        a=new long[maxSize];
        nElems=0;
    }
    public boolean find(long searchKey) {
        int j;
        for(j=0;j<nElems;j++) {
            if(a[j]==searchKey)
                break;
        }
        if(j==nElems)
            return false;
        else 
            return true;
    }
    public void insert(long value) {
        a[nElems]=value;
        nElems++;
    }
    public boolean delete(long value) {
        int j;
        for(j=0;j<nElems;j++)
            if(a[j]==value)
                break;
        if(j==nElems)
            return false;
        else
            for(int k=j;k<nElems;k++)
                a[k]=a[k+1];
        nElems--;
        return true;
    }
    public void display() {
        for(int j=0;j<nElems;j++)
            System.out.print(a[j]+" ");
        System.out.println();
    }

}
public class Test {
    public static void main(String[] agrs) {
        int maxSize=100;
        Array array;
        array=new Array(maxSize);
        array.insert(77);
        array.insert(99);
        array.insert(44);
        array.insert(55);
        array.insert(22);
        array.insert(88);
        array.insert(11);
        array.insert(00);
        array.insert(66);
        array.insert(33);
        array.display();
        System.out.println("found35?"+array.find(35));
        array.delete(00);
        array.delete(55);
        array.delete(99);
        array.display();
    }

}

 

posted @ 2017-12-22 10:48  S-Mustard  阅读(159)  评论(0编辑  收藏  举报