顺序表封装实现容器 学习数据结构心血来潮篇

今天学习顺序表结构心血来潮。

请大家不要嘲笑我, 还在学习阶段。功能不可能有ArrayList那样强大。

哪里逻辑不对请多指正谢谢!!!!!!!

实现的方法:

public void add(E e, Integer index)
public void add(E e)
public E remove(Integer index)
public E remove(Object obj)
public E set(E e, Integer index)
public E get(Integer index)
public Integer containt(Object obj)
public boolean clear()
public int getSize()

 源码

package com.LZ;
/**
 * 
 * @author LiuZhe 2019/3/24
 * 
 */
@SuppressWarnings("unchecked")
public class MyList<E> {
	
	//默认数组的大小
	private int DEFAULCAPACITY = 10;

	//记录MyList对象的元素个数
	private int size = 0;
	
	private E[] array = null;
	//MyList的默认构造函数 容量为10
	public MyList() {
		array = (E[]) new Object[DEFAULCAPACITY];
	}
	
	public MyList(int capacity) {
		array = (E[]) new Object[capacity];
	}
	
//	/**
//	 * 
//	 * @return 元素的个数
//	 */
//	public int getSize() {
//		//i作为计数器
//		int i = 0;
//		for (E e : array) {
//			if(e!=null) {
//				i++;
//			}
//		}
//		
//		return i;
//	}
	
	/**
	 * 元素个数加1
	 */
	private void increment() {
		size++;
	}
	
	/**
	 * 元素个数减1
	 */
	private void decrement() {
		size--;
	}
	
	/**
	 * 
	 * @return 返回Mylist大小
	 */
	public int getSize() {
		return size;
	}
	
	/**
	 * 是否容量已满或者元素个数少于扩容后的1/2
	 */
	public void checkArray() {
		//数组是否已满
		if(size >= array.length) {
			//先将数组内的元素暂时存储
			E[] t = (E[]) new Object[DEFAULCAPACITY];
			t = array;
			//将容量扩大到二倍
			DEFAULCAPACITY <<= 1;
			array = (E[]) new Object[DEFAULCAPACITY];
			//用数组复制
			System.arraycopy(t, 0, array, 0, t.length);
		}
	
//		if(size < array.length/2) {
//			
//		}
		
	}
	
	/**
	 * 添加元素
	 * @param 要添加的对象
	 */
	public void add(E e) {
		checkArray();

		//将元素添加到数组
		array[size] = e;
		increment();
	}
	
	/**
	 * 添加元素到指定位置
	 * @param 插入的元素
	 * @param 元素插入第几个位置   0是第一个位置     
	 */
	public void add(E e, Integer index) {
		//没有指定index或者index>MyList+1 复杂度低
		if(index == null || index == size+1) {
			add(e);
		}else {
			checkArray();
			
			for(int j = size; j >= index; j--) {
				array[j+1] = array[j];
			}
			//将元素插入到指定位置
			array[index] = e;
		}
		//容量+1
		increment();
	}
	
	/**
	 * 按位置删除元素
	 * @param index 元素的位置
	 * @return 返回删除的元素
	 */
	public E remove(Integer index) {
		E e= array[index];
		
		for(int j = index; j < size-1; j++) {
			array[j] = array[j+1];
		}
		
		decrement();
		return e;
	}
	
	/**
	 * 
	 * @param index
	 * @return 返回要删除元素所在的位置 找到返回位置  找不到返回null
	 */
	public Integer remove(Object obj) {
		Integer a = null;
		for(int i = 0; i < size ; i++) {
			if(array[i].equals(obj) || array[i] == obj) {
				a = i;
				for(int j = i; j < size-1; j++) {
					array[j] = array[j+1];
				}
			}
		}
		decrement();
		return a;
	}
	
	/**
	 * 
	 * 元素替换
	 * @param e 新元素
	 * @param index 位置
	 * @return 替换掉的元素
	 */
	public E set(E e, Integer index) {
		E t = array[index];
		array[index] = e;
		return t;
	}
	
	/**
	 * 
	 * @param 是否存在的元素
	 * @return
	 */
	public Integer containt(Object obj) {
		Integer a = null;
		for(int i = 0; i < size; i++) {
			if(array[i].equals(obj) || array[i] == obj) {
				a = i;
				break;
			}
		}
		
		return a;
	}
	/**
	 * 清空
	 * @return
	 */
	public boolean clear() {
		array = null;
		return true;
	}
	
	/**
	 * 
	 * @param index元素的位置  0是第一个位置
	 * @return 返回元素
	 */
	public E get(Integer index) {
		return array[index];
	}
	
}

 

Stkcd [股票代码] ShortName [股票简称] Accper [统计截止日期] Typrep [报表类型编码] Indcd [行业代码] Indnme [行业名称] Source [公告来源] F060101B [净利润现金净含量] F060101C [净利润现金净含量TTM] F060201B [营业收入现金含量] F060201C [营业收入现金含量TTM] F060301B [营业收入现金净含量] F060301C [营业收入现金净含量TTM] F060401B [营业利润现金净含量] F060401C [营业利润现金净含量TTM] F060901B [筹资活动债权人现金净流量] F060901C [筹资活动债权人现金净流量TTM] F061001B [筹资活动股东现金净流量] F061001C [筹资活动股东现金净流量TTM] F061201B [折旧摊销] F061201C [折旧摊销TTM] F061301B [公司现金流1] F061302B [公司现金流2] F061301C [公司现金流TTM1] F061302C [公司现金流TTM2] F061401B [股权现金流1] F061402B [股权现金流2] F061401C [股权现金流TTM1] F061402C [股权现金流TTM2] F061501B [公司自由现金流(原有)] F061601B [股权自由现金流(原有)] F061701B [全部现金回收率] F061801B [营运指数] F061901B [资本支出与折旧摊销比] F062001B [现金适合比率] F062101B [现金再投资比率] F062201B [现金满足投资比率] F062301B [股权自由现金流] F062401B [企业自由现金流] Indcd1 [行业代码1] Indnme1 [行业名称1] 季度数据,所有沪深北上市公司的 分别包含excel、dta数据文件格式及其说明,便于不同软件工具对数据的分析应用 数据来源:基于上市公司年报及公告数据整理,或相关证券交易所、各部委、省、市数据 数据范围:基于沪深北证上市公司 A股(主板、中小企业板、创业板、科创板等)数据整理计算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值