我需要通过传入一个 char 变量来打印列表中的所有字符串。
当我有类似的东西时,我会传入 char p
public static void printStrings(List<String> words, char p)
{
words.stream()
.filter(i -> i.startsWith(p))
.sorted()
.forEach(System.out::println);
}
我将如何传递 char 变量,因为我不断收到一个错误提示
error: incompatible types: char cannot be converted to String
那么我怎样才能在不出错的情况下传递一个 char 变量呢?
最佳答案
String::startsWith
只能接受 String
作为参数。
尝试
.filter(i -> i.startsWith(Character.toString(p)))
关于java - 通过传入 char 变量打印列表中的所有字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51577300/