Java Recursivley返回特定索引的arrayList

Java Recursivley返回特定索引的arrayList,java,recursion,arraylist,Java,Recursion,Arraylist,正如标题所示,我试图递归返回gpa高于3.5的Student对象的arrayList。这是我的尝试 public static ArrayList<Student> honorsStudents(Student[] list, int n) { ArrayList<Student> studentsList = new ArrayList<Student>(); if (n == 0) { return studentsList

正如标题所示,我试图递归返回gpa高于3.5的Student对象的arrayList。这是我的尝试

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);
        }
    }
}
公共静态数组列表荣誉学生(学生[]列表,int n){
ArrayList studentsList=新建ArrayList();
如果(n==0){
返回学生名单;
}否则{
布尔值currentIsHonors=list[n-1].isHonors();
if(CurrentIsOnors){
studentsList.add(列表[n-1]);
返回荣誉学生(名单,n-1);
}否则{
返回荣誉学生(名单,n-1);
}
}
}
当然,isHonors()确定gpa是否大于3.5。 不知道我到底在哪里搞砸了

我的方法没有返回空的arrayList。不捕获GPA大于3.5的任何索引


有什么想法吗?谢谢

问题在于,每次递归调用都会创建一个新列表:

 ArrayList<Student> studentsList = new ArrayList<Student>();
ArrayList studentsList=new ArrayList();
您需要在函数之外创建此列表:

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);
        }
    }
}
static ArrayList studentsList=new ArrayList();
公共静态数组列表荣誉学生(学生[]列表,int n)
{
如果(n==0)
{
返回学生名单;
}       
其他的
{
布尔值currentIsHonors=list[n-1].isHonors();
if(CurrentIsOnors)
{
学生列表添加(列表[n-1]);
返回荣誉学生(名单,n-1);
}
其他的
{
返回荣誉学生(名单,n-1);
}
}
}

问题在于,每次递归调用都会创建一个新列表:

 ArrayList<Student> studentsList = new ArrayList<Student>();
ArrayList studentsList=new ArrayList();
您需要在函数之外创建此列表:

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);
        }
    }
}
static ArrayList studentsList=new ArrayList();
公共静态数组列表荣誉学生(学生[]列表,int n)
{
如果(n==0)
{
返回学生名单;
}       
其他的
{
布尔值currentIsHonors=list[n-1].isHonors();
if(CurrentIsOnors)
{
学生列表添加(列表[n-1]);
返回荣誉学生(名单,n-1);
}
其他的
{
返回荣誉学生(名单,n-1);
}
}
}

您正在每个方法迭代中创建一个新的
ArrayList
。这永远不会递归地工作,因为您需要将元素添加到同一个列表中

考虑使用一个基本方法,使用一个空白列表开始递归,然后为递归的每次迭代传递相同的列表:

//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);
        }
    }
}
//此方法接受初始值并开始实际递归
公共静态数组列表荣誉学生(学生[]列表,int n)
{
返回荣誉学生(列表,n,新ArrayList());
}
//这是实际的递归方法
公共静态数组列表荣誉学生(学生[]列表,int n,列表学生列表)
{
如果(n==0)
{
返回学生名单;
}       
其他的
{
布尔值currentIsHonors=list[n-1].isHonors();
if(CurrentIsOnors)
{
学生列表添加(列表[n-1]);
返回荣誉学生(名单,n-1,学生名单);
}
其他的
{
返回荣誉学生(名单,n-1,学生名单);
}
}
}

您正在每个方法迭代中创建一个新的
ArrayList
。这永远不会递归地工作,因为您需要将元素添加到同一个列表中

考虑使用一个基本方法,使用一个空白列表开始递归,然后为递归的每次迭代传递相同的列表:

//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);
        }
    }
}
//此方法接受初始值并开始实际递归
公共静态数组列表荣誉学生(学生[]列表,int n)
{
返回荣誉学生(列表,n,新ArrayList());
}
//这是实际的递归方法
公共静态数组列表荣誉学生(学生[]列表,int n,列表学生列表)
{
如果(n==0)
{
返回学生名单;
}       
其他的
{
布尔值currentIsHonors=list[n-1].isHonors();
if(CurrentIsOnors)
{
学生列表添加(列表[n-1]);
返回荣誉学生(名单,n-1,学生名单);
}
其他的
{
返回荣誉学生(名单,n-1,学生名单);
}
}
}

您可以使用这段代码,我认为您不需要递归方法

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
    }
公共静态数组列表荣誉学生(学生[]列表,int n){
ArrayList studentsList=新建ArrayList();
如果(n==0)
{
系统输出打印项次(“结束”);
返回学生名单;
}       
学生名单{
if(s.isHonors()){
学生名单。添加(s);
}
}
return studentsList;//所有isHonors==true的学生
}

您可以使用这段代码,我认为您不需要递归方法

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
    }
公共静态数组列表荣誉学生(学生[]列表,int n){
ArrayList studentsList=新建ArrayList();
如果(n==0)
{
系统输出打印项次(“结束”);
返回学生名单;
}       
学生名单{
if(s.isHonors()){
学生名单。添加(s);
}
}
return studentsList;//所有isHonors==true的学生
}

嗯,我的方法不是返回GPA大于3.5的student对象的arraylist。我会在我的帖子中澄清。@tnwd它必须是递归的吗?好的,那么它返回什么?这似乎不必要的复杂。为什么不为循环执行一个简单的
?是的,它必须是递归的@kwiknessIt返回一个空的arraylist,带有大小为0。这是一个我一直头痛不已的类赋值。我不明白我的逻辑是怎么错的,它必须是递归的。@tnwuh,我的方法没有返回带有gpas gr的学生对象的arraylist