zoukankan      html  css  js  c++  java
  • 面向对象数组操作

    封装面向对象数组,并且支持有序和无序,查询元素分为顺序查找和二分法。

      1 /** 
      2  * @ClassName: MyArray 
      3  * @Description: 封装自己数组
      4  * @author dongye 
      5  * @date 2016年3月1日 上午9:28:40 
      6  *  
      7  */
      8 public class MyArray {
      9     private long[] arr;
     10     //有效长度
     11     private int elements;
     12     
     13     public MyArray(){
     14         arr=new long[50];
     15     }
     16     
     17     public MyArray(int maxsize){
     18         arr=new long[maxsize];
     19     }
     20      
     21     /** 
     22      * @Description:无序插入
     23      * @return void    返回类型 
     24      * @author dongye  
     25      * @date 2016年3月1日 上午9:31:01 
     26      */
     27     public void insert(long value) {
     28         arr[elements]=value;
     29         elements++;
     30     }
     31     
     32     /**
     33      * 
     34     * @Title: insertOrder 
     35     * @Description: 有序插入
     36     * @return void    返回类型 
     37     * @author dongye  
     38     * @date 2016年3月1日 上午10:10:52 
     39     * @throws
     40      */
     41     public void insertOrder(long value) {
     42         int i;
     43         for (i = 0; i < elements; i++) {
     44             if(arr[i]>value){
     45                 break;
     46             }
     47         }
     48         for(int j=elements;j>i;j--){
     49             arr[j]=arr[j-1];
     50         }
     51         arr[i]=value;
     52         elements++;
     53     }
     54     
     55     
     56     
     57     
     58     /**
     59      * 
     60     * @Title: display 
     61     * @Description: 显示元素
     62     * @author dongye  
     63     * @date 2016年3月1日 上午9:32:25 
     64      */
     65     public void display(){
     66         System.out.print("[");
     67         for (int i = 0; i <elements; i++) {
     68             System.out.print(arr[i]+" ");
     69         }
     70         System.out.println("]");
     71     }
     72     
     73     /**
     74      * 
     75     * @Description: 查找数据(线性查找) 对于无序有序都可以
     76     * @author dongye  
     77     * @date 2016年3月1日 上午9:35:37 
     78      */
     79     public int search(long value){
     80         int i;
     81         for (i = 0; i < elements; i++) {
     82             if(value==arr[i]){
     83                 break;
     84             }
     85         }
     86         if(i==elements){
     87             i=-1;
     88         }
     89         return i;
     90     }
     91     
     92     public int binarySearch(long value){
     93         int middle=0;
     94         int low=0;
     95         int pow=elements;
     96         while(true){
     97             middle=(low+pow)/2;
     98             System.out.println(low+":"+pow);
     99             System.out.println(middle);
    100             if(value==arr[middle]){
    101                 return middle;
    102             }else if(low>pow){
    103                 return -1;
    104             }else{
    105                 if(value>arr[middle]){
    106                     low=middle+1;
    107                 }else{
    108                     pow=middle-1;
    109                 }
    110             }
    111         }
    112     }
    113     
    114     /**
    115      * 
    116     * @Description: 根据索引查找元素
    117     * @return long    返回类型 
    118     * @author dongye  
    119     * @date 2016年3月1日 上午9:40:59 
    120      */
    121     public long get(int index){
    122         if(index>=elements||index<=0){
    123             throw new ArrayIndexOutOfBoundsException();
    124         }else{
    125             return arr[index];
    126         }
    127     }
    128     
    129     /** 
    130      * @Title: delete 
    131      * @Description: 删除数据
    132      * @return void    返回类型 
    133      * @author dongye  
    134      * @date 2016年3月1日 上午9:41:57 
    135      * @throws 
    136      */
    137     public void delete(int index) {
    138         if(index>=elements||index<=0){
    139             throw new ArrayIndexOutOfBoundsException();
    140         }else{
    141             for (int i = index; i < elements; i++) {
    142                 arr[index]=arr[index+1];
    143                 elements--;
    144             }
    145         }
    146     }
    147     
    148     /**
    149      * 
    150     * @Title: change 
    151     * @Description: 更新数据 
    152     * @return void    返回类型 
    153     * @author dongye  
    154     * @date 2016年3月1日 上午9:45:27 
    155     * @throws
    156      */
    157     public void change(int index,int newValue){
    158         if(index>=elements||index<=0){
    159             throw new ArrayIndexOutOfBoundsException();
    160         }else{
    161             arr[index]=newValue;
    162         }
    163     }
    164     
    165 
    166 }
  • 相关阅读:
    决策树(chap3)Machine Learning In Action学习笔记
    AdaBoost-Machine Learning In Action学习笔记
    支持向量机-Machine Learning In Action学习笔记
    Alamofire 4.0 迁移指南
    从VSS到SVN再到Git 记Git的基本操作
    倍杀测量者
    P3166 数三角形
    APIO2014 序列分割(斜率优化好题)
    P3694 邦邦的大合唱站队
    ACwing 298 栅栏(单调队列DP)
  • 原文地址:https://www.cnblogs.com/snow1314/p/5233892.html
Copyright © 2011-2022 走看看