我的JAVA命令行菜单出现问题。这就是我所拥有的......我想做的是在评论中。
private void listStudents(){
out.println("\n\tStudent Listing");
//Call datastore method that returns all student names as an array of strings
String[] list = data.getStudents();
//Display all names (use foreach statement)
for (String name : list) {
}
}
这里的数据存储方法我也坚持......
String[] getStudents() {
return (String[]) students.toArray();
}
// Method to return students who match search term
String[] findStudents(String searchTerms) {
// Use foreach loop to visit each item in students ArrayList,
// and if the name matches the search term, add it to a new ArraList.
// Then return the new ArrayList as a string array (see getStudents)
}
答案 0 :(得分:2)
不确定这是否正是您所需要的,但根据我的评论理解,请尝试使用以下内容:
private void listStudents()
{
System.out.println("\n\tStudent Listing");
String[] list = data.getStudents();
// List each student.
for (String name : list)
System.out.println(name);
}
private String[] findStudents(String searchTerms)
{
List<String> studentsFound = new ArrayList<String>();
for (String student : students)
{
// Determine if matching student found.
if (student.equals(searchTerms))
studentsFound.add(student);
}
return studentsFound.toArray(new String[0]);
}
答案 1 :(得分:0)
您尚未定义searchterms
可能包含的内容。这是正则表达式吗?它是通配符吗?
public String[] findStudents(String searchTerms) {
List<String> findList = new ArrayList<String>(25);
for (String student : students) {
// Now you'll need to define how the match works,
// Are you using a regexp or some kind of other matching
// algorithm..
boolean match = ...;
if (match) {
findList.add(student);
}
}
return findList.toArray(new String[findList.size()]);
}