【问题标题】:Implement dictionary using Java使用Java实现字典
【发布时间】:2023-03-12 09:05:02
【问题描述】:

任务 词典ADT

  • 字典 ADT 为可搜索的关键元素条目集合建模
  • 允许使用相同键的多个项目
  • 应用:词定义对

字典 ADT 方法:

  • find(k):如果字典有一个key为k的条目,返回它,否则返回null
  • findAll(k):返回所有键为 k 的条目的迭代器
  • insert(k, o):插入并返回条目(k, o)
  • remove(e):从字典中删除条目 e
  • size(), isEmpty()

运算输出字典

insert(5,A) (5,A) (5,A)
insert(7,B) (7,B) (5,A),(7,B)
insert(2,C) (2,C) (5,A),(7,B),(2,C)
insert(8,D) (8,D) (5,A),(7,B),(2,C),(8,D)
insert(2,E) (2,E) (5,A),(7,B),(2,C),(8,D),(2,E)
find(7) (7,B) (5,A),(7,B),(2,C),(8,D),(2,E)
find(4) null (5,A),(7,B),(2,C),(8,D),(2,E)
find(2) (2,C) (5,A),(7,B),(2,C),(8,D),(2,E)
findAll(2) (2,C),(2,E) (5,A),(7,B),(2,C),(8,D),(2,E)
size() 5 (5,A),(7,B),(2,C),(8,D),(2,E)
remove(find(5)) (5,A) (7,B),(2,C),(8,D),(2,E)
find(5) null (7,B),(2,C),(8,D),(2,E)

详细解释:否

【问题讨论】:

  • 家庭作业?分享您的想法/代码 - 如果您有任何具体问题,我们会尽力帮助您。
  • 我真的很喜欢提出问题的方式。
  • @marcog:是的,这个很搞笑。 :D
  • 到目前为止你尝试过什么?什么起作用或不起作用?是否允许使用内置集合类?

标签: java dictionary map


【解决方案1】:

Java 已经有一个集合,它几乎可以满足您的所有需求。您只需要添加一种方法。对于初学者来说,探索 java.util.Collection... 类。然后扩展一个以添加所需的方法。如果处理得当,只需几十行。

对我来说,最简单的方法是使用Map<Object, Set<Object>>。棘手的是返回一个迭代器。

编辑:

另一方面,我会选择这个Entry.java

public class Entry<K, V> {

    K key;
    V value;

    public Entry(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K key() {
        return key;
    }

    public V value() {
        return value;
    }

    @Override
    public String toString() {
        return "(" + key + ", " + value + ")";
    }

    // Methods needed to correctly behave in containers like sets, hashmaps:
    // (I generated those automatically in NetBeans)
    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Entry<K, V> other = (Entry<K, V>) obj;
        if (this.key != other.key && (this.key == null || !this.key.equals(other.key)))
            return false;
        if (this.value != other.value && (this.value == null || !this.value.equals(other.value)))
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 23 * hash + (this.key != null ? this.key.hashCode() : 0);
        hash = 23 * hash + (this.value != null ? this.value.hashCode() : 0);
        return hash;
    }
}

...还有这个:Dictionary.java

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Dictionary<K, V> {

    private List<Entry<K, V>> set;

    public Dictionary() {
        this.set = new LinkedList<Entry<K, V>>();
    }

    /**
     * find(k): if the dictionary has an entry with key k, returns it, else, returns null
     */
    public Entry<K, V> find(K key) {
        // for all entries in set...
        //   check if key mathches
        //     - if it does than return it

        // else
        return null;
    }

    /**
     * findAll(k): returns an iterator of all entries with key k
     * @return
     */
    public Iterator<Entry<K, V>> findAll(K key) {
        // make a temporary list
        // for all entries in set...
        //   check if key matches
        //     - if it does than add it to temporary list

        // return the temporary list iterator (list.iterator())
        return null;
    }

    /**
     * insert(k, o): inserts and returns the entry (k, o)
     */
    public Entry<K, V> insert(K key, V value) {
        // obvious
        return null;
    }

    /**
     * remove(e): remove the entry e from the dictionary
     */
    public Entry<K, V> remove(Entry<K, V> entry) {
        return entry;
    }

    public int size() {
        return set.size();
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    @Override
    public String toString() {
        return set.toString();
    }
}

...,还有这个DictionaryTest.java:

public class DictionaryTest {

    static Dictionary<Integer, Character> dict = new Dictionary<Integer, Character>();

    public static void main(String[] args) {

        /*

        Test cases:

        1. insert(5,A) (5,A) (5,A)
        2. insert(7,B) (7,B) (5,A),(7,B)
        3. insert(2,C) (2,C) (5,A),(7,B),(2,C)
        4. insert(8,D) (8,D) (5,A),(7,B),(2,C),(8,D)
        5. insert(2,E) (2,E) (5,A),(7,B),(2,C),(8,D),(2,E)
        6. find(7) (7,B) (5,A),(7,B),(2,C),(8,D),(2,E)
        7. find(4) null (5,A),(7,B),(2,C),(8,D),(2,E)
        8. find(2) (2,C) (5,A),(7,B),(2,C),(8,D),(2,E)
        9. findAll(2) (2,C),(2,E) (5,A),(7,B),(2,C),(8,D),(2,E)
        10. size() 5 (5,A),(7,B),(2,C),(8,D),(2,E)
        11. remove(find(5)) (5,A) (7,B),(2,C),(8,D),(2,E)
        12. find(5) null (7,B),(2,C),(8,D),(2,E)
         */

        // Test case #1:
        test("insert(5,A)", dict.insert(5, 'A'));

        // Test case #2:
        test("insert(7,B)", dict.insert(7, 'B'));

        // Test case #3:
        test("insert(2,C)", dict.insert(2, 'C'));

        // ...

        // Test case #6:
        test("find(7))", dict.find(7));

        // implement all and check them during implementation


    }

    private static void test(String string, Object result) {
        System.out.print(string + " ");
        System.out.print(result);
        System.out.println(" " + dict);
    }
}

【讨论】:

  • 我真的尝试了所有方法,但我无法解决这个问题......我还有 10 小时的时间 2 做这个......不过感谢您的评论
  • 非常感谢你们的帮助,这真的很有效:) 竖起大拇指 Rekin 我爱你
  • 伙计们,我想知道是否有可能 2 将此代码与 Gui 代码 + 数据库(例如 .read .write)链接,以便能够 2 加载保存等
  • 哈,你这样的野心是在自取其辱。你会厌倦数据库和 GUI 编程——相信我。坚持你需要做的事情。如果你想要任何界面,比做一个命令行。关于数据库——这太过分了。
【解决方案2】:

我会建议阅读带有单独链接的哈希表。哈希表是实现字典的好方法。开放课件中有麻省理工学院的讲座。详情请见http://en.wikipedia.org/wiki/Hash_table

【讨论】:

  • 如果您需要 C# 代码来实现哈希表,请告诉我:)
猜你喜欢
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多