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();
    
        }
    

      

  • 相关阅读:
    轻配置 Vim
    PHP 使用 wkhtmltopdf/image 把HTML页面转换成 PDF/image 文件
    使用 Mailgun 实现 带附件的Email 发送功能
    LinkedIn 第三方登录(JavaScript SDK)
    react学习2
    前端面试题
    react学习
    前端面试-字符串-逆序-间隔
    前端文件上传相关知识
    js原型prototype问题
  • 原文地址:https://www.cnblogs.com/chancy/p/9155743.html
Copyright © 2011-2022 走看看