To make the recursive function , each time it is called, add the value of the first element on the map and then delete it.
If the card is empty, it returns 11
: 9+7+5+3+1+11 = 36 (9,7,5,3,1 11, )
, ( )
( , ) :
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class ArrayList2009 {
public static void main( String [] args ) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(3);
list.add(5);
list.add(7);
list.add(9);
System.out.println( addOne( list ) );
}
private static int addOne( List<Integer> list ){
if ( list.isEmpty() ) {
return 11;
} else {
Iterator<Integer> i = list.iterator();
int num = i.next();
i.remove();
return num + addOne( list );
}
}
}
, , List .