java实现顺序结构线性列表的函数代码
发布时间:2023-01-28 13:49:31
废话不多说,直接上代码
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真正强大的地方在于SQL映射语句,这也是它的魅力所在。相对于它强大的功能,SQL映射文件的配置却非常简单,我上篇文章语句讲
- 一、使用无参构造方法创建二、使用静态工厂创建三、使用实例工厂创建来源:https://www.cnblogs.com/jock766/p/1
- java 二分法算法的实例1、前提:二分查找的前提是需要查找的数组必须是已排序的,我们这里的实现默认为升序2、原理:将数组分为三部分,依次是
- 当我们需要与 NIO Channel 进行交互时, 我们就需要使用到 NIO Buffer, 即数据从 Buffer读取到 Channel
- SpringBoot 工厂模式自动注入Map一、建立工厂类public interface AnimalFactory { S
- 摘要在生产环境下,我们需要关闭swagger配置,避免暴露接口的这种危险行为。方法禁用方法1:使用注解 @Value() 推荐使用packa
- 首先,你可能会见到如下提示:File encoding is disabled because .properties file (see
- Spring中BeanFactory FactoryBean和ObjectFactory的三种的区别引言关于FactoryBean 和 Be
- 引言在前两篇文章中,我们了解了ReentrantLock内部公平锁和非公平锁的实现原理,可以知道其底层基于AQS,使用双向链表实现,同时在线
- 1.接口中的默认方法和静态方法Java 8中允许接口中包含具有具体实现的方法,该方法称为 “默认方法” ,默认方法使用 default 关键
- 第一部分: 使用idea 打包工程jar 1.准备好一份 开发好的 可执行的 含有main方法的&nbs
- 介绍备忘录模式(Memento Pattern)是一种行为型设计模式,它允许在不破坏封装性的前提下,捕获并保存一个对象的内部状态,并在之后可
- 在hibernate5中,有了一些新的变动: 新引导 APISpatial/GIS 支持Java 8 支持扩展 AUTO
- 你知道String、StringBuilder、Stringbuffer的区别吗?当你创建字符串的时候,有考虑过该使用哪个吗?别急,这篇文章
- 本文实例为大家分享了java登录界面的具体实现代码,供大家参考,具体内容如下1. Login.javapackage wzb;import
- CSDN 的小伙伴们,大家好,我是沉默王二。重写(Overriding)算是 Java 中一个非常重要的概念,理解重写到底是什么对每个 Ja
- 本文实例为大家分享了Java实现简单ATM机功能的具体代码,供大家参考,具体内容如下项目介绍基于大家使用银行卡在ATM机取款操作,进行相对应
- 【漏洞通告】2月19日,NVD发布安全通告披露了jackson-databind由JNDI注入导致的远程代码执行漏洞(CVE-2020-88
- 简介optional类是java8中引入的针对NPE问题的一种优美处理方式,源码作者也希望以此替代null。历史1965年,英国一位名为To
- java 图片转base64字符串、base64字符串转图片,具体内容如下1. 图片转base64字符串: /** * base