자바-재 귀 실현 조합

3321 단어 Java
수요:하나의 모델 에 n 개의 상태 가 있 고 모든 상태 에 m 개의 옵션 이 있 습 니 다.모든 상태 에서 하나의 옵션 을 선택 하여 조합 하면 모두 몇 가지 조합 이 있 습 니까?
1.State 클래스
public class State {

	/**      */
	private List selections = new ArrayList();
	/**      */
	private String mName;

	public State(String name) {

		this.mName = name;
	}

	public State() {

	}

	public String getName() {
		return mName;
	}

	public void setName(String mName) {
		this.mName = mName;
	}

	public void addSelection(String selection) {

		selections.add(selection);
	}

	public String getSelection(int index) {

		return this.selections.get(index);
	}

	public int selectionNum() {

		return this.selections.size();
	}

	public List getSelections() {
		return selections;
	}

}

2 조합 류,Cmn
public class Cmn {
	
	
	private List states = new ArrayList();
	
	/**
	 *      
	 */
	public Cmn(){
		
		State state1 = new State("State1");
		state1.addSelection("State1_Selection1");
		state1.addSelection("State1_Selection2");
		State state2 = new State("State2");
		state2.addSelection("State2_Selection1");
		State state3 = new State("State3");
		state3.addSelection("State3_Selection1");
		State state4 = new State("State4");
		state4.addSelection("State4_Selection1");
		state4.addSelection("State4_Selection2");
		state4.addSelection("State4_Selection3");
		addState(state1);
		addState(state2);
		addState(state3);
		addState(state4);
	}
	
	private void addState(State state){
		this.states.add(state);
	}

	/**
	 *     
	 */
	public void cmn(){
		System.out.println("    ");
		printState(this.states);
		System.out.println("    ");
		State state = new State("CMN");
		int index = 0;
		cmn(state,index);
		printState(this.mPxStates);
		
	}
	
	/**      */
	List mPxStates = new ArrayList();
	
	/**
	 *         
	 * @param state
	 * @param index
	 */
	public void cmn(State state,int index){
		
		State stateT = this.states.get(index);
		List selections = stateT.getSelections();
		int sNum = selections.size();
		for (int i = 0; i < sNum; i++) {
			
			String selection = selections.get(i);
			State state2 = new State(); //     state       selection
			state2.getSelections().addAll(state.getSelections());
			state2.addSelection(selection);
			
			if(index+1 == this.states.size()){//        
				state2.setName("CMN "+(mPxStates.size()+1));
				mPxStates.add(state2);
			}else{//   
				int index2 = index+1;
				cmn(state2,index2);
			}
			
		}
	}
	
	public void printState(List states){
		
		int num = states.size();
		for (int i = 0; i < num; i++) {
			
			State state = states.get(i);
			List selections = state.getSelections();
			System.out.println("----------"+state.getName()+"-----------");
			int num2= selections.size();
			for (int j = 0; j < num2; j++) {
				String selection = state.getSelection(j);
				System.out.println(j+"----->"+selection);
			}
			System.out.println();
		}
	}

}
3 Main
public class Main {
	
	public static void main(String[] args){
		
		new Cmn().cmn();
	}

}

좋은 웹페이지 즐겨찾기