前言~🥳🎉🎉🎉
线性表
顺序表
顺序表的模拟实现
成员属性
成员方法
1.构造方法
2,add——新增元素,默认在数组末尾新增
2.1, isFull——判断顺序表是否已满
2.2, expandCapacity——扩容
✅所以 add 方法的写法为:
public void add(int data) {
// 判满
if (isFull()) {
// 满了就扩容
expandCapacity();
}
this.array[this.useSize] = data;
this.useSize++;
}
最后记得,增加数据之后,**useSize 也要++**❗️❗️
3.add——在 pos 位置新增元素
3.1, judgeAddPos——判断 add 时pos 位置合法性
4.contains——判定是否包含某个元素
5, indexOf——查找某个元素对应的位置
6.get——获取 pos 位置的元素
6.1,judgePos——判断 pos 位置合法性
7, set——给 pos 位置的元素设为 value
8,remove——删除第一次出现的数据
9,size——获取顺序表长度
10.clear——清空顺序表
ArrayList的模拟实现总代码
import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
public class SeqList {
private int[] array;// 数组
private int capacity;// 容量
private int useSize;// 当前数组存放的数据的个数
public SeqList(int capacity) {
this.capacity = capacity;
this.array = new int[this.capacity];
}
public SeqList() {
this.capacity = 10;
this.array = new int[10];
//当无参数时,默认创建一个为容量为10的数组
}
// 新增元素,默认在数组最后新增
public void add(int data) {
// 判满
if (isFull()) {
// 满了就扩容
expandCapacity();
}
this.array[this.useSize] = data;
this.useSize++;
}
// 在 pos 位置新增元素
public void add(int pos, int data) {
try {
judgeAddPos(pos);
} catch (Exception e) {
e.printStackTrace();
return;
}
if (isFull()) {
expandCapacity();
}
for (int i = useSize - 1; i > pos - 1; i--) {
array[i + 1] = array[i];
}
array[pos] = data;
useSize++;
}
// 判定是否包含某个元素
public boolean contains(int toFind) {
for (int i = 0; i < useSize; i++) {
if (array[useSize] == toFind)
return true;
}
return false;
}
// 查找某个元素对应的位置
public int indexOf(int toFind) {
for (int i = 0; i < useSize; i++) {
if (array[i] == toFind)
return i;
}
try {
throw new NotFindPos("不存在该数" + toFind);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
// 获取 pos 位置的元素
public int get(int pos) {
try {
judgePos(pos);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
return array[pos];
}
// 给 pos 位置的元素设为 value
public void set(int pos, int value) {
try{
judgePos(pos);
}catch(Exception e) {
e.printStackTrace();
return;
}
array[pos]=value;
}
//删除第一次出现的关键字key
public void remove(int toRemove) {
int pos = indexOf(toRemove);
if (pos == -1) {
// 找不到的情况
System.out.println("不存在该数据");
}else {
// 注意这里的循环条件
for (int i = pos; i < this.useSize - 1; i++) {
this.array[i] = this.array[i + 1];
}
this.useSize--;
}
}
// 获取顺序表长度
public int size() {
return useSize;
}
// 清空顺序表
public void clear() {
useSize=0;
}
// 打印顺序表,注意:ArrayList 没有这个方法,为了方便看测试结果给出的
public void display() {
for (int i = 0; i <useSize ; i++) {
System.out.print(array[i]+" ");
}
System.out.println();
}
public boolean isFull() {
return this.capacity == this.useSize;
}
public void expandCapacity(){
this.array =Arrays.copyOf(this.array,capacity*2);
capacity*=2;
}
public void judgeAddPos(int pos){
if(pos<0||pos>useSize){
throw new ArrayListIndexOutOfException("pos位置不合法");
}
}
public void judgePos(int pos){
if(pos<0||pos>useSize-1){
throw new ArrayListIndexOutOfException("pos位置不合法");
}
}}
class ArrayListIndexOutOfException extends RuntimeException{
public ArrayListIndexOutOfException(String message) {
super(message);
}
}
class NotFindPos extends RuntimeException{
public NotFindPos(String message){
super(message);
}
}