【问题标题】:Encoding a String Array编码字符串数组
【发布时间】:2021-04-20 23:58:58
【问题描述】:

编辑:[学校作业]

所以,我想只用 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};

您可以看到“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 不同):(
谢谢!

【问题讨论】:

  • 如果输入的是{"football", "play", "Hello"};怎么办?
  • 如果这是学校作业,你应该说做。这会影响什么样的答案是合适的。
  • 你当然应该按照作业的要求去做。只是让你知道,这不是人们在现实生活中会使用的数据结构。 Anti-pattern: parallel collections.

标签: java arrays string encode


【解决方案1】:
/* 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;
}

【讨论】:

    【解决方案2】:

    循环输入单词。对于每个输入单词,查看您的目标单词列表是否包含该特定单词。如果是这样,请将 1 添加到您的结果列表中。如果不是,则加零。

    我使用更方便的集合,但您可以对数组执行相同的方法。

    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 ) ;
    }
    

    【讨论】:

    • 我认为@OP 在一个类中,可能不能使用列表。问题是关于添加第二个(内部)循环
    • @ControlAltDel 如果这是为了功课,作者应该在问题中说做。读懂他们的想法不是我的工作。
    【解决方案3】:

    使用两级嵌套循环,您应该检查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]
    

    【讨论】:

      【解决方案4】:

      您在这里所做的是遍历字典数组并将单词添加到 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-11
        • 2015-03-15
        • 2013-12-29
        • 1970-01-01
        • 2020-07-17
        • 2011-10-02
        相关资源
        最近更新 更多