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

 更新时间:2013年10月24日 10:09:44   作者:  
java实现顺序结构线性列表的函数代码。需要的朋友可以过来参考下,希望对大家有所帮助

废话不多说,直接上代码

复制代码 代码如下:

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);
    }
}

相关文章

  • MyBatis学习教程(六)-调用存储过程

    MyBatis学习教程(六)-调用存储过程

    这篇文章主要介绍了MyBatis学习教程(六)-调用存储过程的相关资料,非常不错,具有参考借鉴价值,感兴趣的朋友一起看下吧
    2016-05-05
  • Java数据结构之单链表详解

    Java数据结构之单链表详解

    在之前的学习中,我们主要了解了很多 Java 的 基本语法,但是在之后的 Java学习中,了解基础数据结构的知识非常重要,数据结构的思想可以帮助我们更加清晰明白的了解 Java 的解题思路等等.今天我们就来开始学习实现一个Java基础的单链表,需要的朋友可以参考下
    2021-05-05
  • java加密MD5实现及密码验证代码实例

    java加密MD5实现及密码验证代码实例

    这篇文章主要介绍了java加密MD5实现及密码验证代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • SpringMVC的Body参数拦截的问题

    SpringMVC的Body参数拦截的问题

    SpringMVC对出参和入参有非常友好的拓展支持,方便你对数据的输入和输出有更大的执行权,我们如何通过SpringMVC定义的结果做一系列处理呢,需要的朋友可以参考下
    2018-06-06
  • IDEA利用自带Axis工具和wsdl文件反向生成服务端客户端代码图文详解

    IDEA利用自带Axis工具和wsdl文件反向生成服务端客户端代码图文详解

    这篇文章主要介绍了IDEA利用自带Axis工具和wsdl文件反向生成服务端客户端代码详细流程,在这里小编使用的是idea2021.1最新开发工具,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友可以参考下
    2021-05-05
  • ZooKeeper入门教程一简介与核心概念

    ZooKeeper入门教程一简介与核心概念

    本文是ZooKeeper入门系列教程,涵盖ZooKeeper核心内容,通过实例和大量图表,结合实战,帮助学习者理解和运用,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2022-01-01
  • Quarkus改造Pmml模型项目异常记录及解决处理

    Quarkus改造Pmml模型项目异常记录及解决处理

    这篇文章主要为大家介绍了Quarkus改造Pmml模型项目是遇到的异常记录以及解决方法,有需要的同学可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-02-02
  • spring-security关于hasRole的坑及解决

    spring-security关于hasRole的坑及解决

    这篇文章主要介绍了spring-security关于hasRole的坑及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • JAVA8之函数式编程Function接口用法

    JAVA8之函数式编程Function接口用法

    这篇文章主要介绍了JAVA8之函数式编程Function接口用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • Java语言实现简单FTP软件 FTP本地文件管理模块实现(9)

    Java语言实现简单FTP软件 FTP本地文件管理模块实现(9)

    这篇文章主要为大家详细介绍了Java语言实现简单FTP软件,FTP本地文件管理模块的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04

最新评论