简体   繁体   中英

How would I get all the words from a list that begins with the specified letter

I am trying to show the list of words which start with the letter specified by the user input.

So for example if I add three words to my list, cat, corn and dog, and the user inputs the letter c, the output on the Java applet should be cat, corn.

However, I have no idea on how to go about this.

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

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

b1 is adding all the user input into a secretly stored list, and I now want to make another button when pressed to show the words that start with the specified letter by the user.

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

You could loop through all the possible indices, check if the element at that index starts with the letter, and print it if it does.

ALTERNATIVE (and probably better) code (I was going to put this after, but since its better it deserves to be first. Taken form @larsmans's answer 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);
    }
}

DISCLAIMER : This code is untested, I don't have much experience with ArrayList , and Java is more of a quaternary programming language for me. Hope it works :)

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

You can try something like this -

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

Result is -

[aah, abh]

If you can use Java 8 then you can build in features to filter your list:

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

This uses the filter method to filter each list item which starts with the String of the prefix argument.

The output is:

[cat, corn]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM