zoukankan      html  css  js  c++  java
  • java学习之—数组的曾删改查

    /**
     * 数组的曾删改查
     * Create by Administrator
     * 2018/6/8 0008
     * 上午 9:54
     **/
    public class HighArray {
        private long[] a;
    
        private int nElems;
    
        public HighArray(int max){
            a = new long[max];
            nElems = 0;
        }
    
        /**
         * 使用二分查找法
         * @param searchKey
         * @return
         */
        public int findOne(long searchKey){
            // 数组的第一个
            int lowerBound = 0;
            // 数组的最后一个
            int upperBound = nElems - 1;
            // 数组的一半,下标
            int curIn;
            while (true){
                curIn = (lowerBound + upperBound) / 2;
                //判断这个数是不是数组的对半curIn是否相等
                if(a[curIn] == searchKey){
                    return curIn;
                }else if(lowerBound > upperBound){ //如果成立 测范围已经不存在了
                    return nElems;
                }else {
                    //如果成立 则将范围设置curIn的后半部分,反之就设置为前半部分
                    if(a[curIn] < searchKey){
                        lowerBound = curIn + 1;
                    }else {
                        upperBound = curIn - 1;
                    }
                }
            }
        }
    
        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++;
        }
    
        /**
         * 保存排序
         * @param value
         */
        public void save(long value){
            int j;
            for (j=0; j<nElems; j++){
                if(a[j] > value){
                    break;
                }
            }
            for (int i = nElems; i > j; i--) {
                a[i] = a[i-1];
            }
            a[j] = value;
            nElems++;
        }
    
        public boolean delete(long value){
            int j = findOne(value);
    //        for(j = 0; j < nElems; j++){
    //            if(a[j] == value){
    //                break;
    //            }
    //        }
            if (j == nElems){
                return false;
            }else {
                for (int i = j; i < nElems; i++) {
                    a[i] = a[i+1];
                }
                nElems--;
                return true;
            }
        }
    
        public void show(){
            for (int i = 0; i < nElems; i++) {
                System.out.print(a[i]+ " ");
            }
            System.out.println("");
        }
    
        public static void main(String[] args) {
    
            HighArray array = new HighArray(100);
    
            array.save(10);
            array.save(3);
            array.save(2);
            array.save(11);
            array.save(9);
            array.save(5);
            array.save(4);
            array.save(2);
            array.save(7);
    
            array.show();
            long num = 0;
            System.out.println("查找结果"+num+":"+ array.find(num));
    
            array.delete(2);
            array.delete(5);
            array.delete(8);
    
            array.show();
    
        }
    

      

  • 相关阅读:
    IOS 关于分辨率的那点事
    IOS多线程编程之NSOperation和NSOperationQueue的使用
    UI应遵循的三大网站设计原则
    Xcode 4.4中LLVM compiler 4.0带来的ObjectiveC新语法特性
    iPhone实战:操作SQLite
    flock()函数使用示例
    libtool: syntax error near unexpected token `]*'
    求职简历撰写要点
    thrift的js客户端收到含汉字字符中显示为乱码解决方法
    多写引发的思考
  • 原文地址:https://www.cnblogs.com/chancy/p/9155743.html
Copyright © 2011-2022 走看看