加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 通过传入char变量从列表中打印所有字符串

发布时间:2020-12-15 08:26:47 所属栏目:Java 来源:网络整理
导读:我需要通过传入char变量来打印列表中的所有字符串. 当我有一些例如喜欢的东西时,我传递了char p public static void printStrings(ListString words,char p){words.stream().filter(i - i.startsWith(p)) .sorted().forEach(System.out::println);} 我将如何
我需要通过传入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)))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读