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());
    }
}
相关推荐
程序员 小柴5 分钟前
docker的与使用
java·docker·eureka
ゞ 正在缓冲99%…10 分钟前
leetcode76.最小覆盖子串
java·算法·leetcode·字符串·双指针·滑动窗口
xuanjiong11 分钟前
纯个人整理,蓝桥杯使用的算法模板day2(0-1背包问题),手打个人理解注释,超全面,且均已验证成功(附带详细手写“模拟流程图”,全网首个
算法·蓝桥杯·动态规划
Seven9724 分钟前
【Guava】并发编程ListenableFuture&Service
java
WannaRunning25 分钟前
浅谈Tomcat数据源连接池
java·oracle·tomcat
惊鸿.Jh30 分钟前
【滑动窗口】3254. 长度为 K 的子数组的能量值 I
数据结构·算法·leetcode
明灯L31 分钟前
《函数基础与内存机制深度剖析:从 return 语句到各类经典编程题详解》
经验分享·python·算法·链表·经典例题
forestsea32 分钟前
使用 Spring Boot 和 GraalVM 的原生镜像
java·spring boot·spring native·原生映像
逸狼35 分钟前
【JavaEE进阶】Spring AOP入门
java·java-ee
碳基学AI36 分钟前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义免费下载方法
大数据·人工智能·python·gpt·算法·语言模型·集成学习