简体   繁体   English

使用add,remove和contains方法实现数据结构

[英]Implementing a data structure with add, remove and contains methods

I would like to create a data structure in java, basically I have researched this and the best structure would be a list to allow for duplicates, I would like to attempt this without the Java API. 我想在Java中创建一个数据结构,基本上我已经研究了这个结构,最好的结构是一个允许重复的列表,我想在没有Java API的情况下尝试这样做。 I don't know how to go about doing this or even starting this and have already done around 6 hours of research. 我不知道该怎么做,甚至不知道如何开始,并且已经完成了大约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 don't know where to start please give me some guidance :) thanks 我不知道从哪里开始,请给我一些指导:)谢谢

There are inconsistencies in your code that make understanding it quite hard.... for example, you suggest a 'set' is the thing you want it to be, but then you have a count(...) method which indicates you expect multiple copies of the values in the 'set'. 您的代码中存在不一致的地方,这使得理解起来非常困难。例如,您建议将“ set”作为您想要的东西,但是然后您有了一个count(...)方法,它表明您期望“设置”中值的多个副本。 Traditional understanding is that members of a 'set' are not equals() to any other member of the set. 传统的理解是,“集合”的成员不equals()集合的任何其他成员的equals()

Additionally, what you have right now is just an array of data. 此外,您现在拥有的只是一个数据数组。 This data array does not give you any advantages.... Things that would be advantages are (the sorts of things you get in java.util.*): 该数据数组不会给您带来任何好处。...可能会带来好处的是(在java.util。*中获得的各种东西):

  • all values are unique 所有值都是唯一的
  • the ability to search the data is 'fast' 搜索数据的能力是“快速的”
  • there is reduced maintenance because the data will grow/shrink as you need it. 减少了维护,因为数据将根据您的需要增长/缩小。
  • the data is always sorted 数据总是被排序

... something has to be better than just a plain array. ... 一定要比单纯的数组更好。

Right now, not even you add() method works: 现在,即使您的add()方法也无法正常工作:

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

The above method will delete all the data in the array by creating an empty one, and that's all. 上面的方法将通过创建一个空的数组来删除数组中的所有数据,仅此而已。

My suggestion os for you to look through some standard examples for how things are done... consider looking at .... google basic java data structures .... I found this blog which looks useful 我的建议是让您浏览一些标准示例以了解操作方式...考虑查看.... google基本Java数据结构 ....我发现此博客看起来很有用

How about this (Tested). 怎么样(已测试)。

Note: This is like a Java List, NOT Set because there are duplicate. 注意:这就像一个Java列表,未设置,因为存在重复项。

This is not the best implementation. 这不是最好的实现。 We can prepare array buffer for word array like initialCapacity. 我们可以为单词数组(如initialCapacity)准备数组缓冲区。

public class WordStoringTest implements WordStore { 公共类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;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM