帮忙编写一个JAVA程序!

2024-11-29 23:21:38
推荐回答(2个)
回答1:

import java.util.LinkedList;
import java.util.List;

public class Main {
public static void main(String[] args) {
List list = new LinkedList() {
{
add("John Alex");
add("Miller Scott");
add("John Anna");
add("Johnson Jack");
add("Hunter JeFF");
add("Williams Serena");
add("Williams Venus");
}
};
// 显示所有
System.out.println(list);
// 寻找John开头的
// 如果Johnson Jack不算的话,regex改成"^John .*"(即点之前加空格)
String regex = "^John.*";
for (String str : list) {
if (str.matches(regex))
System.out.println(str);
}
// 随机排列
java.util.Collections.shuffle(list);
System.out.println(list);
}

}

回答2:

public static void main(String[] args){
String kk = "John Alex,Miller Scott,John Anna,Johnson Jack,Hunter JeFF,Williams Serena,Williams Venus";
String[] kks = kk.split(",");
LinkedList list = new LinkedList();
list.addAll(Arrays.asList(kks));
System.out.println(list);
for(int i=0;i String value = (String)list.get(i);
if(value.indexOf("John")>-1){
System.out.println(value);
}
}
}