Java数据结构之手写顺序表

简单实现顺序表

package com.mmg.sqltest;

/**
 * 手工实现顺序表
 * @author MMG
 */
public class MMGList {

    //定义变量
    private Object[] data;
    private int n;

    //初始化列表
    public MMGList(int length) {
        this.data = new Object[length];
        this.n = 0;
    }

    public MMGList(Object[] values) {
        this(values.length);
        for (int i = 0; i < values.length; i++) {
            this.data[i] = values[i];
        }
        this.n = values.length;
    }

    //给列表一个初始化的大小
    public MMGList() {
        this(10);
    }

    //根据下标获取值
    public Object get(int i) {
        if (i >= 0 && i < this.n) {
            return this.data[i];
        } else {
            throw new IndexOutOfBoundsException(i + "下标越界");
        }
    }

    //根据下表设置值
    public void set(int i, Object obj) {
        if (obj == null)
            throw new NullPointerException("t==null");
        if (i >= 0 && i < this.n) {
            this.data[i] = obj;
        } else {
            throw new IndexOutOfBoundsException(i + "下标越界");
        }
    }

    //插入值
    public int insert(Object obj) {
        return this.insert(this.n, obj);
    }

    //根据下表插入值
    public int insert(int i, Object obj) {
        if (obj == null)
            throw new NullPointerException("空指针异常!");
        if (i >= 0 && i <= this.n) {
            Object src[] = this.data;
            if (this.n == data.length) {
                this.data = new Object[src.length +1];
                for (int j = 0; j < i; j++) {
                    this.data[j] = src[j];
                }
            }
            for (int j = this.n - 1; j >= i; j--) {
                this.data[j + 1] = src[j];
            }
            this.data[i] = obj;
            this.n++;
            return i;
        } else {
            throw new IndexOutOfBoundsException(i + "下标越界");
        }
    }

    //根据下表删除值
    public Object remove(int i) {
        if (this.n == 0) {
            try {
                throw new Exception("空表无法删除!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (i >= 0 && i < this.n) {
            Object old = this.data[i];
            for (int j = i; j < this.n - 1; j++) {
                this.data[j] = this.data[j + 1];
            }
            this.data[this.n - 1] = null;
            this.n--;
            return old;
        } else {
            throw new IndexOutOfBoundsException(i + "下标越界");
        }
    }

    //判断是否有对应的值
    public boolean contains(Object key) {
        for (int i = 0; i < this.n; i++) {
            if (key.equals(this.data[i])) {
                return true;
            }
        }
        return false;
    }

    //列表大小
    public int size() {
        return this.n;
    }

    //清空列表
    public void clear() {
        this.n = 0;
    }

    //判断列表是否为空
    public boolean isEmpty() {
        return this.n == 0;
    }

    //打印列表
    public void printList() {
        for (int i = 0; i < this.n; i++) {
            System.out.println(this.data[i]);
        }
    }

    //获取元素在列表中出现的一个的位置
    public int indexOf(Object key) {
        for (int i = 0; i < this.n; i++) {
            if (key.equals(this.data[i])) {
                return i;
            }
        }
        return -1;
    }
    
	//有序顺序表的合并
	public void merge(MMGList listA,MMGList listB,MMGList listC){
		int i,j;
		i=j=0;//两表均从第一个元素开始依次扫描、比较
		while(i<listA.size()&&j<listB.size()){
			//将两表中较小的数插入到listC表中
			Object o1 = listA.get(i);
			Object o2 = listB.get(j);
				if(!o1.equals(o2)){
					listC.insert(listA.get(i));
					i++;
				}else{
					listC.insert(listB.get(j));
					j++;
				}
			}
			//若剩余的是listA表,将表中剩余元素全部插入到listC表中
			while(i<listA.size()){
				listC.insert(listA.get(i));
				i++;
			}
			//若剩余的是listB表,将表中剩余元素全部插入到listC表中
			while(j<listB.size()){
				listC.insert(listB.get(j));
				j++;
			}
		}
}

可以自己写个测试类进行测试!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木芒果呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值