微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java ArrayMap实现

如何解决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 举报,一经查实,本站将立刻删除。