递归 return 特定索引的 arrayList
Recursivley return an arrayList of specific indexes
如标题所示,我正在尝试递归 return 一个 gpa 高于 3.5 的学生 objects 的数组列表。这是我的尝试。
public static ArrayList<Student> honorsStudents(Student[] list, int n) {
ArrayList<Student> studentsList = new ArrayList<Student>();
if (n == 0) {
return studentsList;
} else {
boolean currentIsHonors = list[n - 1].isHonors();
if (currentIsHonors) {
studentsList.add(list[n - 1]);
return honorsStudents(list, n - 1);
} else {
return honorsStudents(list, n - 1);
}
}
}
isHonors()当然是判断gpa是否大于3.5。
不确定我到底在哪里搞砸了。
我的方法不是 return空数组列表。不捕获 GPA 大于 3.5 的任何索引。
有什么想法吗?谢谢
问题是您在每次递归调用时都创建了一个新列表:
ArrayList<Student> studentsList = new ArrayList<Student>();
您需要在函数之外创建此列表:
static ArrayList<Student> studentsList = new ArrayList<Student>();
public static ArrayList<Student> honorsStudents(Student[] list, int n)
{
if (n==0)
{
return studentsList;
}
else
{
boolean currentIsHonors = list[n-1].isHonors();
if(currentIsHonors)
{
studentsList.add(list[n-1]);
return honorsStudents(list, n-1);
}
else
{
return honorsStudents(list, n-1);
}
}
}
您正在每个方法迭代中创建一个新的 ArrayList
。这永远不会递归地工作,因为您需要将元素添加到 same 列表。
考虑使用一个使用空白列表开始递归的基本方法,然后为递归的每次迭代传递 相同 列表:
//This method takes in the initial values and starts the actual recursion
public static ArrayList<Student> honorsStudents(Student[] list, int n)
{
return honorStudents(list, n, new ArrayList<Student>());
}
//this is the actual recursive method
public static ArrayList<Student> honorsStudents(Student[] list, int n, List<Student> studentsList)
{
if (n==0)
{
return studentsList;
}
else
{
boolean currentIsHonors = list[n-1].isHonors();
if(currentIsHonors)
{
studentsList.add(list[n-1]);
return honorsStudents(list, n-1, studentsList);
}
else
{
return honorsStudents(list, n-1, studentsList);
}
}
}
你可以使用这段代码,我认为你不需要递归方法
public static ArrayList<Student> honorsStudents(Student[] list, int n) {
ArrayList<Student> studentsList = new ArrayList<Student>();
if (n==0)
{
System.out.println("END");
return studentsList;
}
for(Student s: list{
if(s.isHonors()){
studentsList.add(s);
}
}
return studentsList; //all students with isHonors == true
}
如标题所示,我正在尝试递归 return 一个 gpa 高于 3.5 的学生 objects 的数组列表。这是我的尝试。
public static ArrayList<Student> honorsStudents(Student[] list, int n) {
ArrayList<Student> studentsList = new ArrayList<Student>();
if (n == 0) {
return studentsList;
} else {
boolean currentIsHonors = list[n - 1].isHonors();
if (currentIsHonors) {
studentsList.add(list[n - 1]);
return honorsStudents(list, n - 1);
} else {
return honorsStudents(list, n - 1);
}
}
}
isHonors()当然是判断gpa是否大于3.5。 不确定我到底在哪里搞砸了。
我的方法不是 return空数组列表。不捕获 GPA 大于 3.5 的任何索引。
有什么想法吗?谢谢
问题是您在每次递归调用时都创建了一个新列表:
ArrayList<Student> studentsList = new ArrayList<Student>();
您需要在函数之外创建此列表:
static ArrayList<Student> studentsList = new ArrayList<Student>();
public static ArrayList<Student> honorsStudents(Student[] list, int n)
{
if (n==0)
{
return studentsList;
}
else
{
boolean currentIsHonors = list[n-1].isHonors();
if(currentIsHonors)
{
studentsList.add(list[n-1]);
return honorsStudents(list, n-1);
}
else
{
return honorsStudents(list, n-1);
}
}
}
您正在每个方法迭代中创建一个新的 ArrayList
。这永远不会递归地工作,因为您需要将元素添加到 same 列表。
考虑使用一个使用空白列表开始递归的基本方法,然后为递归的每次迭代传递 相同 列表:
//This method takes in the initial values and starts the actual recursion
public static ArrayList<Student> honorsStudents(Student[] list, int n)
{
return honorStudents(list, n, new ArrayList<Student>());
}
//this is the actual recursive method
public static ArrayList<Student> honorsStudents(Student[] list, int n, List<Student> studentsList)
{
if (n==0)
{
return studentsList;
}
else
{
boolean currentIsHonors = list[n-1].isHonors();
if(currentIsHonors)
{
studentsList.add(list[n-1]);
return honorsStudents(list, n-1, studentsList);
}
else
{
return honorsStudents(list, n-1, studentsList);
}
}
}
你可以使用这段代码,我认为你不需要递归方法
public static ArrayList<Student> honorsStudents(Student[] list, int n) {
ArrayList<Student> studentsList = new ArrayList<Student>();
if (n==0)
{
System.out.println("END");
return studentsList;
}
for(Student s: list{
if(s.isHonors()){
studentsList.add(s);
}
}
return studentsList; //all students with isHonors == true
}