1、数组实现增、删、改、查
/**
* 1) 数组的插入、删除、按照下标随机访问操作;
* 2)数组中的数据是int类型的;
*/
/**
* 测试数据注意事项:
*
* 插入数据:
* 1、判断空数组插入数据。
* 2、输入下标索引超出下标范围。
* 3、将数据插入到尾部。
*
* 查找数据:
* 1、输入下标超出下标范围
*
*
* 删除数据:
* 1、索引超出下标范围。
* 2、删除尾元素
*
*/
import java.util.Arrays;
/**
* 1) 数组的插入、删除、按照下标随机访问操作;
* 2)数组中的数据是int类型的;
*
*/
public class Array {
//声明变量
private int data[];
private int n;
private int count;
public static void main(String[] args) {
Array array = new Array(4);
//插入数据
array.insertHead(1);
System.out.println("插入数据:"+array.insert(1, 2));
array.insert(2, 3);
array.insert(3, 4);
//第一次打印所有数据
System.out.println("打印所有数据:");
array.printAll();
//查找
System.out.println("查找数据:"+array.find(1));
//删除数据
System.out.println("删除数据:"+array.delete(4));
//打印所有数据
System.out.println("打印所有数据:");
array.printAll();
}
/**
* @param capacity:用户传参,数组的大小
* 功能:构造函数(初始化数据)
*/
public Array(int capacity) {
//定义一个大小为 capacity 的数组
data = new int[capacity];
n = capacity;
count = 0;
}
/**
* 功能:插入头元素
* @param value 插入的元素值
* @return
*/
public Boolean insertHead(int value) {
data[count] = value;
count++;
return true;
}
/**
* 功能:数组插入元素
* @param index:数组下标索引
* @param value:要插入的元素值
* @return
*/
public boolean insert(int index, int value) {
//首先判断删除的索引值是否在数组索引范围内(边界问题)
if (index < 0 || index > count) return false;
//还要考虑到一种情况就是,如果你一直删除元素知道把元素全部删除完,数组长度为0,无法进行插入元素,对于这种情况就需要进行判断
if (count == n) {
return false;
}
//数组中数据从最后一依次向后移动,直到将用户指定索引元素空出空间
for (int i = count - 1; i >= index; --i) {
data[i+1] = data[i];
}
//将元素插入到数组中
data[index] = value;
//数组长度+1
++count;
return true;
}
/**
* 功能:下标随机访问
* @param index:用户传参下标
* @return
*/
public int find(int index) {
//索引判断,课程中所讲的边界问题(不在数组的范围内函数返回-1)
if (index < 0 || index > count ) return -1;
//否则返回该索引对应的数据
return data[index-1];
}
/**
* 功能:根据用户输入索引删除数组中数据。
* @param index
* @return
*/
public boolean delete(int index) {
//首先判断删除的索引值是否在数组索引范围内(边界问题)
if (index < 0 || index > count) return false;
//将删除元素的后边元素都向前依次移动
for(int i = index; i < count; ++i) {
data[i-1] = data[i];
}
//删除一个元素后,数组长度 -1
--count;
return true;
}
/**
* 通过for循环输出数组所有元素
*/
public void printAll() {
for (int i = 0; i < count; ++i) {
System.out.print(data[i] + " ");
}
System.out.println();
}
}
2、实现一个支持动态扩容的数组
/**
* 描述:功能:实现一个支持动态扩容的数组
* 方法一:System.arraycopy()
* 最好时间复杂度为O(1),最坏时间复杂度为 O(n),平均时间复杂度为 O(1).
*
* 方法二:ArrayList 实现自动扩容
*
* @author: JR
* @date: 2019/10/13 15:41
**/
public class DynamicDilatationArray {
private int count;
private int[] array;
private int num;
private int n;
//初始化
public DynamicDilatationArray(int number) {
count = 0;
array = new int[number];
num = number;
n = 1;
}
public static void main(String[] args) {
DynamicDilatationArray data = new DynamicDilatationArray(5);
data.insert(1);
data.insert(2);
data.insert(3);
data.insert(4);
data.insert(5);
data.insert(6);
data.insert(7);
data.insert(8);
data.insert(9);
data.insert(10);
data.insert(11);
data.insert(12);
data.print();
}
//插入数据
public void insert(int value) {
if(count>=num*n) {
//动态扩容
int[] newArray = new int[array.length*2];
//数组数据搬移
for(int j =0;j<array.length;j++) {
newArray[j] = array[j];
}
// System.arraycopy(array, 0, newArray, 0, array.length);
array = newArray;
//然后再插入数组
array[count] = value;
//下标移动
count++;
//扩容计数
n++;
}else {
//插入数据
array[count] = value;
//下标移动
count++;
}
}
//打印数组内容
public void print() {
for(int i=0;i<array.length;i++) {
System.out.print(array[i]+" ");
}
}
}
3、实现一个大小固定的有序数组,支持动态增删改操作
/**
* 描述:实现一个大小固定的有序数组,支持动态增删改操作
*
* @author: JR
* @date: 2019/10/13 15:49
**/
public class FixedOrderArray {
private int[] data;
private int n;
private int count;
public FixedOrderArray(int n) {
this.n = n;
count = 0;
data = new int[n];
}
public static void main(String[] args) {
FixedOrderArray array02 = new FixedOrderArray(5);
//插入
array02.insert(1);//0
array02.insert(3);//1
array02.insert(2);//2
array02.insert(1);//3
array02.insert(0);//4
//删除
array02.delete(0);
array02.print();
//查找数据
System.out.println();
System.out.print("查找该数据的下标为:"+array02.find(2));
}
/**
* 功能:插入
* @param value 插入的元素
* @return 返回 Boolean 值
*/
public Boolean insert(int value) {
//判断数组是否为空
if(data == null && data.length==0) {
return false;
}else {
//第一个数据直接插入数组
if(count == 0) {
data[count] = value;
count++;
return true;
}
//优化:先判断是否大于最后一个数据
if(value>=data[count-1]) {
data[count] = value;
count++;
return true;
}
//其他数据比较搬移
for (int i = 0; i < data.length; i++) {
if(value >= data[i]) {
for (int j = count-1; j >= i+1; j--) {
data[j+1] = data[j];
}
data[i+1] = value;
count++;
return true;
}else {
//插入数组第一个位置
for (int j = count-1; j >=0; j--) {
data[j+1] = data[j];
}
data[0] = value;
count++;
return true;
}
}
}
return false;
}
/**
* 功能:删除下标指定数据
* @param index 指定下标
* @return 返回删除的数据
*/
public int delete(int index) {
//判断删除元素的下标是否合理
if(index < 0 || index >= data.length) {
return -1;
}else {
for (int i = index; i < count-1; i++) {
data[i] = data[i+1];
}
}
count--;
return 0;
}
/**
* 功能: 查找
* @param value 要查找的元素
* @return 返回该下标
*/
public int find(int value) {
for (int i = 0; i <= count-1; i++) {
if(data[i]==value) {
return i;
}
}
return -1;
}
//打印数组内容
public void print() {
for(int i=0;i<count;i++) {
System.out.print(data[i]+" ");
}
}
}
4、两个有序数组的合并
/**
* 描述:实现两个有序数组合并为一个有序数组
*
* @author: JR
* @date: 2019/10/13 15:56
**/
public class MergeOrder {
static int[] a = new int[] {1,2,5,6};
static int[] b = new int[] {1,3,6,7,8};
static int[] c = new int[a.length+b.length];
public static void main(String[] args) {
merge(a,b,c);
for(int i = 0;i < c.length; i++) {
System.out.print(c[i]+" ");
}
}
/**
* 合并两个有序数组
* @param a 数组 a
* @param b 数组 b
*/
public static void merge(int[] a,int[] b,int[] c) {
if(a==null || b==null) {
return;
}else {
int i = 0;
int j = 0;
int n = 0;
while(i < a.length && j < b.length) {
if(a[i]<=b[j]) {
c[n] = a[i];
i++;
n++;
}else {
c[n] = b[j];
j++;
n++;
}
}
while(i >= a.length) {
if(j < b.length) {
c[n] = b[j];
j++;
n++;
}else {
break;
}
}
while(j >= b.length) {
if(i < a.length) {
c[n] = a[i];
i++;
n++;
}else {
break;
}
}
}
}
}