
正文
Java实现自定义数组及其方法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
自定义数组
主要功能有增、删(根据索引,根据值)、改、查扩容等功能
package array;
public class CustomArray {
private int[] array = null;
//数组有效长度
public int length = 0;
//空参构造函数,默认数组大小为10
public CustomArray() {
this.array = new int[10];
}
public CustomArray(int size) {
this.array = new int[size];
}
//给自定义数组添加元素
public void insert(int number) {
//判断数组是否满
//满了,扩容,扩容需要新建一个数组,将旧的数据复制过去,再插入
//没满,直接插入
//插入之后length+1
if (length == array.length) {
expand(this.array);
array[length] = number;
} else {
this.array[length] = number;
}
length++;
}
//根据索引删除元素
public void deleteByIndex(int index) throws Exception {
//判断索引是否越界,即超过了有效长度
//超过了,抛出异常提示
//没超过就删除
//首先需要将该索引之后的所有元素前移一个位置。
//最后length-1
if (index > length - 1 || index < 0) {
throw new Exception("删除时索引越界");
} else {
for (int i = index; i < length; i++) {
array[i] = array[i + 1];
}
length--;
}
}
//根据值删除元素,删除多个
public void deleteByValue(int value) throws Exception {
//首先看数组中有没有这个值,没有抛异常提示
boolean flag = false;
for (int i = 0; i < length; i++) {
if (array[i] == value) {
flag = true;
deleteByIndex(i);
}
}
if (!flag)
throw new Exception("该元素不存在");
deleteOne(value);
}
//删除一个元素
public void deleteOne(int value) throws Exception {
boolean flag = false;
for (int i = 0; i < length; i++) {
if (array[i] == value) {
flag = true;
deleteByIndex(i);
break;
}
}
if (!flag)
throw new Exception("该元素不存在");
}
//修改某索引对应元素的值
public void update(int index, int value) throws Exception {
if (index > length - 1 || index < 0) {
throw new Exception("修改时索引越界");
} else {
array[index] = value;
}
}
//(遍历)数组的元素
public void travel() {
System.out.print("[");
for (int i = 0; i < length; i++) {
System.out.print(array[i]);
if (i < length - 1)
System.out.print(",");
}
System.out.println("]");
}
//根据值查找元素,返回索引
public int search(int value) throws Exception {
int i = 0;
for (i = 0; i < length; i++) {
if (value == array[i])
break;
}
if (i == length)
return -1;
return i;
}
//根据索引元素,返回值
public int searchByIndex(int index) throws Exception {
if(index<0||index>=length){
throw new Exception("索引越界");
}
return array[index];
}
//每次扩容后比之前大一倍
public void expand(int[] arr) {
int expandSize = arr.length * 2;
this.array = new int[expandSize];
for (int i = 0; i < arr.length; i++) {
this.array[i] = arr[i];
}
}
}
测试类如下:
package array;
public class ArrayTest {
public static void main(String[] args) throws Exception {
CustomArray array=new CustomArray();
array.insert(10);
array.insert(9);
array.insert(8);
array.insert(4);
array.insert(5);
array.insert(6);
array.deleteByIndex(0);
array.travel();
}
}
自定义有序数组
主要功能有插入、二分查找递归版、二分查找非递归
package array;
public class OrderArray {
private int[] array = null;
//数组有效长度
public int length = 0;
public OrderArray() {
this.array = new int[50];
}
public OrderArray(int size) {
this.array = new int[size];
}
//此处有许多细节
public void insert(int value) {
int i;
for (i = 0; i < length; i++) {
if (value < array[i])
break;
}
for (int j = length - 1; j >=i; j--) {
array[j+1] = array[j];
}
array[i] = value;
length++;
}
public void travel() {
System.out.print("[");
for (int i = 0; i < length; i++) {
System.out.print(array[i]);
if (i < length - 1)
System.out.print(",");
}
System.out.println("]");
}
//二分查找,返回索引
public int binarySearch(int value){
int left=0,right=length-1;
int mid;
while(left<=right){
mid=(left+right)/2;
if(value==array[mid])
return mid;
if(value<array[mid]){
right=mid-1;
}else{
left=mid+1;
}
}
return -1;
}
public int binarySearchByRecursion(int value,int begin,int end){
int mid=(begin+end)/2;
if(array[mid]==value)
return mid;
if(begin>end)
return -1;
if(value<array[mid]){
end=mid-1;
return binarySearchByRecursion(value,begin,end);
}else{
begin=mid+1;
return binarySearchByRecursion(value,begin,end);
}
}
}
测试类:
package array;
public class ArrayTest {
public static void main(String[] args) throws Exception {
OrderArray orderArray=new OrderArray();
orderArray.insert(9);
orderArray.insert(8);
orderArray.insert(7);
orderArray.insert(6);
orderArray.insert(5);
orderArray.travel();
System.out.println(orderArray.binarySearch(6));
System.out.println(orderArray.binarySearchByRecursion(6,0,orderArray.length-1));
}
}
通过以上练习可以很好的巩固基础编码能力
冰冻三尺非一日之寒,脚踏实地埋头干







