假设有两个集合A和B分别用两个线性表A和B表示,即线性表中的数据元素为集合中的成员,集合A是集合B的子集。编写算法分别求A和B的并集C=A∪B,A和B的交集C= A∩B及A在B中的补集C=B-A

题目:假设有两个集合A和B分别用两个线性表A和B表示,即线性表中的数据元素为集合中的成员,集合A是集合B的子集。编写算法分别求A和B的并集C=A∪B,A和B的交集C= A∩B及A在B中的补集C=B-A

public class Array<E> {
    private E[] data;
    private int size;

    /**
     * 有参构造方法
     *
     * @param capacity 传入数组的容量capacity构造Array
     */
    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    /**
     * 设置数组容量的默认值capacity=10
     */
    public Array() {
        this(10);
    }

    //获得数组的容量
    public int getCapcaity() {
        return data.length;
    }

    //获得数组的元素的个数
    public int getSize() {
        return size;
    }

    //返回数组是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 在所所有元素添加一个新的元素
     *
     * @param e 添加的值
     */
    public void addFirst(E e) {
        add(0, e);
    }

    /**
     * 添加元素
     *
     * @param e 添加元素的值
     */
    public void addLast(E e) {
        add(size, e);
    }

    /**
     * @param index 插入的index
     * @param e     要添加的值
     */
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
        }
        if (size == data.length) {
//            throw new IllegalArgumentException("Add failes.Array is full.");
            resize(2 * data.length);
        }

        //把要添加元素后的元素往回移动一个位置
        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }

        data[index] = e;

        size++;
    }

    /**
     * 获取index索引位置的元素
     *
     * @param index 索引位置
     */
    public E get(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
        }
        return data[index];
    }

    public E getLast() {
        return get(size - 1);
    }

    public E getFirst() {
        return get(0);
    }

    /**
     * 修改index索引位置的元素为e
     *
     * @param index 索引
     * @param e     值
     */
    public void set(int index, E e) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Set failed. Index is illegal.");
        data[index] = e;
    }

    //查找数组中是否有元素e
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) {
                return true;
            }
        }
        return false;
    }

    // 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) {
                return i;
            }
        }
        return -1;
    }

    // 从数组中删除index位置的元素, 返回删除的元素
    public E remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Remove failed. Index is illegal.");
        }
        E ret = data[index];
        for (int i = index + 1; i < size; i++) {
            data[i - 1] = data[i];
        }
        size--;
        data[size] = null; // loitering objects != memory leak

        if (size == data.length / 2) {
            resize(data.length / 2);
        }
        return ret;
    }

    // 从数组中删除第一个元素, 返回删除的元素
    public E removeFirst() {
        return remove(0);
    }

    // 从数组中删除最后一个元素, 返回删除的元素
    public E removeLast() {
        return remove(size - 1);
    }

    // 从数组中删除元素e
    public void removeElement(E e) {
        int index = find(e);
        if (index != -1)
            remove(index);
    }

    //a和b中的并集
    public static <E> void union(Array<E> a, Array<E> b, Array<E> c) {
        //将a的元素遍历的c
        for (int i = 0; i < a.size; i++) {
            E t = (E) a.get(i);
            c.add(i, t);
        }
        //判断b的元素是否在a中,不在则加入到c中
        for (int i = 0; i < b.size; i++) {
            E t = (E) b.get(i);
            if (a.find(t) == -1)
                c.add(i, t);
        }
    }

    //a在b中的补集
    public static <E> void sub(Array<E> a, Array<E> b, Array<E> c) {
        for (int i = 0; i < b.size; i++) {
            E t = (E) b.get(i);
            if (a.find(t) == -1)
                c.addLast(t);
        }
    }

    //a和b交集
    public static <E> void intersection(Array<E> a, Array<E> b, Array<E> c) {
        for (int i = 0; i < a.size; i++) {
            E t = (E) a.get(i);
            if (b.find(t) != -1)
                c.addLast(t);
        }
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
//        res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length));
        res.append('[');
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size - 1)
                res.append(", ");
        }
        res.append(']');
        return res.toString();
    }

    private void resize(int newCapacity) {

        E[] newdata = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newdata[i] = data[i];
        }
        data = newdata;
    }
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值