【数据结构与算法】ArrayList的模拟实现
⛵⛵hello,进来的小伙伴们,你们好呐!
🚀🚀系列专栏:【数据结构与算法】
🍼🍼作者简介:一名大三在读的科班Java小白,星夜漫长,你我同行!
🌯🌯本篇内容:初始ArrayList与顺序表,简单了解ArrayList的基本用法,入门顺序表!
✈️✈️码云存放仓库gitee:Java初阶数据结构代码存放!
🍅🍅目录
一、ArrayList的模拟实现
2、类MyArrayList
3、打印顺序表
4、新增元素,默认在数组后面新增
5、在pos位置新增元素
6、判定是否包含某个元素
7、查找某个元素对应的位置
8、获取pos位置的元素
8、给pos位置的元素改为value
9、删除第一次出现的元素key
10、获取顺序表长度
11、清空顺序表
一、ArrayList的模拟实现
接着上上篇……博客,我们已经介绍了ArrayList的基本使用方法,也给大家演示了ArrayList的遍历方法,那么最近博主是一直在更新数据结构的博客,所以才想起来还有ArrayList的模拟实现没有给大家写,哈哈,借助国庆节的活动,今天就给大家带来的就是ArrayList的模拟实现,忘记的同学可以去翻翻该专栏的ArrayList与顺序表这篇博客!
1、首先在我们的idea里面新建一个包Arraylist,我希望大家可以这样把同一类型的知识点写在一个包里面,这样非常方便,也符合了我们java中包的定义使用。
主函数代码:
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList();
myArrayList.add(1);
myArrayList.add(2);
myArrayList.add(3);
myArrayList.display();
myArrayList.display();
2、类MyArrayList
private int[] elem;//数组
private int usedSize;//记录有效的数据的个数
private static final int DEFAULT_SIZE = 10;
public MyArrayList() {
this.elem = new int[DEFAULT_SIZE];
}
3、打印顺序表
public void display() {
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i]+" ");
}
System.out.println();
}
运行结果:
4、新增元素,默认在数组后面新增
public void add(int data) {
//1. 检查当前的顺序表是不是满了?
if(isFull()) {
//2. 如果满了 就要进行扩容
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
//3.
this.elem[this.usedSize] = data;
//4.
this.usedSize++;
}
public boolean isFull() {
/*if(size() >= this.elem.length){
return true;
}
return false;*/
return size() >= this.elem.length;
}
5、在pos位置新增元素
/**
* 在 pos 位置新增元素
* 如果pos下标不合法,那么就会抛出一个 PosWrongfulException
*/
public void add(int pos, int data) throws PosWrongfulException{
if(isFull()) {
System.out.println("满了");
this.elem =
Arrays.copyOf(this.elem,2*this.elem.length);
}
if(pos < 0 || pos > this.usedSize) {
System.out.println("pos位置不合法!");
throw new PosWrongfulException("pos位置不合法");
}
//pos一定是合法的
//1. 开始挪动数据
for (int i = this.usedSize-1; i >= pos ; i--) {
this.elem[i+1] = this.elem[i];
}
//2.插入数据
this.elem[pos] = data;
//3. usedSize++
this.usedSize++;
}
自定义一个异常:PosWrongfulException
public class PosWrongfulException extends RuntimeException{
public PosWrongfulException() {
}
public PosWrongfulException(String message) {
super(message);
}
}
自定义异常: EmptyException
public class EmptyException extends RuntimeException{
public EmptyException() {
}
public EmptyException(String message) {
super(message);
}
}
6、判定是否包含某个元素
public boolean contains(int toFind) {
for (int i = 0; i < this.size(); i++) {
if(this.elem[i] == toFind) {
return true;
}
}
return false;
}
7、查找某个元素对应的位置
public int indexOf(int toFind) {
for (int i = 0; i < this.size(); i++) {
if(this.elem[i] == toFind) {
return i;
}
}
return -1;
}
8、获取pos位置的元素
public int get(int pos) {
if(isEmpty()) {
throw new EmptyException("当前顺序表为空!");
}
if(pos < 0 || pos >= this.usedSize) {
throw new PosWrongfulException("get获取元素的时候,pos不合法异常!");
}
return this.elem[pos];
}
public boolean isEmpty() {
return size() == 0;
}
8、给pos位置的元素改为value
public void set(int pos, int value) {
if(isEmpty()) {
throw new EmptyException("当前顺序表为空!");
}
if(pos < 0 || pos >= this.usedSize) {
throw new PosWrongfulException("set获取元素的时候,pos不合法异常!");
}
this.elem[pos] = value;
}
9、删除第一次出现的元素key
public void remove(int key) {
if(isEmpty()) {
throw new EmptyException("顺序表为空!");
}
int index = this.indexOf(key);
if(index == -1) {
System.out.println("没有这个数字");
return;
}
for (int i = index; i < size()-1; i++) {
this.elem[i] = this.elem[i+1];
}
this.usedSize--;
}
10、获取顺序表长度
public int size() {
return this.usedSize;
}
11、清空顺序表
public void clear() {
/*for (int i = 0; i < size(); i++) {
this.elem[i] = null;
}
this.usedSize=0;*/
this.usedSize=0;
}
🍱🍱OK,那么以上就是ArrayList的个人实现代码了,当然对于顺序表还是比较简单的结构,大家在学习数据结构的过程中一定是先深入理解,从底层出发,个人实现何种顺序表,这样的学习效果才是最棒的,那么最后祝大家"国庆快乐鸭!"🍺🍺