gpt4 book ai didi

java - For Each Loop 返回搜索字符串

转载 作者:行者123 更新时间:2023-12-02 07:39:50 26 4
gpt4 key购买 nike

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

最佳答案

不确定这是否正是您所需要的,但根据我从您的评论中了解到的内容,请尝试使用以下内容:

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]);
}

关于java - For Each Loop 返回搜索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11751490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com