【问题标题】:Implementing a data structure with add, remove and contains methods使用 add、remove 和 contains 方法实现数据结构
【发布时间】:2013-12-21 10:28:47
【问题描述】:

我想在java中创建一个数据结构,基本上我已经研究过了,最好的结构是允许重复的列表,我想在没有Java API的情况下尝试这个。我不知道如何去做,甚至不知道如何开始,并且已经完成了大约 6 个小时的研究。

class WordStoringTest implements WordStore {
    private String[] words;

    public WordStoringTest(int n){
        words = new String[n];
    }
    public void add(String word) {
        int count = words.length+1;
        words = new String[count];
    }

    @Override
    public int count(String word) {
    int count =0;
        for(int i=0; i<words.length; i++){
            if(words[i].equals(word)){
                count++;
            }
        }
        return count;
    }

    @Override
    public void remove(String word) {
        // TODO Auto-generated method stub
    }
}

我不知道从哪里开始,请给我一些指导:)谢谢

【问题讨论】:

  • 您具体遇到了什么问题?
  • I would like to attempt this without the java api。第一个问题是 why ?当 Java 已经预先构建了大量 API 时,为什么还要重新发明轮子。
  • 如何开始,我应该使用字符串数组吗?
  • @SaifAsif 有时候我们需要造轮子来了解轮子。
  • 如果你完全被卡住了,那么阅读HashSet的源代码怎么样? Java 是开源的。也就是说,如果 Set 是你想要的,我真的看不出 count() 方法的意义,因为它总是返回 0 或 1。你应该先熟悉集合是什么,然后再考虑如何实现他们。

标签: java hashset


【解决方案1】:

这个怎么样(已测试)。

注意:这就像一个 Java List,NOT Set 因为有重复。

这不是最好的实现。我们可以为像initialCapacity这样的单词数组准备数组缓冲区。

公共类 WordStoringTest 实现 WordStore {

private String[] words;

public WordStoringTest() {
    this(0);
}

public WordStoringTest(int n) {
    this.words = new String[n];
}

public void add(String word) {
    String[] newWords = new String[this.words.length + 1];
    System.arraycopy(this.words, 0, newWords, 0, this.words.length);
    newWords[newWords.length - 1] = word;
    this.words = newWords;
}

// @Override
public int count(String word) {
    int count = 0;
    for (String w : this.words) {
        if (word.equals(w)) {
            count++;
        }
    }
    return count;
}

// @Override
public void remove(String word) {
    int pos = 0;
    String[] temp = this.words;
    while (pos < temp.length) {
        String w = temp[pos];
        if (w.equals(word)) {
            String[] newTemp = new String[temp.length - 1];
            if (pos == 0) {
                System.arraycopy(temp, 1, newTemp, 0, newTemp.length);
            } else if (pos == temp.length - 1) {
                System.arraycopy(temp, 0, newTemp, 0, newTemp.length);
            } else {
                System.arraycopy(temp, 0, newTemp, 0, pos);
                System.arraycopy(temp, pos + 1, newTemp, pos, newTemp.length - pos);
            }
            temp = newTemp;
        } else {
            pos++;
        }
    }
    this.words = temp;
}

}

【讨论】:

  • 感谢 Loc,有什么方法可以让上述内容更高效,我尝试将 10,000 个单词添加到数组中,然后再添加 10,000 个单词,这需要相当长的时间。我试图改进我编写代码的方式以提高效率,例如我也一直在学习最好的排序方法,请建议让这种数据结构更有效地用于大量使用
  • 正如我在评论中所说,为了获得更好的性能。您需要在此处为​​单词数组准备缓冲区(例如 new ArrayList(initialCapacity) )。例如。而不是 new String[this.words.length + 1],你必须这样编码: new String[this.words.length + 1 + 100]; // 100 是缓冲区。当然,这需要对我的代码进行一些修改。
  • 不是固定缓冲区大小,数组列表实现通常是分配大小的两倍。这会导致摊销 O(1) 添加而不是 O(n)。
  • 问题是数组列表是 Java API 的一部分
  • 我一直在研究 java 中的集合,但真的不知道如何在我的程序中实现它们来代替 StringBuffer
【解决方案2】:

您的代码中存在不一致,很难理解它......例如,您建议“set”是您想要的东西,但是您有一个 count(...) 方法表明您期望'set' 中值的多个副本。传统的理解是“集合”的成员对于该集合的任何其他成员都不是equals()

此外,您现在拥有的只是一组数据。这个数据数组没有给你任何优势....有优势的东西是(你在 java.util.* 中得到的东西):

  • 所有值都是唯一的
  • 搜索数据的能力非常“快速”
  • 减少了维护,因为数据会根据您的需要增长/缩小。
  • 数据总是排序的

... something 必须比普通数组更好。

现在,即使你的 add() 方法也不起作用:

public void add(String word) {
    int count = words.length+1;
    words = new String[count];
}

上述方法会删除数组中的所有数据,创建一个空的,仅此而已。

我的建议是让您查看一些标准示例以了解事情是如何完成的...考虑查看....google basic java data structures....我找到了this blog which looks useful

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2021-04-04
    • 2021-06-18
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    相关资源
    最近更新 更多