【前言】
arraylist是java集合框架中list接口的动态数组实现,可以方便的存储和操作数据。它提供了一系列方法,便于我们进行增删查改,这篇文章是进行arraylist的一个自我实现,让我们从底层更深度地理解arraylist(顺序表)
一、 定义arraylist类
arr: 创建一个整型数组,用来存储数据;usedsize: 有效数据,也就是真实的元素个数;default_capacity:默认数组容量myarraylist():构造方法,初始化数组长度为默认的容量
public class myarraylist implements ilist{
private int[] arr;//存储数据
private int usedsize;//有效数据个数
public static final int default_capacity = 10;//默认数组容量
public myarraylist(){
this.arr = new int[default_capacity];
}
二、常见方法实现
arraylist的常见方法如下:

1.add(int data):新增元素,添加在数组后面
public void add(int data) {
//判断顺序表是否满了
if (isfull()) {
//扩容
grow();
}
//存储
this.arr[this.usedsize] = data;
this.usedsize++;
}
public boolean isfull(){
return this.usedsize == this.arr.length;
}
public void grow(){
this.arr = arrays.copyof(this.arr,2*this.arr.length);
}
添加元素时,要先检查数组是否已满(
isfull()通过比较数组的长度和元素个数),如果满了,就要进行扩容grow(),这里是copy两倍的数组容量,然后再进行存储
2.add(int pos, int data):再pos位置添加元素
public void add(int pos, int data) {
//检查是不是满的
if(isfull()){
grow();
}
checkpos(pos,"add方法执行的时候pos位置不合法");
//判断是不是放在usedsize位置
if(pos == usedsize){
arr[pos] = data;
usedsize++;
return;
}
//如果放在前面,就要移动数据
for (int i = usedsize-1; i >= pos; i++) {
arr[i+1] = arr[i];
}
arr[pos] = data;
usedsize++;
}
private void checkposadd(int pos,string msg) {
if (pos < 0 || pos > usedsize) {
throw new posillegalityexception(msg);
}
}

还是先要检查容量是否已满
isfull(),满了就要进行扩容grow(),然后添加时,要先检查pos位置是否合法,如上图,pos < 0 || pos > usedsize就是不合法的,在这个范围则会抛出我们自定义的posillegalityexception异常,正常情况下进行添加:将pos位置及后面的元素统统向后移动一个位置,为要添加的元素腾出位置,然后有效数据usedsize++
3.contains(int tofind):判断是否包含某个元素
public boolean contains(int tofind) {
for (int i = 0; i < this.usedsize; i++) {
if(arr[i] == tofind){
return true;
}
}
return false;
}
使用for循环遍历整个数组,if语句判断当前元素是否是你要找的元素tofind,是返回true,不是返回false
4.indexof(int tofind):查找某个元素对应的位置
public int indexof(int tofind) {
for (int i = 0; i < this.usedsize; i++) {
if(arr[i] == tofind){
return i;
}
}
return -1;
}
同理:使用for循环遍历整个数组,if语句判断当前元素是否是你要找的元素tofind,是返回对应元素的下标i,不是返回-1
5.get(int pos):获取pos位置的元素
public int get(int pos) {
if(isempty()){
throw new emptylistexception("当前顺序表为空");
}
checkpos(pos,"get方法的pos位置不合法");
return arr[pos];
}
private void checkpos(int pos,string msg){
if (pos<0 || pos >= usedsize){
throw new posillegalityexception(msg);
}
}
public boolean isempty(){
return usedsize == 0;
}
获取元素,先检查pos位置是否为空
isempty(),如果为空,抛出我们的自定义异常emptylistexception,然后检查pos位置是否合法pos<0 || pos >= usedsize这个范围就是不合法的,抛出我们的自定义异常posillegalityexception,最后就可以返回pos位置的元素
6.set(int pos, int value):给pos位置的元素设为value
public void set(int pos, int value) {
if(isempty()){
throw new emptylistexception("当前顺序表为空");
}
checkpos(pos,"set方法执行的时候pos位置不合法"+pos);
arr[pos] = value;
}
替换时要先检查是否为空
isempty(),为空抛出自定义异常emptylistexception,不为空检查pos位置是否合法,最后将给定的值替换指定的数组元素
7.remove(int toremove):删除第⼀次出现的关键字key
public void remove(int toremove) {
if(isempty()){
throw new emptylistexception("当前顺序表为空");
}
//查找要删除数据的下标
int index = indexof(toremove);
if(index == -1){
system.out.println("没有你要删除的数据");
return;
}
for (int i = index; i < usedsize-1; i++) {
arr[i] = arr[i+1];
}
usedsize--;
}
//arr[usedsize] = null;如果是引用类型,需要手动置空

还是先检查是否为空,然后可以通过
indexof(toremove)查找要删除的元素,如果index == -1,则没有你要删除的元素,如果有,进行覆盖删除,让后面的元素逐一向前移动一个位置直到你要,直到覆盖掉你要删除的元素,最后将有效元素usedsize–
8.size():获取顺序表⻓度
public int size() {
return this.usedsize;
}
9.clear():清空顺序表
public void clear(){
for (int i = 0; i < this.usedsize; i++) {
arr[i] = 0;
}
usedsize = 0;
}
三、总结
这篇文章只是从深度的实现了一下arraylist提供的一系列方法,以便于更好地去理解和使用arraylist,让我们认识到了,数据结构的学习和使用,要考虑到所有方面,对我们的逻辑思维能力要求很高,同样也会提高我们这方面的能力,相关的其他内容后面会详细道来
到此这篇关于java arraylist底层方法的自我实现的文章就介绍到这了,更多相关java arraylist底层方法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论