在Java中使String数据结构更有效

时间:2013-12-08 15:41:08

标签: java oop data-structures

我有创建数据结构的任务和功课,这将使程序工作,它基本上生成随机的无意义的单词,你需要创建一个存储它们的数据结构,可以添加,计数和删除它们。这是我到目前为止所做的,但需要提高效率,提高效率。我也不能使用Java API。

public class WordStoringTest implements WordStore {
private StringBuilder[] words;
private int[] array;
private int[] abcdefg;
private int[] hijklmn;
private int[] opqrstu;
private int[] vwxyz;

private int n;

public WordStoringTest(int n) {
    this.n = n;
    this.words = new StringBuilder[n];
    this.array = new int[n];    
    this.abcdefg = new int[n];
    this.hijklmn = new int[n];
    this.opqrstu = new int[n];
    this.vwxyz = new int[n];

    for(int i=0; i<n; i++){
        abcdefg[i] = 0;
        hijklmn[i] = 0;
        opqrstu[i] = 0;
        vwxyz[i] = 0;
    }
}

public void add(String word) {
    StringBuilder word2 = new StringBuilder(word);
    int i = word2.hashCode(); 
    StringBuilder[] newWords = new StringBuilder[this.words.length + 1];
    System.arraycopy(this.words, 0, newWords, 0, this.words.length);
    newWords[newWords.length - 1] = word2;
    this.words = newWords;  
    if(word.charAt(0) >= 'a' && word.charAt(0) <= 'g'){
        int count =0;
        for(int a=0; a<abcdefg.length; a++){
            if(abcdefg[count] !=0){
                count++;
            }
        }
        int[] arrayTemp = new int[count+1];
        int c =0;
        for(int v = 0; v<abcdefg.length; v++){
            if(abcdefg[v] !=0){
                arrayTemp[v] = abcdefg[v];
                c++;
            }
        }
        arrayTemp[c] = i;
        abcdefg = arrayTemp;
    }
    else if(word.charAt(0) >= 'h' && word.charAt(0) <= 'n'){
        int count =0;
        for(int a=0; a<hijklmn.length; a++){
            if(hijklmn[count] !=0){
                count++;
            }
        }

        int[] arrayTemp = new int[count+1];
        int c =0;
        for(int v = 0; v<hijklmn.length; v++){
            if(hijklmn[v] !=0){
                arrayTemp[v] = hijklmn[v];
                c++;
            }
        }
        arrayTemp[c] = i;
        hijklmn = arrayTemp;
    }
    else if(word.charAt(0) >= 'o' && word.charAt(0) <= 'u'){
        int count =0;
        for(int a=0; a<opqrstu.length; a++){
            if(opqrstu[count] !=0){
                count++;
            }
        }

        int[] arrayTemp = new int[count+1];
        int c =0;
        for(int v = 0; v<opqrstu.length; v++){
            if(opqrstu[v] !=0){
                arrayTemp[v] = opqrstu[v];
                c++;
            }
        }
        arrayTemp[c] = i;
        opqrstu = arrayTemp;
    }
    else if(word.charAt(0) >= 'v' && word.charAt(0) <= 'z'){
        int count =0;
        for(int a=0; a<vwxyz.length; a++){
            if(vwxyz[count] !=0){
                count++;
            }
        }
        int[] arrayTemp = new int[count+1];
        int c =0;
        for(int v = 0; v<vwxyz.length; v++){
            if(vwxyz[v] !=0){
                arrayTemp[v] = vwxyz[v];
                c++;
            }
        }
        arrayTemp[c] = i;
        vwxyz = arrayTemp;
    }
}

public int count(String word) {
    int i = word.hashCode();
    int count = 0;
    int[] temp = null;
    if(word.charAt(0) >= 'a' && word.charAt(0) <= 'g'){
        temp = abcdefg;
    }
    else if(word.charAt(0) >= 'h' && word.charAt(0) <= 'n'){
        temp = hijklmn;
    }
    else if(word.charAt(0) >= 'o' && word.charAt(0) <= 'u'){
        temp = opqrstu;
    }
    else if(word.charAt(0) >= 'v' && word.charAt(0) <= 'z'){
        temp = vwxyz;
    }

    for (int w = 0; w<temp.length; w++){
        if (array[w] == i) {
            count++;
        }
    }
    return count;
}

public void remove(String word) {
    int pos = 0;
    StringBuilder[] temp = this.words;
    while (pos < temp.length) {
        StringBuilder w = temp[pos];
        if (w.equals(word)) {
            StringBuilder[] newTemp = new StringBuilder[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;
    System.out.println("1" + "*");






    int posi = 0;
    int f = word.hashCode();
    int[] temps = this.array;
    while (posi < temps.length) {
        int g = temps[posi];
        if (g ==f) {
            int[] newTemps = new int[temps.length - 1];
            if (posi == 0) {
                System.arraycopy(temps, 1, newTemps, 0, newTemps.length);
            } else if (posi == temps.length - 1) {
                System.arraycopy(temps, 0, newTemps, 0, newTemps.length);
            } else {
                System.arraycopy(temps, 0, newTemps, 0, posi);
                System.arraycopy(temps, posi + 1, newTemps, posi, newTemps.length - posi);
            }
            temps = newTemps;
        } else {
            posi++;
        }
    }
    this.array = temps;

}
}

我知道删除方法不起作用:)

1 个答案:

答案 0 :(得分:1)

首先,

  • 不使用StringBuffer,差不多十年前它们被StringBuilder取代,速度稍快。
  • 在使用基元时不要使用包装器。这不仅表现更好,而且意图更清晰。如果它不是Integer并且您不需要,请不要使用null
  • 我会使用ArrayList作为StringBuilders的列表,或者我会以相同的方式增长数组。即根据需要增加一倍的尺寸,而不是在每个add()
  • 上增加尺寸
  • 使用word.charAt(0) >= 'a' && word.charAt(0) <= 'g'
  • 等范围检查
  • 不止一次使用计数器,你似乎移动哪个计数器在每次迭代时都会递增。
  • 由于每个字符范围中的代码基本相同,您应该能够使代码相同并使用计数器集合而不是重复代码。

根据您的描述,它表明代码应该远远更简单。喜欢这个

// unfortunately builtin collections force you to use wrappers. 
// Trove for example, does not.
final Map<String, Integer> wordCount = new HashMap<>();
public void add(String word) {
    Integer count = wordCount.get(word);
    if (count == null)
        count = 0;
    wordCount.put(word, count+1);
}
public int count(String word) {
    Integer count = wordCount.get(word);
    return count == null ? 0 : count.intValue();
}
public void remove(String word) {
    Integer count = wordCount.get(word);
    if (count == null)
        return;
    if (count <= 1);
        wordCount.remove(word);
    else
        wordCount.put(word, count+1);
}

比较使用Trove4j

// No Integer wrappers needed.
final TObjectIntHashMap<String> wordCount = new TObjectIntHashMap<String>();
public void add(String word) {
    wordCount.adjustOrPutValue(word, 1, 1);
}
public int count(String word) {
    return wordCount.get(word);
}
public void remove(String word) {
    int count = wordCount.adjustOrPutValue(word, 0, -1);
    if (count <= 0);
        wordCount.remove(word);
}