본문 바로가기
일기장 - 아둘파

[java] For문 순회 하며 경우의 수 만들기

by 아둘파 2010. 11. 3.


import java.util.*;


public class Want {

 public void startLogic(int colNums, List alist, List blist, List clist, List dlist, List elist){
  
  int a, b, c, d, e;
  
  a = alist.size();
  b = blist.size();
  c = clist.size();
  d = dlist.size();
  e = elist.size();
  
  // for 문 순회를 위해 0이면 1을 배정 (트릭)
  if(a==0){a=1;}
  if(b==0){b=1;}
  if(c==0){c=1;}
  if(d==0){d=1;}
  if(e==0){e=1;}
  
  for(int i=0; i <a ; i++){
   for(int j=0; j < b; j++){
    for(int k=0; k<c; k++){
     for(int l=0; l<d; l++){
      for(int m=0; m<e; m++){
       if(colNums==1){
        
       }else if(colNums==1){
        System.out.println(alist.get(i));
       }else if(colNums==2){
        System.out.println("컬럼_1 : " + alist.get(i) + " // "
             + "컬럼_2 : " + blist.get(j)
        );
       }else if(colNums==3){
        System.out.println("컬럼_1 : " + alist.get(i) + " // "
           + "컬럼_2 : " + blist.get(j) + " // "
           + "컬럼_3 : " + clist.get(k)
        );
       }else if(colNums==4){
        System.out.println("컬럼_1 : " + alist.get(i) + " // "
           + "컬럼_2 : " + blist.get(j) + " // "
           + "컬럼_3 : " + clist.get(k) + " // "
           + "컬럼_4 : " + dlist.get(l)
        );
       }else if(colNums==5){
        System.out.println("컬럼_1 : " + alist.get(i) + " // "
           + "컬럼_2 : " + blist.get(j) + " // "
           + "컬럼_3 : " + clist.get(k) + " // "
           + "컬럼_4 : " + dlist.get(l) + " // "
           + "컬럼_5 : " + dlist.get(m)
        );
       }
      }
     }
    }
   }
  }
  
 }
 
 
}

import java.util.*;


public class Tester {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // 컬럼이 3개이고 각 컬럼당 2,3,4 개의 데이터가 있다고 가정.
  
  int colNums = 3;
  List alist = new ArrayList();
  List blist = new ArrayList();
  List clist = new ArrayList();
  List dlist = new ArrayList();
  List elist = new ArrayList();
  
  alist.add("A1");
  alist.add("A2");
  
  blist.add("B1");
  blist.add("B2");
  blist.add("B3");
  
  clist.add("C1");
  clist.add("C2");
  clist.add("C3");
  clist.add("C4");

  Want service = new Want();

  service.startLogic(colNums, alist, blist, clist, dlist, elist);
  
 }

}

댓글