import java.util.ArrayList; public class AB { public ArrayList rAccmulate(int count) { if (count <= 0) return new ArrayList(); ArrayList alAcc = rAccmulate(count - 1); alAcc.add(count); return alAcc; } public ArrayList rAccmulateB(int count) { ArrayList ret = new ArrayList<>(count); rAccumulateUtil(count, ret); return ret; } private void rAccumulateUtil(int count, ArrayList arrLis) { if (count <= 0) return; arrLis.add(count); rAccumulateUtil(count - 1, arrLis); } public static void main(String[] args) { System.out.println("AA " + (new AB()).rAccmulate(5)); System.out.println("BB " + (new AB()).rAccmulateB(5)); } }