recursive java

package com.testcases;

import java.util.ArrayList;

/**
 * Created by shiyanghuang on 16/3/8.
 */
public class recursive {

    static int MAX_LEVEL = 5;
    static ArrayList> data = new ArrayList>();

    public void rc(ArrayList result, int level, ArrayList answer) {
        if (level >= MAX_LEVEL) {
            ArrayList clone = new ArrayList(result);
            answer.add(clone);
            return;
        } else {
            for (int i = 0; i < data.get(level).size(); i++) {
                result.add(data.get(level).get(i));
                rc(result, level + 1, answer);
                result.remove(result.size() - 1);
            }
        }
    }

    public static void printArray(ArrayList> result) {
        for (int i = 0; i < result.size(); i++) {
            for (int j = 0; j < result.get(i).size(); j++) {
                System.out.print(result.get(i).get(j) + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        ArrayList> answer = new ArrayList>();
        ArrayList line = new ArrayList();
        for (int j = 0; j < MAX_LEVEL; j++) {
            for (int i = 0; i < 4; i++) {
                line.add(i);
            }
            data.add(line);
            line = new ArrayList();
        }

        System.out.println("Data:");
        printArray(data);

        ArrayList> result = new ArrayList>();

        new recursive().rc(result, 0, answer);
        
        System.out.println("Answer");
        printArray(answer);
    }
}

One Response so far.

  1. WONDERFUL Post.thanks for share..extra wait .. …

LEAVE A COMMENT