编码字符串数组
Encoding a String Array
编辑:[作业]
所以,我想只用 0 和 1 来编码单词。
0 = 单词不存在
1 = 单词出现
我的字典对应:
String[] dictionary = {"Hello", "I", "am", "Lukas", "and", "Jonas", "play", "football"};
例如:如果我对这些词进行编码...
String[] listOfWords = {"Hello", "play" "football"};
我必须有以下数组:
int[] wordsEncode = {1,0,0,0,0,0,1,1};
你可以看到“你好”存在,“我”“是”“卢卡斯”“和”“乔纳斯”不存在。最后,“玩”和“足球”出现了。
我们必须保留字典的顺序,这是我的代码中的问题。
我真的不知道如何解决这个问题(使用第二个 for 循环?)?
我认为 wordEncode[i] 是我的 error,但如何解决?
这是我的代码:
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 不一样):(
谢谢!
/* 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;
}
循环输入单词。对于每个输入词,查看您的目标词列表是否包含该特定词。如果是这样,请在结果列表中添加 1。如果不是,请加零。
我使用更方便的 Collections,但您可以对数组执行相同的方法。
List< String > input = List.of( "Hello", "I", "am", "Lukas", "and", "Jonas", "play", "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 ) ;
}
使用两级嵌套循环,你应该检查dictionary[]
的每个元素是否在listOfWords[]
中,如果是,更新wordsEncode[]
中相应索引处的值至 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", "play", "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, 0, 0, 0, 0, 0, 1, 1]
你在这里所做的是遍历字典数组并将单词添加到 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;
}
编辑:[作业]
所以,我想只用 0 和 1 来编码单词。
0 = 单词不存在
1 = 单词出现
我的字典对应:
String[] dictionary = {"Hello", "I", "am", "Lukas", "and", "Jonas", "play", "football"};
例如:如果我对这些词进行编码...
String[] listOfWords = {"Hello", "play" "football"};
我必须有以下数组:
int[] wordsEncode = {1,0,0,0,0,0,1,1};
你可以看到“你好”存在,“我”“是”“卢卡斯”“和”“乔纳斯”不存在。最后,“玩”和“足球”出现了。
我们必须保留字典的顺序,这是我的代码中的问题。
我真的不知道如何解决这个问题(使用第二个 for 循环?)?
我认为 wordEncode[i] 是我的 error,但如何解决?
这是我的代码:
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 不一样):(
谢谢!
/* 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;
}
循环输入单词。对于每个输入词,查看您的目标词列表是否包含该特定词。如果是这样,请在结果列表中添加 1。如果不是,请加零。
我使用更方便的 Collections,但您可以对数组执行相同的方法。
List< String > input = List.of( "Hello", "I", "am", "Lukas", "and", "Jonas", "play", "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 ) ;
}
使用两级嵌套循环,你应该检查dictionary[]
的每个元素是否在listOfWords[]
中,如果是,更新wordsEncode[]
中相应索引处的值至 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", "play", "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, 0, 0, 0, 0, 0, 1, 1]
你在这里所做的是遍历字典数组并将单词添加到 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;
}