顺序表SequentiaList
创建List接口并定义方法
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();
}
实现方法
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);
}
}
测试
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() {
List<String> list = new SequentiaList<>(5);
assertTrue(list.size() == 0);
list.add("Java");
assertTrue(list.size() == 1);
}
@Test
public void testIsEmpty() {
List<String> list = new SequentiaList<>(5);
assertTrue(list.isEmpty());
list.add("Java");
assertFalse(list.isEmpty());
}
@Test
public void testContains() {
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() {
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() {
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() {
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() {
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() {
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() {
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() {
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() {
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());
}
}