java - Writing a recursive method which returns the even indexes of an arraylist -
i need write recursive method returns indexes of arraylist, predefined list of consecutive numbers [1, 2, 3, 4, 5, 6...].
the problem if come in 10 numbers printed receive "2, 4, 6, 8, 10" wrong (as need indexes), whereas if come in odd number, such 9, output "1, 3, 5, 7, 9".
//recursive method public static arraylist<integer> even(arraylist<integer> list) { arraylist<integer> newlist = listmethods.deepclone(list); if (newlist.size() <= 1) { homecoming newlist; } else { int temp = newlist.remove(0); newlist.remove(0); newlist = listmethods.even(newlist); newlist.add(temp); } //main runner arraylist<integer> newlist = listmethods.even(templist); if (list.size() == 0) { system.out.println("the list empty"); } else { (integer : newlist) { system.out.println(i + ","); } }
java recursion arraylist indexing
No comments:
Post a Comment