Java中使用自定义类封装数组,添加类方法实现数据操作

1、具体见注释

2、后续或有更新

  1 public class MyArray {
  2     private long[] array;
  3     private int cnt; // 自定义数组类的元素个数
  4 
  5     /**
  6     使用自定义类封装数组,添加类方法实现数据操作
  7     */
  8     public MyArray() {
  9         array = new long[50];
 10     }
 11 
 12     public MyArray(int size) {
 13         array = new long[size];
 14     }
 15 
 16     /**
 17     插入数据,返回值为空
 18     */
 19     public void insert(long insertValue) {
 20         array[cnt++] = insertValue;
 21     }
 22 
 23     /**
 24     显示数据,返回值为空
 25     */
 26     public void display() {
 27         System.out.print("[");
 28         for (int i = 0; i < cnt ; ++i) {
 29             System.out.print(array[i]);
 30             if (i != cnt - 1) {
 31                 System.out.print(",");
 32             }
 33         }
 34         System.out.println("]");
 35     }
 36 
 37     /**
 38     按值查找数据,返回索引值
 39     算法:线性查找
 40     */
 41     public int search(long targetValue) {
 42         int i;
 43         int searchResult;
 44         for (i = 0; i < cnt; ++i) {
 45             if (targetValue == array[i]) {
 46                 break;
 47             }
 48         }
 49         if (i == cnt) {
 50             searchResult = -1;
 51         } else {
 52             searchResult = i;
 53         }
 54         return searchResult; // 保持单一出口
 55 
 56     }
 57 
 58     /**
 59     按索引查找数据,返回值为目标数据
 60     */
 61     public long get(int targetIndex) {
 62         if (targetIndex < 0 || targetIndex >= cnt) {
 63             throw new ArrayIndexOutOfBoundsException();
 64         } else {
 65             return array[targetIndex];
 66         }
 67     }
 68 
 69     /**
 70     按值删除数据,返回其索引值
 71     */
 72     public int deleteByValue(long deleteValue) {
 73         int i;
 74         int deleteResult;
 75         for (i = 0; i < cnt; ++i) {
 76             if (array[i] == deleteValue) {
 77                 int j;
 78                 for (j = i; j < cnt-1; ++j) {
 79                     array[j] = array[j+1];
 80                 }
 81                 array[j] = array[--cnt];
 82                 break; // 仅删除从左到右第一个找到的目标值
 83             }
 84         }
 85         if (i == cnt) {
 86             deleteResult = -1;
 87         } else {
 88             deleteResult = i;
 89         }
 90         return deleteResult; // 保持单一出口
 91 
 92     }
 93 
 94 
 95     /**
 96     按索引删除数据,返回值为空
 97     */
 98     public void delete(int index) {
 99         if (index < 0 || index >= cnt) {
100             throw new ArrayIndexOutOfBoundsException();
101         } else {
102             int i;
103             for (i = index; i < cnt - 1; ++i) {
104                 array[i] = array[i + 1];
105             }
106             //array[i] = array[cnt - 1];
107             //cnt--;
108 
109             array[i] = array[--cnt]; // 替换上两行
110         }
111     }
112 
113     /**
114     根据索引值,更新数据,返回值为空
115     */
116     public void update(int index, int newValue) {
117         if (index < 0 || index >= cnt) {
118             throw new ArrayIndexOutOfBoundsException();
119         } else {
120             array[index] = newValue;
121         }
122     }
123 
124     public static void main(String[] args) {
125         MyArray array = new MyArray(3);
126         array.insert(13);
127         array.insert(34);
128         array.insert(90);
129 
130         array.display();
131 
132         array.deleteByValue(34);
133 
134         array.display();
135 
136     }
137 
138 }

 3、添加自定义有序数组类

  1 public class MyOrderArray {
  2     private long[] array;
  3     private int cnt; // 自定义数组类的元素个数
  4 
  5     /**
  6     使用自定义类封装数组,添加类方法实现数据操作
  7     */
  8     public MyOrderArray() {
  9         array = new long[50];
 10     }
 11 
 12     public MyOrderArray(int size) {
 13         array = new long[size];
 14     }
 15 
 16     /**
 17     按序插入数据,返回值为空
 18     */
 19     public void insert(long insertValue) {
 20         int i;
 21         for (i = 0; i < cnt; ++i) {
 22             if (array[i] > insertValue) {
 23                 break;
 24             }
 25         }
 26         int j;
 27         for (j = cnt; j > i; --j) {
 28             array[j] = array[j - 1];
 29         }
 30         array[i] = insertValue;
 31         cnt++;
 32     }
 33 
 34     /**
 35     显示数据,返回值为空
 36     */
 37     public void display() {
 38         System.out.print("[");
 39         for (int i = 0; i < cnt ; ++i) {
 40             System.out.print(array[i]);
 41             if (i != cnt - 1) {
 42                 System.out.print(",");
 43             }
 44         }
 45         System.out.println("]");
 46     }
 47 
 48     /**
 49     按值查找数据,返回索引值
 50     算法:线性查找
 51     */
 52     public int search(long targetValue) {
 53         int i;
 54         int searchResult;
 55         for (i = 0; i < cnt; ++i) {
 56             if (targetValue == array[i]) {
 57                 break;
 58             }
 59         }
 60         if (i == cnt) {
 61             searchResult = -1;
 62         } else {
 63             searchResult = i;
 64         }
 65         return searchResult; // 保持单一出口
 66 
 67     }
 68 
 69     /**
 70     按索引查找数据,返回值为目标数据
 71     */
 72     public long get(int targetIndex) {
 73         if (targetIndex < 0 || targetIndex >= cnt) {
 74             throw new ArrayIndexOutOfBoundsException();
 75         } else {
 76             return array[targetIndex];
 77         }
 78     }
 79 
 80     /**
 81     按值删除数据,返回其索引值
 82     */
 83     public int deleteByValue(long deleteValue) {
 84         int i;
 85         int deleteResult;
 86         for (i = 0; i < cnt; ++i) {
 87             if (array[i] == deleteValue) {
 88                 int j;
 89                 for (j = i; j < cnt-1; ++j) {
 90                     array[j] = array[j+1];
 91                 }
 92                 array[j] = array[--cnt];
 93                 break; // 仅删除从左到右第一个找到的目标值
 94             }
 95         }
 96         if (i == cnt) {
 97             deleteResult = -1;
 98         } else {
 99             deleteResult = i;
100         }
101         return deleteResult; // 保持单一出口
102 
103     }
104 
105 
106     /**
107     按索引删除数据,返回值为空
108     */
109     public void delete(int index) {
110         if (index < 0 || index >= cnt) {
111             throw new ArrayIndexOutOfBoundsException();
112         } else {
113             int i;
114             for (i = index; i < cnt - 1; ++i) {
115                 array[i] = array[i + 1];
116             }
117             //array[i] = array[cnt - 1];
118             //cnt--;
119 
120             array[i] = array[--cnt]; // 替换上两行
121         }
122     }
123 
124     /**
125     根据索引值,更新数据,返回值为空
126     */
127     public void update(int index, int newValue) {
128         if (index < 0 || index >= cnt) {
129             throw new ArrayIndexOutOfBoundsException();
130         } else {
131             array[index] = newValue;
132         }
133     }
134 
135     public static void main(String[] args) {
136         MyOrderArray array = new MyOrderArray(3);
137         array.insert(90);
138         array.insert(13);
139         array.insert(34);
140 
141         array.display();
142 
143         array.deleteByValue(34);
144 
145         array.display();
146 
147     }
148 
149 }

 4、MyArray类与MyOrderArray类目前仅区别于insert方法,后续或有更新

 5、MyOrderArray类新增二分查找方法binarySearch,具体细节见该方法代码

  1 public class MyOrderArray {
  2     private long[] array;
  3     private int cnt; // 自定义数组类的元素个数
  4 
  5     /**
  6     使用自定义类封装数组,添加类方法实现数据操作
  7     */
  8     public MyOrderArray() {
  9         array = new long[50];
 10     }
 11 
 12     public MyOrderArray(int size) {
 13         array = new long[size];
 14     }
 15 
 16     /**
 17     按序插入数据,返回值为空
 18     */
 19     public void insert(long insertValue) {
 20         int i;
 21         for (i = 0; i < cnt; ++i) {
 22             if (array[i] > insertValue) {
 23                 break;
 24             }
 25         }
 26         int j;
 27         for (j = cnt; j > i; --j) {
 28             array[j] = array[j - 1];
 29         }
 30         array[i] = insertValue;
 31         cnt++;
 32     }
 33 
 34     /**
 35     显示数据,返回值为空
 36     */
 37     public void display() {
 38         System.out.print("[");
 39         for (int i = 0; i < cnt ; ++i) {
 40             System.out.print(array[i]);
 41             if (i != cnt - 1) {
 42                 System.out.print(",");
 43             }
 44         }
 45         System.out.println("]");
 46     }
 47 
 48     /**
 49     按值查找数据,返回索引值
 50     算法:线性查找
 51     */
 52     public int search(long targetValue) {
 53         int i;
 54         int searchResult;
 55         for (i = 0; i < cnt; ++i) {
 56             if (targetValue == array[i]) {
 57                 break;
 58             }
 59         }
 60         if (i == cnt) {
 61             searchResult = -1;
 62         } else {
 63             searchResult = i;
 64         }
 65         return searchResult; // 保持单一出口
 66 
 67     }
 68 
 69     /**
 70     按值查找数据,返回索引值
 71     算法:二分查找
 72     */
 73     public int binarySearch(long targetValue) {
 74         int middle = 0;
 75         int low = 0;
 76         int top = cnt;
 77 
 78         while (true) {
 79             middle = (top + low) / 2;
 80             if (targetValue == array[middle]) {
 81                 return middle;
 82             } else if (low > top) {
 83                 return -1;
 84             } else if (targetValue < array[middle]) {
 85                 top = middle - 1; // 切记减一
 86             } else if (targetValue >= array[middle]) {
 87                 low = middle + 1; // 切记加一
 88             }
 89         }
 90     }
 91 
 92     /**
 93     按索引查找数据,返回值为目标数据
 94     */
 95     public long get(int targetIndex) {
 96         if (targetIndex < 0 || targetIndex >= cnt) {
 97             throw new ArrayIndexOutOfBoundsException();
 98         } else {
 99             return array[targetIndex];
100         }
101     }
102 
103     /**
104     按值删除数据,返回其索引值
105     */
106     public int deleteByValue(long deleteValue) {
107         int i;
108         int deleteResult;
109         for (i = 0; i < cnt; ++i) {
110             if (array[i] == deleteValue) {
111                 int j;
112                 for (j = i; j < cnt-1; ++j) {
113                     array[j] = array[j+1];
114                 }
115                 array[j] = array[--cnt];
116                 break; // 仅删除从左到右第一个找到的目标值
117             }
118         }
119         if (i == cnt) {
120             deleteResult = -1;
121         } else {
122             deleteResult = i;
123         }
124         return deleteResult; // 保持单一出口
125 
126     }
127 
128     /**
129     按索引删除数据,返回值为空
130     */
131     public void delete(int index) {
132         if (index < 0 || index >= cnt) {
133             throw new ArrayIndexOutOfBoundsException();
134         } else {
135             int i;
136             for (i = index; i < cnt - 1; ++i) {
137                 array[i] = array[i + 1];
138             }
139             //array[i] = array[cnt - 1];
140             //cnt--;
141 
142             array[i] = array[--cnt]; // 替换上两行
143         }
144     }
145 
146     /**
147     根据索引值,更新数据,返回值为空
148     */
149     public void update(int index, int newValue) {
150         if (index < 0 || index >= cnt) {
151             throw new ArrayIndexOutOfBoundsException();
152         } else {
153             array[index] = newValue;
154         }
155     }
156 
157     public static void main(String[] args) {
158         MyOrderArray array = new MyOrderArray(3);
159         array.insert(90);
160         array.insert(13);
161         array.insert(34);
162 
163         array.display();
164 
165         //array.deleteByValue(34);
166         System.out.println(array.binarySearch(90));
167 
168         array.display();
169 
170     }
171 
172 }

 

转载于:https://www.cnblogs.com/valuestack/p/custom-class-array.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值