问题描述
编辑:[学校作业]
所以,我想编码,只有 0 和 1,单词。
0 = 单词不存在
1 = 出现单词
我的字典对应:
String[] dictionary = {"Hello","I","am","Lukas","and","Jonas","play","football"};
例如:如果我对这些词进行编码...
String[] listofWords = {"Hello","play" "football"};
我必须有以下数组:
int[] wordsEncode = {1,1,1};
您可以看到“Hello”出现,“I”“am”“Lukas”“和”“Jonas”不出现。最后,出现了“play”和“football”。
我们必须保留字典的顺序,这是我代码中的问题。
我真的不知道如何解决这个问题(使用第二个 for 循环?)?
我认为 wordEncode[i] 是我的错误,但如何解决?
这是我的代码:
class Dictionary {
/**
* Array words Dictionary
*/
String[] dictionary;
/**
* Maximum of words MAX_WORDS
*/
final int MAX_WORDS = 50;
/**
* Number of words in the dictionary
*/
int numberWordsDictionary;
/**
* Constructor
*/
Dictionary() {
dictionary = new String[MAX_WORDS];
numberWordsDictionary = 0;
}
int[] encoder(String[] listofWords) {
int[] wordsEncode = new int[numberWordsDictionary];
StringBuilder builder = new StringBuilder();
for(String word : dictionary) {
builder.append(word);
}
String dictionaryString = builder.toString();
for(int i = 0; i < listofWords.length; i++) {
if(dictionaryString.contains(listofWords[i])) {
wordsEncode[i] = 1;
} else {
wordsEncode[i] = 0;
}
}
return wordsEncode;
}
}
抱歉缩进(与我的 Java IDE 不同):(
谢谢!
解决方法
使用二级嵌套循环,你应该检查dictionary[]
的每个元素是否在listOfWords[]
中,如果是,将wordsEncode[]
中对应索引处的值更新为{ {1}}。
1
输出:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] dictionary = { "Hello","I","am","Lukas","and","Jonas","play","football" };
String[] listOfWords = { "Hello","football" };
int[] wordsEncode = new int[dictionary.length];
for (int i = 0; i < dictionary.length; i++) {
boolean found = false;
for (String s : listOfWords) {
if (s.equals(dictionary[i])) {
found = true;
break;
}
}
if (found) {
wordsEncode[i] = 1;
}
}
// Display the result
System.out.println(Arrays.toString(wordsEncode));
}
}
,
循环输入单词。对于每个输入词,查看您的目标词列表是否包含该特定词。如果是这样,请在结果列表中添加 1。如果不是,请添加零。
我使用更方便的集合,但您可以对数组执行相同的方法。
List< String > input = List.of( "Hello","football" ) ;
List< String > targets = List.of( "Hello","play" "football" ) ;
List< Integers > hits = new ArrayList<>() ;
for( String s : input )
{
int i = targets.contains( s ) ? 1 : 0 ;
hits.add( i ) ;
}
,
您在这里所做的是遍历字典数组并将单词添加到 StringBuilder 以检查您在 listOfWords 数组中获得的某个单词是否在 StringBuilder 中。但是有一个更好的解决方案,您可以创建一个嵌套循环,比较 listOfWords 数组和字典数组的每个元素,如果找到匹配项,则将第二个循环索引处的编码数组值设置为 1:
int[] encoder(String[] listOfWords) {
int[] wordsEncode = new int[numberWordsDictionary];
for (int i = 0; i < listOfWords.length; i++) {
for (int j = 0; j < numberWordsDictionary; j++) {
if (listOfWords[i].equals(dictionary[j])) {
wordsEncode[j] = 1;
break;
}
}
}
return wordsEncode;
}
,
/* This approach is wrong,the combined string could catch words that are
part of the ending of one word and part of the beginning of another but
not actually a word in the dictionary. For instance,if you had
"arch" and "attach" in your dictionary,testing for "chat" would return true
*/
/*
StringBuilder builder = new StringBuilder();
for(String word : dictionary) {
builder.append(word);
}
String dictionaryString = builder.toString();
*/
for(int i = 0; i < listOfWords.length; i++) {
boolean found = false;
for (int j = 0; j < dictionary.length; j++) {
if (dictionary[j].equalslistOfWords[i]) {
found = true;
}
}
if (found) {
wordsEncode[i] = 1;
} else {
wordsEncode[i] = 0;
}
// you can also do wordsEncode[i] = found ? 1 : 0;
}
return wordsEncode;
}