zoukankan      html  css  js  c++  java
  • Java实现自定义数组及其方法

    自定义数组

    主要功能有增、删(根据索引,根据值)、改、查扩容等功能

      1 package array;
      2 
      3 public class CustomArray {
      4     private int[] array = null;
      5     //数组有效长度
      6     public int length = 0;
      7 
      8     //空参构造函数,默认数组大小为10
      9     public CustomArray() {
     10         this.array = new int[10];
     11     }
     12 
     13     public CustomArray(int size) {
     14         this.array = new int[size];
     15     }
     16 
     17     //给自定义数组添加元素
     18     public void insert(int number) {
     19         //判断数组是否满
     20         //满了,扩容,扩容需要新建一个数组,将旧的数据复制过去,再插入
     21         //没满,直接插入
     22         //插入之后length+1
     23         if (length == array.length) {
     24             expand(this.array);
     25             array[length] = number;
     26         } else {
     27             this.array[length] = number;
     28         }
     29         length++;
     30 
     31     }
     32 
     33     //根据索引删除元素
     34     public void deleteByIndex(int index) throws Exception {
     35         //判断索引是否越界,即超过了有效长度
     36         //超过了,抛出异常提示
     37         //没超过就删除
     38             //首先需要将该索引之后的所有元素前移一个位置。
     39             //最后length-1
     40         if (index > length - 1 || index < 0) {
     41             throw new Exception("删除时索引越界");
     42         } else {
     43             for (int i = index; i < length; i++) {
     44                 array[i] = array[i + 1];
     45             }
     46             length--;
     47         }
     48     }
     49 
     50     //根据值删除元素,删除多个
     51     public void deleteByValue(int value) throws Exception {
     52         //首先看数组中有没有这个值,没有抛异常提示
     53         boolean flag = false;
     54         for (int i = 0; i < length; i++) {
     55             if (array[i] == value) {
     56                 flag = true;
     57                 deleteByIndex(i);
     58             }
     59         }
     60         if (!flag)
     61             throw new Exception("该元素不存在");
     62         deleteOne(value);
     63 
     64     }
     65 
     66     //删除一个元素
     67     public void deleteOne(int value) throws Exception {
     68         boolean flag = false;
     69         for (int i = 0; i < length; i++) {
     70             if (array[i] == value) {
     71                 flag = true;
     72                 deleteByIndex(i);
     73                 break;
     74             }
     75         }
     76         if (!flag)
     77             throw new Exception("该元素不存在");
     78 
     79     }
     80 
     81 
     82     //修改某索引对应元素的值
     83     public void update(int index, int value) throws Exception {
     84         if (index > length - 1 || index < 0) {
     85             throw new Exception("修改时索引越界");
     86         } else {
     87             array[index] = value;
     88         }
     89     }
     90 
     91     //(遍历)数组的元素
     92     public void travel() {
     93         System.out.print("[");
     94         for (int i = 0; i < length; i++) {
     95             System.out.print(array[i]);
     96             if (i < length - 1)
     97                 System.out.print(",");
     98         }
     99         System.out.println("]");
    100     }
    101 
    102     //根据值查找元素,返回索引
    103     public int search(int value) throws Exception {
    104         int i = 0;
    105         for (i = 0; i < length; i++) {
    106             if (value == array[i])
    107                 break;
    108         }
    109         if (i == length)
    110             return -1;
    111         return i;
    112     }
    113     //根据索引元素,返回值
    114     public int searchByIndex(int index) throws Exception {
    115         if(index<0||index>=length){
    116             throw new Exception("索引越界");
    117         }
    118         return array[index];
    119 
    120     }
    121 
    122     //每次扩容后比之前大一倍
    123     public void expand(int[] arr) {
    124         int expandSize = arr.length * 2;
    125         this.array = new int[expandSize];
    126 
    127         for (int i = 0; i < arr.length; i++) {
    128             this.array[i] = arr[i];
    129         }
    130     }
    131 
    132 
    133 }

    测试类如下:

     1 package array;
     2 
     3 public class ArrayTest {
     4 
     5     public static void main(String[] args) throws Exception {
     6 
     7         CustomArray array=new CustomArray();
     8         array.insert(10);
     9         array.insert(9);
    10         array.insert(8);
    11         array.insert(4);
    12         array.insert(5);
    13         array.insert(6);
    14         array.deleteByIndex(0);
    15 
    16         array.travel();
    17         
    18     }
    19 }

    自定义有序数组

    主要功能有插入、二分查找递归版、二分查找非递归

     1 package array;
     2 
     3 public class OrderArray {
     4     private int[] array = null;
     5     //数组有效长度
     6     public int length = 0;
     7 
     8     public OrderArray() {
     9         this.array = new int[50];
    10     }
    11 
    12     public OrderArray(int size) {
    13         this.array = new int[size];
    14     }
    15 
    16     //此处有许多细节
    17     public void insert(int value) {
    18 
    19         int i;
    20         for (i = 0; i < length; i++) {
    21             if (value < array[i])
    22                 break;
    23         }
    24         for (int j = length - 1; j >=i; j--) {
    25             array[j+1] = array[j];
    26         }
    27         array[i] = value;
    28 
    29         length++;
    30 
    31     }
    32 
    33     public void travel() {
    34         System.out.print("[");
    35         for (int i = 0; i < length; i++) {
    36             System.out.print(array[i]);
    37             if (i < length - 1)
    38                 System.out.print(",");
    39         }
    40         System.out.println("]");
    41     }
    42     //二分查找,返回索引
    43     public int binarySearch(int value){
    44         int left=0,right=length-1;
    45         int mid;
    46 
    47         while(left<=right){
    48             mid=(left+right)/2;
    49 
    50             if(value==array[mid])
    51                 return mid;
    52             if(value<array[mid]){
    53                 right=mid-1;
    54             }else{
    55                 left=mid+1;
    56             }
    57         }
    58         return -1;
    59     }
    60     public int binarySearchByRecursion(int value,int begin,int end){
    61         int mid=(begin+end)/2;
    62 
    63         if(array[mid]==value)
    64             return mid;
    65         if(begin>end)
    66             return -1;
    67         if(value<array[mid]){
    68             end=mid-1;
    69             return binarySearchByRecursion(value,begin,end);
    70         }else{
    71             begin=mid+1;
    72             return binarySearchByRecursion(value,begin,end);
    73         }
    74     }
    75 
    76 
    77 }

    测试类:

     1 package array;
     2 
     3 public class ArrayTest {
     4 
     5 
     6     public static void main(String[] args) throws Exception {
     7 
     8         OrderArray orderArray=new OrderArray();
     9         
    10         orderArray.insert(9);
    11         orderArray.insert(8);
    12         orderArray.insert(7);
    13         orderArray.insert(6);
    14         orderArray.insert(5);
    15         orderArray.travel();
    16         System.out.println(orderArray.binarySearch(6));
    17         System.out.println(orderArray.binarySearchByRecursion(6,0,orderArray.length-1));
    18 
    19     }
    20 
    21 }

    通过以上练习可以很好的巩固基础编码能力

    冰冻三尺非一日之寒,脚踏实地埋头干

  • 相关阅读:
    python地图投影转换
    python 矢量数据转栅格数据
    读CSV文件并写arcgis shp文件
    python 读取矢量文件
    python 读hdf4文件,再转写成一个tif文件
    python gdal 写GeoTiff文件
    python gdal 读取栅格数据
    linux下在用python向文件写入数据时' '不起作用
    【FFT&NTT 总结】
    -【网络流总结】
  • 原文地址:https://www.cnblogs.com/treasury/p/12594103.html
Copyright © 2011-2022 走看看