MyArrayList.java
public class MyArrayList {
public int[] elem ;
public int usedSize;
public static final int DEFAULT_SIZE = 10;
public MyArrayList() {
this.elem = new int[DEFAULT_SIZE];
this.usedSize = 0;
}
public boolean isFull() {
if(usedSize == this.elem.length) {
return true;
}
return false;
}
public boolean isEmpty() {
if(this.usedSize == 0) {
return true;
}
return false;
}
public void display() {
for (int i = 0; i < usedSize ; i++ ) {
System.out.print(elem[i]+" ");
}
System.out.println();
}
public void add(int pos,int data) {
if(isFull()) {
throw new RuntimeException("顺序表是满的!");
}
if (pos<0 || pos > this.usedSize) {
throw new ArrayIndexOutOfBoundsException("pos位置不合法!");
}
for (int i = usedSize - 1; i >= pos ; i-- ) {
this.elem[ i + 1 ] = this.elem[ i ];
}
this.elem[ pos ] = data;
this.usedSize++;
}
public boolean contains(int toFind) {
if(isEmpty()) {
throw new RuntimeException("顺序表为空!");
}
for (int i = 0; i < this.usedSize ; i++ ) {
if(this.elem[ i ] == toFind) {
return true;
}
}
return false;
}
public int search(int toFind) {
if(isEmpty()) {
throw new RuntimeException("顺序表为空!");
}
for (int i = 0; i < this.usedSize ; i++ ) {
if(this.elem[ i ] == toFind) {
return i;
}
}
return -1;
}
public int getPos(int pos) {
if(isEmpty()) {
throw new RuntimeException("顺序表为空!");
}
if (pos<0 || pos > this.usedSize) {
throw new ArrayIndexOutOfBoundsException("pos位置不合法!");
}
return this.elem[ pos ];
}
public void setPos(int pos, int value) {
if (pos<0 || pos > this.usedSize) {
throw new ArrayIndexOutOfBoundsException("pos位置不合法!");
}
this.elem[pos] = value;
}
public int size() {
int l = elem.length;
return l;
}
public void clear() {
this.usedSize=0;
}
public void remove(int key) {
if( search(key) != -1) {
for( int i=search(key); i < this.usedSize-1;i++ ) {
this.elem[i]= this.elem[i+1];
}
this.usedSize--;
}
}
public void grow() {
this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
}
}
TestDemo.java
public class TestDemo {
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList();
{myArrayList.add(0,10);
myArrayList.add(1,20);
myArrayList.add(2,30);
myArrayList.add(3,40);
myArrayList.add(4,50);
myArrayList.add(5,60);
myArrayList.add(6,70);
myArrayList.add(7,80);
myArrayList.add(8,90);
myArrayList.add(9,100);}
System.out.println("==================================");
System.out.println("原始顺序表:");
myArrayList.display();
System.out.println("==================================");
System.out.println("扩容");
myArrayList.grow();
myArrayList.add(10,101);
myArrayList.display();
System.out.println("==================================");
System.out.println("是否包含 3 :"+myArrayList.contains(3));
System.out.println("==================================");
System.out.println("在表里寻找 3 :"+myArrayList.search(3));
System.out.println("==================================");
System.out.println("找到 表中 2 位置的数据:"+myArrayList.getPos(2));
System.out.println("==================================");
System.out.println("顺序表长度:"+myArrayList.size());
System.out.println("==================================");
System.out.println("更改 3 处的元素为 100");
myArrayList.setPos(3,100);
myArrayList.display();
System.out.println("==================================");
System.out.println("删除");
myArrayList.remove(100);
myArrayList.display();
System.out.println("==================================");
System.out.println("清除");
myArrayList.clear();
myArrayList.display();
System.out.println("==================================");
}
}
运行结果
=================================
原始顺序表:
10 20 30 40 50 60 70 80 90 100
==================================
扩容
10 20 30 40 50 60 70 80 90 100 101
==================================
是否包含 3 :false
==================================
在表里寻找 3 :-1
==================================
找到 表中 2 位置的数据:30
==================================
顺序表长度:20
==================================
更改 3 处的元素为 100
10 20 30 100 50 60 70 80 90 100 101
==================================
删除
10 20 30 50 60 70 80 90 100 101
==================================
清除
==================================