java数据结构与算法:顺序表SequentiaList

顺序表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());
    }
}
相关推荐
秋の花几秒前
【JAVA基础】Java集合基础
java·开发语言·windows
偷心编程几秒前
双向链表专题
数据结构
香菜大丸几秒前
链表的归并排序
数据结构·算法·链表
jrrz0828几秒前
LeetCode 热题100(七)【链表】(1)
数据结构·c++·算法·leetcode·链表
小松学前端3 分钟前
第六章 7.0 LinkList
java·开发语言·网络
Wx-bishekaifayuan10 分钟前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava
oliveira-time12 分钟前
golang学习2
算法
customer0814 分钟前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
全栈开发圈16 分钟前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫