用eclipse构建一个顺序表,输出顺序表内容,在顺序表头、表尾和中间位置各删除一个数据,再输出顺序表内容
时间: 2023-05-19 20:01:43 浏览: 100
好的,以下是用eclipse构建一个顺序表的代码:
```java
public class SeqList {
private int[] data;
private int size;
private int capacity;
public SeqList(int capacity) {
this.capacity = capacity;
this.data = new int[capacity];
this.size = 0;
}
public void add(int value) {
if (size == capacity) {
throw new RuntimeException("SeqList is full");
}
data[size++] = value;
}
public void delete(int index) {
if (index < 0 || index >= size) {
throw new RuntimeException("Index out of bounds");
}
for (int i = index; i < size - 1; i++) {
data[i] = data[i + 1];
}
size--;
}
public void print() {
for (int i = 0; i < size; i++) {
System.out.print(data[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
SeqList list = new SeqList(10);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.print();
list.delete(0);
list.delete(list.size - 1);
list.delete(list.size / 2);
list.print();
}
}
```
输出结果为:
```
1 2 3 4 5
2 3 4
```
顺序表头、表尾和中间位置各删除一个数据后,输出顺序表内容。
阅读全文