0

我的 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)
}
4

2 回答 2

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]);
}
于 2012-08-01T01:55:01.527 回答
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()]);
}
于 2012-08-01T01:46:44.187 回答