java实现顺序结构线性列表的函数代码

所属分类: 软件编程 / java 阅读数: 52
收藏 0 赞 0 分享

废话不多说,直接上代码

复制代码 代码如下:

package com.ncu.list;

/**
 *
 * 顺序结构线性列表
 * 
 *
 */
public class SquenceList<T> {
    private int size; // 线性表的长度
    private Object[] listArray;
    private int currenSize = 0; // 当前线性表中的数据

    public SquenceList() {

    }

    public SquenceList(int size) {
        this.size = size;
        listArray = new Object[size];
    }

    public void arrayCopy(int index) {
        Object newArray[] = new Object[size];
        for (int i = 0; i < currenSize; i++) {
            if (i >= index) {
                newArray[i] = listArray[i + 1];
            } else {
                newArray[i] = listArray[i];
            }
        }
        listArray = newArray;
        newArray = null; // 释放资源
    }

    /**
     * 根据索引位置移除元素
     *
     * @param index
     */
    public void remove(int index) {
        index = index - 1;
        if (index < 0 || index > currenSize) {
            System.out.println("线性表索引越界");
        }
        if (currenSize == 0) {
            System.out.println("线性表为空");
        } else {
            currenSize--;
            arrayCopy(index);
            if (currenSize == 0) {
                listArray = null;
            }
        }
    }

    /**
     * 根据元素内容移除元素
     *
     * @param element
     */
    public void removeLocate(T element) {
        for (int i = 0; i < currenSize;) {
            if (element.equals(listArray[i])) {
                remove(i + 1);
            } else {
                i++;
            }
        }
    }

    /**
     * 从线性表尾段插入数据
     *
     * @param element
     */
    public void add(T element) {
        if (currenSize > size || currenSize < 0) {
            System.out.println("线性表索引越界");
        } else {
            listArray[currenSize] = element;
            currenSize++;
        }
    }

    private void insert(T element, int index) {
        index = index - 1;
        if (currenSize > size || currenSize < 0 || index < 0
                || index >= currenSize) {
            System.out.println("线性表索引越界");
        } else {
            Object newArray[] = new Object[size];
            for (int i = 0; i < currenSize; i++) {
                if (i >= index) {
                    newArray[index] = element;
                    newArray[i + 1] = listArray[i];
                } else {
                    newArray[i] = listArray[i];
                }

            }
            listArray = newArray;
            newArray = null;
            currenSize++;
        }
    }

    /**
     * 在指定索引位置插入数据
     *
     * @param element
     * @param index
     */
    public void add(T element, int index) {
        if (index == size) {
            add(element);
        } else {
            insert(element, index);
        }
    }

    /**
     * 删除线性表最后一个元素
     */
    public void delete() {
        if (isEmpty()) {
            System.out.println("线性表为空,不能删除");
        } else {
            listArray[currenSize - 1] = null;
            currenSize--;
        }
    }

    /**
     * 判读线性表是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        if (currenSize == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据索引找到相应的元素
     *
     * @param index
     * @return
     */
    public T get(int index) {
        T obj = null;
        if (isEmpty() || index > currenSize || index < 0) {
            System.out.println("线性表为空,不能删除");
        } else {
            obj = (T) listArray[index - 1];
        }

        return obj;
    }

    /**
     * 清空线性表
     */
    public void clear() {
        size = 0;
        currenSize = 0;
    }

    /**
     * 得到线性表当前的元素的个数
     *
     * @return
     */
    public int size() {
        return currenSize;
    }

    public void showList() {
        if (currenSize > 0) {
            for (int i = 0; i < currenSize; i++) {
                System.out.println(listArray[i]);

            }
        } else {
            System.out.println("线性表为空");
        }

        System.out.println("------------");
    }

    public static void main(String[] args) {
        SquenceList<Integer> list = new SquenceList<Integer>(10);
    }
}

更多精彩内容其他人还在看

Javaweb 鼠标移入移出表格颜色变化的实现

这篇文章主要介绍了Javaweb 鼠标移入移出表格颜色变化的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Java 实现图片压缩的两种方法

这篇文章主要介绍了Java 实现图片压缩的两种方法,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

据说这个是可以撸到2089年的idea2020.2(推荐)

这篇文章主要介绍了据说这个是可以撸到2089年的idea2020.2,本教程给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

一篇文章带你搞定SpringBoot不重启项目实现修改静态资源

这篇文章主要介绍了一篇文章带你搞定SpringBoot不重启项目实现修改静态资源,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

win10操作系统下重启电脑java环境变量失效

这篇文章主要介绍了win10操作系统下重启电脑java环境变量失效,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Java实现批量修改文件名和重命名的方法

这篇文章主要介绍了Java实现批量修改文件名和重命名的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

关于Java HashMap自动排序的简单剖析

这篇文章主要给大家介绍了关于Java HashMap自动排序的简单剖析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring中BeanFactory和ApplicationContext的作用和区别(推荐)

这篇文章主要介绍了Spring中BeanFactory和ApplicationContext的作用和区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java程序执行Cmd指令所遇问题记录及解决方案

这篇文章主要介绍了Java程序执行Cmd指令所遇问题记录,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

深入浅析jni中的java接口使用

这篇文章主要介绍了jni中的java接口使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多