Java ArrayMap实现

问题描述

我正在尝试将ArrayMap实现为一种实践。这是代码框架。 https://gitlab.cs.washington.edu/cse373-20su-students/skeleton/-/blob/master/maps/src/maps/ArrayMap.java

/**
 * @see AbstractIterableMap
 * @see Map
 */
public class ArrayMap<K,V> extends AbstractIterableMap<K,V> {
    // Todo: define a reasonable default value for the following field
    private static final int DEFAULT_INITIAL_CAPACITY = 0;
    /*
    Warning:
    You may not rename this field or change its type.
    We will be inspecting it in our secret tests.
     */
    SimpleEntry<K,V>[] entries;

    // You may add extra fields or helper methods though!

    public ArrayMap() {
        this(DEFAULT_INITIAL_CAPACITY);
    }

    public ArrayMap(int initialCapacity) {
        this.entries = this.createArrayOfEntries(initialCapacity);
        // Todo: replace this with your code
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    /**
     * This method will return a new,empty array of the given size that can contain
     * {@code Entry<K,V>} objects.
     *
     * Note that each element in the array will initially be null.
     *
     * Note: You do not need to modify this method.
     */
    @SuppressWarnings("unchecked")
    private SimpleEntry<K,V>[] createArrayOfEntries(int arraySize) {
        /*
        It turns out that creating arrays of generic objects in Java is complicated due to something
        kNown as "type erasure."

        We've given you this helper method to help simplify this part of your assignment. Use this
        helper method as appropriate when implementing the rest of this class.

        You are not required to understand how this method works,what type erasure is,or how
        arrays and generics interact.
        */
        return (SimpleEntry<K,V>[]) (new SimpleEntry[arraySize]);
    }

    @Override
    public V get(Object key) {
        // Todo: replace this with your code
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public V put(K key,V value) {
        // Todo: replace this with your code
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public V remove(Object key) {
        // Todo: replace this with your code
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void clear() {
        // Todo: replace this with your code
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public boolean containsKey(Object key) {
        // Todo: replace this with your code
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public int size() {
        // Todo: replace this with your code
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public Iterator<Map.Entry<K,V>> iterator() {
        // Note: you won't need to change this method (unless you add more constructor parameters)
        return new ArrayMapIterator<>(this.entries);
    }

    // Todo: after you implement the iterator,remove this toString implementation
    // Doing so will give you a better string representation for assertion errors the debugger.
    @Override
    public String toString() {
        return super.toString();
    }

    private static class ArrayMapIterator<K,V> implements Iterator<Map.Entry<K,V>> {
        private final SimpleEntry<K,V>[] entries;
        // You may add more fields and constructor parameters

        public ArrayMapIterator(SimpleEntry<K,V>[] entries) {
            this.entries = entries;
        }

        @Override
        public boolean hasNext() {
            // Todo: replace this with your code
            throw new UnsupportedOperationException("Not implemented yet.");
        }

        @Override
        public Map.Entry<K,V> next() {
            // Todo: replace this with your code
            throw new UnsupportedOperationException("Not implemented yet.");
        }
    }
}

我对为什么ArrayMapIterator是静态嵌套类感到困惑。因为hasNext()需要从封闭类访问大小。这个设计不好吗?还是我错过了重点。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)