把集合中以“API”结尾的元素查找出来并组成一个新的集合
public class Demo3 {
public static void main(String[] args) {
// 把features集合中以“API”结尾的元素查找出来并组成一个新的集合
List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Stream API", "Date and Time API");
ArrayList<String> list = new ArrayList<String>();
for (String strs : features) {
char[] cs2 = strs.toCharArray();
if (cs2[cs2.length-1]=='I'&&cs2[cs2.length-2]=='P'&&cs2[cs2.length-3]=='A'){
list.add(strs);
}
}
for (String str:list
) {
System.out.println(str);
}
}
}
本文介绍了一个Java程序示例,该程序从一个包含多个字符串的列表中筛选出以API结尾的字符串,并将这些字符串组成一个新的列表。通过遍历原始列表并检查每个字符串的最后三个字符来实现筛选。

被折叠的 条评论
为什么被折叠?



