数据结构泛型之初接触

Posted xmdykf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构泛型之初接触相关的知识,希望对你有一定的参考价值。

1. 设计一个泛型类 Collection,它存储 object 对象的集合(在数组中),以及该集合的当前大小,提供......等方法。

import java.util.Arrays;

/**
 * @author 小喵钓鱼
 * @date 2020-02-13 12:21
 * @veision 1.10
 */
public class Collection<E> {
    private Object[] elementData;


    // setter
    public void setElementData(Object [] obj)
    {
        this.elementData = obj;
    }

    // getter
    public Object[] getElementData() {
        return elementData;
    }

    // isEmpty
    public boolean isEmplty()
    {
        return elementData.length == 0;
    }

    // makeEmpty
    // 与 list 的clear() 方法类似,Collection 不为 null,但是元素个数为0
    public void makeEmplty()
    {
        elementData = new Object[]{};
    }

    public void insert(E obj)
    {
        int length = elementData.length;
        Object [] temp = new Object[length + 1];
        /**
         *  System..arraycopy
         *  源数组,开始位置,目标组,目标开始,目标结束位置
         */
        System.arraycopy(elementData, 0, temp, 0, length);
        // 最后一个位置 赋予
        temp[length] = obj;
        // 将 temp 又赋予回去
        elementData = temp;
    }

    public void remove(int index) {
        // 如果 index 不符合规范
        if (index < 0 || elementData == null)
            return;
        int length = elementData.length;
        // 如果 index ...
        if (length - 1 < index)
        {
            return;
        }
        Object [] temp = new Object[length - 1];
        /**
         * 思路,从 源数据的 0 开始到 index - 1 处
         * 然后 再从 elementData 的 index + 1 开始 到末尾
         */
        System.arraycopy(elementData, 0, temp, 0, index);
        System.arraycopy(elementData, index + 1, temp, index, length - index - 1);
        elementData = temp;
    }

    public void remove(E obj)
    {
        if (elementData == null || obj == null) {
            return;
        }
        int length = elementData.length;
        for (int i =0; i < length; i++) {
            if (elementData[i] != null && elementData[i] == obj)
            {
                Object [] temp = new Object[length - 1];
                System.arraycopy(elementData, 0, temp, 0, i);
                System.arraycopy(elementData, i + 1, temp, i,length - 1 - i);
                elementData = temp;
                break;
            }
        }
    }

    public boolean isPresent(E obj) {
        if (elementData == null || obj == null) {
            return false;
        }
        boolean flag = false;

        for (int i = 0; i < elementData.length; i++)
        {
            if (null != elementData[i] && obj == elementData[i])
            {
                flag = true;
                break;
            }
        }

        return flag;

    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub    
        int i = 0;
        Collection<String> collection = new Collection<String>();
        Object[] objects = new Object[]{"1"};
        collection.setElementData(objects);
        collection.insert("2");
        collection.insert("4");
        System.out.println(Arrays.toString(collection.getElementData()));
        collection.remove(0);
        System.out.println(Arrays.toString(collection.getElementData()));
        boolean flag = collection.isEmplty();
        System.out.println("isEmpty: " + flag);
        collection.makeEmplty();
        boolean flag2 = collection.isEmplty();
        System.out.println("isEmpty: " + flag2);
    }
}

2. 设计一个泛型类 OrderCollection,它存储 Comparable的对象的集合(在数组中).....

/**
 * @author 小喵钓鱼
 * @date 2020-02-13 13:30
 * @veision 1.10
 */
public class OrderedCollection<E> {
    private Comparable[] elementData;

    public Comparable[] getElementData() {
        return elementData;
    }

    public void setElementData(Comparable[] elementData) {
        this.elementData = elementData;
    }

    public boolean isEmpty()
    {
        return elementData.length == 0;
    }

    public void makeEmpty()
    {
        elementData = new Comparable[]{};
    }

    public void insert(Comparable c)
    {
        int length = elementData.length;
        Comparable[] temp = new Comparable[length + 1];

        System.arraycopy(elementData, 0, temp, 0, length);
        temp[length] = c;
        elementData = temp;
    }



    public Comparable<E> findMin()
    {
        if (elementData == null)
            return null;

        int length = elementData.length;
        Comparable min = elementData[0];
        for (int i = 0; i < length; i++)
        {
            if (min.compareTo(elementData[i]) < 0)
            {
                min = elementData[i];
            }
        }
        return min;
    }

    public Comparable<E> findMax()
    {
        if (elementData == null)
            return null;

        int length = elementData.length;
        Comparable max = elementData[0];
        for (int i = 0; i < length; i++)
        {
            if (max.compareTo(elementData[i]) > 0)
            {
                max = elementData[i];
            }
        }
        return max;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        OrderedCollection<String> orderedCollection = new OrderedCollection<String>();
        Comparable[] comparables = new Comparable[]{"abc"};
        orderedCollection.setElementData(comparables);
        orderedCollection.insert("acc");
        orderedCollection.insert("bcc");
        orderedCollection.insert("dcc");
        orderedCollection.insert("aaa");
        Comparable min = orderedCollection.findMin();
        System.out.println("Min: " + min);
        Comparable max = orderedCollection.findMax();
        System.out.println("Max: " + max);
    }
}

以上是关于数据结构泛型之初接触的主要内容,如果未能解决你的问题,请参考以下文章

泛型之泛型方法

泛型之逆变和协变总结

Java泛型之<T>

Java泛型之类型未被擦除

Java基础 -- 泛型之泛型参数

Java泛型之类型擦除