Thursday, 15 September 2011

java - Recursivley return an arrayList of specific indexes -


As the title suggests, I am repeatedly attempting to return the array of student objects whose GPA is more than 3.5. Here's my attempt on this

Public stable Array list and student; Gt; Respected students (student [] list, end n) {ArrayList & lt; Students & gt; Student list = new array list & student; & Gt; (); If (n == 0) {return list of students; } Other {Boolean currentEihanhors = list [N-1] .Ehansers (); If (current ezenhares) {studentsList.add (list [n-1]); Return honor students (list, N-1); } And {return honor students (list, N-1); }}}

Hansers () certainly determines whether the GPA is greater than 3.5 or not. Not sure how exactly I am fixing it.

My method is not returning an empty array list GPA not catching any index with more than 3.5

Any thoughts? Thank you

The problem is that you are creating a new list with each recurring call:

Arreelist & lt; Students & gt; Student list = new array list & student; & Gt; ();

You need to create this list outside of the function:

  Fixed array list & lt; Students & gt; Student list = new array list & student; & Gt; (); Public Stable ArrayList & lt; Students & gt; Respected students (student [] list, end n) {if (n == 0) {return students list; } Other {Boolean currentEihanhors = list [N-1] .Ehansers (); If (current ezoneirs) {studentsList.add (list [N-1]); Return honor students (list, N-1); } And {return honor students (list, N-1); }}}  

No comments:

Post a Comment