编写一个java语言程序,实现顺序表各种基本运算
时间: 2023-04-02 14:02:36 浏览: 67
非常感谢您的提问。作为一个 AI 语言模型,我可以回答您的问题。以下是一个简单的 Java 语言程序,实现顺序表的基本运算:
```
public class SeqList {
private int[] data;
private int size;
private int capacity;
public SeqList(int capacity) {
this.data = new int[capacity];
this.size = ;
this.capacity = capacity;
}
public boolean isEmpty() {
return size == ;
}
public boolean isFull() {
return size == capacity;
}
public int size() {
return size;
}
public void clear() {
size = ;
}
public int get(int index) {
if (index < || index >= size) {
throw new IndexOutOfBoundsException();
}
return data[index];
}
public void set(int index, int value) {
if (index < || index >= size) {
throw new IndexOutOfBoundsException();
}
data[index] = value;
}
public void insert(int index, int value) {
if (index < || index > size) {
throw new IndexOutOfBoundsException();
}
if (isFull()) {
throw new RuntimeException("SeqList is full");
}
for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = value;
size++;
}
public void remove(int index) {
if (index < || index >= size) {
throw new IndexOutOfBoundsException();
}
for (int i = index; i < size - 1; i++) {
data[i] = data[i + 1];
}
size--;
}
public int indexOf(int value) {
for (int i = ; i < size; i++) {
if (data[i] == value) {
return i;
}
}
return -1;
}
public void display() {
for (int i = ; i < size; i++) {
System.out.print(data[i] + " ");
}
System.out.println();
}
}
```
这个程序实现了顺序表的基本运算,包括创建、判断是否为空、判断是否已满、获取元素、设置元素、插入元素、删除元素、查找元素位置和显示元素等操作。希望能对您有所帮助。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)