如果我有一个包含[StudentName,ResigsterNumber,Department,grade,comment]
的列表如何只选择以小写字母开头的单词。即(评分和评论)
答案 0 :(得分:2)
使用Java 8非常简单:
List<String> list = Arrays.asList("StudentName", "Department", "grade", "comment");
List<String> filteredList = list.stream()
.filter(s -> Character.isLowerCase(s.charAt(0)))
.collect(Collectors.toList());
System.out.println(filteredList); // prints "[grade, comment]"
答案 1 :(得分:0)
您可以检查每个单词的第一个字母的ASCII值。小写字母的ASCII值介于97到122之间。
循环遍历列表并将String存储在变量str
int i=str.charAt(0);
if(i>=97 && i<=122){
// this is a word starting with lowercase letter
System.out.println(str)
}
答案 2 :(得分:0)
只需遍历列表并选择1个元素位于'a'
和'z'
之间的字符串
List<String> result=new ArrayList<String>();
for(String str: list){
if(str.charAt(0)>='a' && str.charAt(0)<='z'){
result.add(str);
}
}
答案 3 :(得分:0)
这是打印出最终输出的完整类:
public class Test {
public static void main(String[] args) {
String[] testNames = new String[] { "StudentName", "ResigsterNumber", "Department", "grade", "comment" };
List<String> names = new ArrayList<>();
Arrays.asList(testNames).forEach((testCh) -> {
if (testCh != null && !testCh.isEmpty() && Character.isLowerCase(testCh.charAt(0))) {
names.add(testCh);
}
});
System.out.println(names);
}
}
答案 4 :(得分:0)
只需循环遍历元素并将它们置于以下条件:
boolean hasLowerCase = element.equals(element.toLowerCase());
这将允许您检查:
if(hasLowerCase){System.out.println(Element is in lowercase)};
if(!hasLowerCase){System.out.println(Element is not in lowercase)};
答案 5 :(得分:0)
for (String l : list){
char character = l.charAt(0); // This gives the first character
if (Character.isLowerCase(character)){
// proceed with list element with small letter found
}
}