【问题标题】:How would I get all the words from a list that begins with the specified letter我如何从以指定字母开头的列表中获取所有单词
【发布时间】:2016-02-03 10:35:57
【问题描述】:

我正在尝试显示以用户输入指定的字母开头的单词列表。

例如,如果我在列表中添加三个单词 cat、corn 和 dog,并且用户输入字母 c,Java 小程序上的输出应该是 cat、corn。

但是,我不知道该怎么做。

public void actionPerformed(ActionEvent e){
    if (e.getSource() == b1 ){
        x = textf.getText();
        wordList.add(x);
        textf.setText(null);
    } 

    if (e.getSource() == b2 ){
    }
}

b1 正在将所有用户输入添加到一个秘密存储的列表中,我现在想在按下时创建另一个按钮以显示以用户指定字母开头的单词。

textf = my text field
wordList = my list I created
x = string I previously defined 

【问题讨论】:

  • 如何声明wordList
  • List wordList = new ArrayList ();

标签: java input applet


【解决方案1】:

如果您可以使用 Java 8,那么您可以内置功能来过滤您的列表:

public static void main(String[] args) throws Exception {
    final List<String> list = new ArrayList<>();
    list.add("cat");
    list.add("corn");
    list.add("dog");
    System.out.println(filter(list, "c"));
}

private static List<String> filter(final Collection<String> source, final String prefix) {
    return source.stream().filter(item -> item.startsWith(prefix)).collect(Collectors.toList());
}

这使用filter 方法过滤每个以prefix 参数的字符串开头的列表项。

输出是:

[猫,玉米]

【讨论】:

    【解决方案2】:

    你可以试试这样的 -

    public static void main(String[] args) {
            String prefix = "a";
            List<String> l = new ArrayList<String>();
            List<String> result = new ArrayList<String>();
            l.add("aah");
            l.add("abh");
            l.add("bah");
    
            for(String s: l) {
                if(s.startsWith(prefix)) {
                    result.add(s);
                }
            }
    
            System.out.println(result);
       }
    

    结果是 -

    [aah, abh]
    

    【讨论】:

      【解决方案3】:

      您可以遍历所有可能的索引,检查该索引处的元素是否以字母开头,如果是则打印它。

      替代(可能更好)代码(我本来打算把它放在后面,但因为它更好,所以它应该是第一个。取自@larsmans 的答案here

      //given wordList as the word list
      //given startChar as the character to search for in the form of a *String* not char
      for (String element : wordList){
          if (element.startsWith(startChar)){
              System.out.println(element);
          }
      }
      

      免责声明:此代码未经测试,我对ArrayList 没有太多经验,Java 对我来说更像是一种四元编程语言。希望它有效:)

      //given same variables as before
      for (int i = 0; i < wordList.size(); i++){
          String element = wordList.get(i);
          //you could remove the temporary variable and replace element with
          //  wordList.get(i)
          if (element.startsWith(startChar){
              System.out.println(element);
          }
      }
      

      【讨论】:

      • 这实际上会在小程序中打印出来吗,因为这是我遇到的主要问题
      • 好吧,我真的不使用小程序,所以你必须自己找出来。您可以创建一个新文本字段并向其中添加单词。我很确定System.out.println 适用于小程序。你总是可以尝试。
      • 是的,我会找到一种方法,因为没有打印,但我知道代码是正确的,因为我之前做过类似的事情,但什么也没发生。感谢您的帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      相关资源
      最近更新 更多