public class CreatSeqList {
//创建初始变量
public int[] element;
public int length;
public CreatSeqList() {
this.element = new int[];
}
//打印顺序表
public void display() {
for (int i = 0; i < length; i++) {
System.out.print(element[i]);
}
}
//添加元素:首先判断是否需要增容,然后判断下标是否合法,最后插入元素,可以在第一个元素前插入
private boolean isFull() {
if (this.element.length == this.length) {
return true;
}
return false;
}
public void add(int positon, int data) {
if (isFull) {
this.element = Arrays.copyOf(this.element, this element.length * 2);
}
if (position < 0 || position >= this.element.length) {
System.out.println("false position");
}
for (int i = this.length - 1; i >= position; i--) {
this.element[i + 1] = this.element[i];
}
element[position] = data;
this.length++;
}
//判断是否包含元素
public boolean contains(int data){
for(int i = 0; i < element.length; i++){
if(data == element[i]){
return true;
}
}
return false;
}
//同理,查找元素下标
public int search(int data){
for(int i = 0; i < element.length; i++){
if(data == element[i]){
return i;
}
}
return 0;
}
//获取position上元素
public int getPos(int position){
if(position < 0 || position >= element.length){
return 0;
}
return element[position];
}
//更新指定位置元素
public void setPos(int position, int data){
if(position < 0 || position >= element.length){
System.out.println("false");
}
this.element[position] = data;
}
//获取顺序表长度
public int size(){
return this.element.length;
}
}
可参考上一篇https://blog.csdn.net/m0_73906351/article/details/128587301