如果此单词不在此数组中,如何更新数组?

时间:2015-12-08 16:11:12

标签: java arrays

我正在努力将Java文件转换为XREF文件。

这是我的问题:我想检查一个单词是否在数据库中,如果它不在数据库中,则将其添加到数据库中。

这是我的代码(这是一种方法):

void add(String word, int lineNumber) {
    /* If not a Java reserved word, insert word into the database,
       indicating that it is referenced on line lineNumber */

    // all Java reserved words; word at end guarantees
    String[] javaReservedWord = {
        "abstract", "assert", "boolean", "break",
            "byte", "case", "catch", "char", "class", "const", "continue",
            "default", "do", "double", "else", "enum", "extends", "false",
            "final", "finally", "float", "for", "goto", "if", "implements",
            "import", "instanceof", "int", "interface", "long", "native",
            "new", "null", "package", "private", "protected", "public",
            "return", "short", "static", "strictfp", "super", "switch",
            "synchronized", "this", "throw", "throws", "transient", "true",
            "try", "void", "volatile", "while", word
    };

    int i; // termination of for loop

    // traverse javaReservedWord and see if any entry matches word
    for (i = 0; !word.equals(javaReservedWord[i]); i++);

    // matches a reserved word, so don't add to database
    if (i < javaReservedWord.length - 1) return;

    /* doesn't match a reserved word, so proceed by searching to see if
          word is already in database */   
  }

在此方法中,Java文件已StringTokenizer为多个单词,String wordlineNumber是此word所在的行。

3 个答案:

答案 0 :(得分:1)

你假设每行是1个字吗?另外,为什么要将lineNumber作为add()的参数传递?用户是否知道要为方法输入哪个lineNumber?

至于你的add()方法,为什么在类的声明中有这个数组而不是私有数组?如果您打算从其他方法访问此数组,该怎么办?通过这样做,您可以节省方法所具有的代码量。要在字符串数组中添加单词,您可以执行以下操作:

String[] javaReservedWord = {
    "abstract", "assert", "boolean", "break",
    "byte", "case", "catch", "char", "class", "const", "continue",
    "default", "do", "double", "else", "enum", "extends", "false",
    "final", "finally", "float", "for", "goto", "if", "implements",
    "import", "instanceof", "int", "interface", "long", "native",
    "new", "null", "package", "private", "protected", "public",
    "return", "short", "static", "strictfp", "super", "switch",
    "synchronized", "this", "throw", "throws", "transient", "true",
    "try", "void", "volatile", "while"
};

String[] temp = new String[javaReservedWord.length + 1];
for(int i = 0; i < javaReservedWord.length; i++)
    temp[i] = javaReservedWord[i];

temp[javaReservedWord.length + 1] = word;
 javaReservedWord = temp;
}

答案 1 :(得分:1)

您不能从您定义为void的方法返回任何内容,也不能返回两件事。你需要一个特殊的课程。

我会尝试这样的事情:

import java.util.Arrays;
import java.util.List;

public class Word {

    private List<String> javaReservedWord = Arrays.asList(
            "abstract", "assert", "boolean", "break",
            "byte", "case", "catch", "char", "class", "const", "continue",
            "default", "do", "double", "else", "enum", "extends", "false",
            "final", "finally", "float", "for", "goto", "if", "implements",
            "import", "instanceof", "int", "interface", "long", "native",
            "new", "null", "package", "private", "protected", "public",
            "return", "short", "static", "strictfp", "super", "switch",
            "synchronized", "this", "throw", "throws", "transient", "true",
            "try", "void", "volatile", "while"
    );

    class WordAndNumber {
        private String word;
        private int lineNumber;

        public WordAndNumber(String word, int lineNumber) {
            this.word = word;
            this.lineNumber = lineNumber;
        }

        public String getWord() {
            return word;
        }

        public int getLineNumber() {
            return lineNumber;
        }
    }

    private WordAndNumber add(String word, int lineNumber) {

        if (javaReservedWord.contains(word)) {
            return null;
        } else {
            // add in DB

            return new WordAndNumber(word, lineNumber);
        }
    }
}

答案 2 :(得分:0)

将HashSet初始化为类字段(将由classloader设置) - 每次要添加一行时,您都不会浪费时间来初始化字段。

HashSet将提供一个恒定的搜索时间(而不是ArrayList或Array的lineat) - 搜索将更快地执行。

public class Main {
Set<String> blackList = new HashSet<String>(Arrays.asList("abstract", "assert", "boolean", "break",
        "byte", "case", "catch", "char", "class", "const", "continue",
        "default", "do", "double", "else", "enum", "extends", "false",
        "final", "finally", "float", "for", "goto", "if", "implements",
        "import", "instanceof", "int", "interface", "long", "native",
        "new", "null", "package", "private", "protected", "public",
        "return", "short", "static", "strictfp", "super", "switch",
        "synchronized", "this", "throw", "throws", "transient", "true",
        "try", "void", "volatile", "while"));

void addAllowed(String word, int lineNumber) {
    if (!blackList.contains(word)){
        add(word, lineNumber);
    }
}

void add(String word, int lineNumber){
    //ToDo: code to add word to database
}

}