顺序表SequentiaList
创建List接口并定义方法
java
package com.lhs;
import java.util.Objects;
public interface List<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
boolean add(E e);
E get(int index);
E set(int index, E e);
E remove(int index);
void addFirst(E e);
void addLast(E e);
E removeFirst();
E removeLast();
}
实现方法
java
package com.lhs;
import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;
public class SequentiaList<E> implements List<E> {
private static final int DEFAULT_CAPACITY = 10;
private int size;
private Object[] elementData;
public SequentiaList(int capacity) {
elementData = new Object[capacity];
size = 0;
}
public SequentiaList() {
this(DEFAULT_CAPACITY);
}
// 返回列表中元素的数量
@Override
public int size() {
return size;
}
// 判断列表是否为空
@Override
public boolean isEmpty() {
return size == 0;
}
// 判断列表中是否包含某个元素
@Override
public boolean contains(Object o) {
if(size == 0){
return false;
}
for (int i = 0; i < size; i++) {
if(elementData[i] == o)
return true;
}
return false;
}
// 向列表中添加一个元素
@Override
public boolean add(E e) {
if(size == elementData.length){
throw new IndexOutOfBoundsException("list is full");
}
elementData[size] = e;
size++;
return true;
}
// 获取列表中指定索引位置的元素
@Override
public E get(int index) {
if(index >= size){
throw new IndexOutOfBoundsException(index + " is out of bounds");
}
return (E) elementData[index];
}
// 设置列表中指定索引位置的元素
@Override
public E set(int index, E e) {
if(index >= size){
throw new IndexOutOfBoundsException(index + " is out of bounds");
}
E oldVal = (E) elementData[index];
elementData[index] = e;
return oldVal;
}
// 移除列表中指定索引位置的元素
@Override
public E remove(int index) {
if(index >= size){
throw new IndexOutOfBoundsException(index + " is out of bounds");
}
E oldVal = (E) elementData[index];
for (int i = index ;i < size - 1;i++){
elementData[i] = elementData[i+1];
}
size--;
return oldVal;
}
// 向列表开头添加一个元素
@Override
public void addFirst(E e) {
if(size == elementData.length){
throw new IndexOutOfBoundsException("list is full");
}
for (int i = size ;i > 0;i--){
elementData[i] = elementData[i-1];
}
elementData[0] = e;
size++;
}
// 向列表结尾添加一个元素
@Override
public void addLast(E e) {
if(size == elementData.length){
throw new IndexOutOfBoundsException("list is full");
}
elementData[size] = e;
size++;
}
// 移除列表开头的一个元素
@Override
public E removeFirst() {
return remove(0);
}
// 移除列表结尾的一个元素
@Override
public E removeLast() {
return remove(size-1);
}
}
测试
java
package com.lhs;
import org.junit.Test;
import static junit.framework.TestCase.*;
import static org.junit.Assert.assertThrows;
public class SequentiaListTest {
@Test
public void testSize() {
// 实例化SequentialList
List<String> list = new SequentiaList<>(5);
assertTrue(list.size() == 0);
list.add("Java");
assertTrue(list.size() == 1);
}
@Test
public void testIsEmpty() {
// 实例化SequentialList
List<String> list = new SequentiaList<>(5);
assertTrue(list.isEmpty());
list.add("Java");
assertFalse(list.isEmpty());
}
@Test
public void testContains() {
// 实例化SequentialList
List<String> list = new SequentiaList<>(5);
list.add("Java");
list.add("C++");
list.add("C");
list.add("Python");
list.add("TypeScript");
// 判断存在
assertTrue(list.contains("Java"));
// 判断不存在
assertFalse(list.contains("Java++"));
}
@Test
public void testAdd() {
// 实例化SequentialList
List<Integer> list = new SequentiaList<>(5);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
Throwable excpetion = assertThrows(IndexOutOfBoundsException.class, () -> {
list.add(6); // 抛异常
});
assertEquals("list is full", excpetion.getMessage());
}
@Test
public void testGet() {
// 实例化SequentialList
List<String> list = new SequentiaList<>(5);
list.add("Java");
list.add("C++");
list.add("C");
// 判断存在
assertEquals("C++", list.get(1));
// 判断不存在
assertNull(list.get(4));
}
@Test
public void testSet() {
// 实例化SequentialList
List<String> list = new SequentiaList<>(5);
list.add("Java");
list.add("C++");
list.add("C");
// 判断存在
assertEquals("C", list.set(2, "Python"));
// 判断不存在
assertEquals(null, list.set(4, "TypeScript"));
}
@Test
public void testRemove() {
// 实例化SequentialList
List<String> list = new SequentiaList<>(5);
list.add("Java");
list.add("C++");
list.add("C");
// 判断存在
assertEquals("C", list.remove(2));
// 判断不存在
int index = 6;
Throwable excpetion = assertThrows(IndexOutOfBoundsException.class, () -> {
list.remove(index); // 抛异常
});
assertEquals(index + " is out of bounds", excpetion.getMessage());
}
@Test
public void testAddFirst() {
// 实例化SequentialList
List<String> list = new SequentiaList<>(5);
list.addFirst("Java");
list.addFirst("C++");
list.addFirst("C");
// 判断存在
assertEquals("C", list.get(0));
assertEquals("C++", list.get(1));
assertEquals("Java", list.get(2));
}
@Test
public void testAddLast() {
// 实例化SequentialList
List<String> list =new SequentiaList<>(5);
list.addLast("Java");
list.addLast("C++");
list.addLast("C");
// 判断存在
assertEquals("Java", list.get(0));
assertEquals("C++", list.get(1));
assertEquals("C", list.get(2));
}
@Test
public void testRemoveFirst() {
// 实例化SequentialList
List<String> list = new SequentiaList<>(5);
list.add("Java");
list.add("C++");
list.add("C");
// 判断存在
assertEquals("Java", list.removeFirst());
assertEquals("C++", list.removeFirst());
assertEquals("C", list.removeFirst());
}
@Test
public void testRemoveLast() {
// 实例化SequentialList
List<String> list = new SequentiaList<>(5);
list.add("Java");
list.add("C++");
list.add("C");
// 判断存在
assertEquals("C", list.removeLast());
assertEquals("C++", list.removeLast());
assertEquals("Java", list.removeLast());
}
}