我如何从以指定字母开头的列表中获取所有单词
How would I get all the words from a list that begins with the specified letter
我正在尝试显示以用户输入指定的字母开头的单词列表。
因此,例如,如果我在列表中添加三个词,cat、corn 和 dog,并且用户输入字母 c,Java applet 上的输出应该是 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
您可以遍历所有可能的索引,检查该索引处的元素是否以该字母开头,如果是则打印它。
替代(可能更好)代码(我本来打算把它放在后面,但因为它更好,所以应该放在第一位。采用@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);
}
}
你可以试试这样的 -
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]
如果您可以使用 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
参数的字符串开头的每个列表项。
输出为:
[cat, corn]
我正在尝试显示以用户输入指定的字母开头的单词列表。
因此,例如,如果我在列表中添加三个词,cat、corn 和 dog,并且用户输入字母 c,Java applet 上的输出应该是 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
您可以遍历所有可能的索引,检查该索引处的元素是否以该字母开头,如果是则打印它。
替代(可能更好)代码(我本来打算把它放在后面,但因为它更好,所以应该放在第一位。采用@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);
}
}
你可以试试这样的 -
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]
如果您可以使用 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
参数的字符串开头的每个列表项。
输出为:
[cat, corn]