第一题:
public static void main (String[] args){
String[] fileNames = { "abc.txt", "bcd.exe",
"cde.exe","def.dat","efg.exe" };
for (String fileName : fileNames) {
if (fileName.endsWith(".exe")) {
System.out.print(fileName.substring(0, fileName.lastIndexOf(".exe"))+" ");
}
}
}
fileName.endsWith(".exe") 判断是否已".exe"结尾
substring(0,4) 截取 0-3 的字符串,不包含 4
lastIndexOf(".exe") 获取".exe"最后一次出现的位置
答案:bcd cde efg
第二题:
public static void go(Long n) {
System.out.println("Long");
}
public static void go(Short n) {
System.out.println("Short");
}
评论0
最新资源