用 java这种语言以测试驱动开发实现顺序表的构建、插入、删除、查找、反转
时间: 2023-10-19 13:18:46 浏览: 75
以下是使用 Java 语言进行测试驱动开发实现顺序表的构建、插入、删除、查找、反转的代码:
1. 先创建一个顺序表类:
```
public class ArrayList {
private int[] data;
private int size;
public ArrayList(int capacity) {
data = new int[capacity];
size = 0;
}
public ArrayList() {
this(10);
}
public int getSize() {
return size;
}
public int getCapacity() {
return data.length;
}
public boolean isEmpty() {
return size == 0;
}
public void add(int index, int e) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("Add failed. Index is illegal.");
}
if (size == data.length) {
resize(2 * data.length);
}
for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
size++;
}
public void addFirst(int e) {
add(0, e);
}
public void addLast(int e) {
add(size, e);
}
public int get(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Get failed. Index is illegal.");
}
return data[index];
}
public void set(int index, int e) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Set failed. Index is illegal.");
}
data[index] = e;
}
public boolean contains(int e) {
for (int i = 0; i < size; i++) {
if (data[i] == e) {
return true;
}
}
return false;
}
public int find(int e) {
for (int i = 0; i < size; i++) {
if (data[i] == e) {
return i;
}
}
return -1;
}
public int remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Remove failed. Index is illegal.");
}
int ret = data[index];
for (int i = index + 1; i < size; i++) {
data[i - 1] = data[i];
}
size--;
data[size] = 0;
if (size == data.length / 4 && data.length / 2 != 0) {
resize(data.length / 2);
}
return ret;
}
public int removeFirst() {
return remove(0);
}
public int removeLast() {
return remove(size - 1);
}
public void removeElement(int e) {
int index = find(e);
if (index != -1) {
remove(index);
}
}
public void reverse() {
for (int i = 0; i < size / 2; i++) {
int temp = data[i];
data[i] = data[size - i - 1];
data[size - i - 1] = temp;
}
}
private void resize(int newCapacity) {
int[] newData = new int[newCapacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
data = newData;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d, capacity = %d\n", size, data.length));
res.append("[");
for (int i = 0; i < size; i++) {
res.append(data[i]);
if (i != size - 1) {
res.append(", ");
}
}
res.append("]");
return res.toString();
}
}
```
2. 创建一个测试类:
```
public class ArrayListTest {
private ArrayList arrayList;
@Before
public void setUp() {
arrayList = new ArrayList();
}
@Test
public void testAdd() {
arrayList.add(0, 1);
arrayList.add(1, 2);
arrayList.add(2, 3);
assertEquals(3, arrayList.getSize());
assertEquals("[1, 2, 3]", arrayList.toString());
arrayList.addFirst(0);
arrayList.addLast(4);
assertEquals(5, arrayList.getSize());
assertEquals("[0, 1, 2, 3, 4]", arrayList.toString());
}
@Test
public void testGetAndSet() {
arrayList.add(0, 1);
arrayList.add(1, 2);
arrayList.add(2, 3);
assertEquals(2, arrayList.get(1));
arrayList.set(1, 4);
assertEquals(4, arrayList.get(1));
}
@Test
public void testContainsAndFind() {
arrayList.add(0, 1);
arrayList.add(1, 2);
arrayList.add(2, 3);
assertTrue(arrayList.contains(2));
assertEquals(1, arrayList.find(2));
assertFalse(arrayList.contains(4));
assertEquals(-1, arrayList.find(4));
}
@Test
public void testRemove() {
arrayList.add(0, 1);
arrayList.add(1, 2);
arrayList.add(2, 3);
int removed = arrayList.remove(1);
assertEquals(2, removed);
assertEquals(2, arrayList.getSize());
assertEquals("[1, 3]", arrayList.toString());
arrayList.removeFirst();
assertEquals(1, arrayList.getSize());
assertEquals("[3]", arrayList.toString());
arrayList.removeElement(3);
assertEquals(0, arrayList.getSize());
assertEquals("[]", arrayList.toString());
}
@Test
public void testReverse() {
arrayList.add(0, 1);
arrayList.add(1, 2);
arrayList.add(2, 3);
arrayList.reverse();
assertEquals("[3, 2, 1]", arrayList.toString());
}
}
```
3. 运行测试类,检查测试结果。
阅读全文