티스토리 뷰

프로그래밍/Java

재귀호출

안싱미 2016. 3. 17. 13:23

재귀호출

게시판에서 메뉴를 구성할 때, 카테고리별로 보여줘야하는 경우가 있다.

그때는 재귀호출을 이용하여야 한다.


유투브를 예제로 재귀호출을 실습해보자.

아래 그림처럼 유투브 아래에 있는 모든 카테고리를 출력하는 코드를 짜볼 것이다. 

프로젝트 구성도는 다음과 같다.


Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.util.ArrayList;
import java.util.List;
 
public class Main {
       public static void main(String[] args) {
              
              ItemVO youtube = new ItemVO();
              youtube.setName("유튜브");
              youtube.setDepth(0);
              youtube.setCategory(true);
              
              ItemList subCategory1 = new ItemList();
              List<ItemVO> items = new ArrayList<ItemVO>();
              ItemVO game = new ItemVO();
              game.setName("Game");
              game.setDepth(1);
              game.setCategory(true);
              items.add(game);
              
              ItemVO music = new ItemVO();
              music.setName("Music");
              music.setDepth(1);
              music.setCategory(true);
              items.add(music);
              subCategory1.setItems(items);
              youtube.setItemList(subCategory1);
              
              ItemList gameCategory = new ItemList();
              List<ItemVO> gameItem = new ArrayList<ItemVO>();
              ItemVO liveAnnounce = new ItemVO();
              liveAnnounce.setName("실황");
              liveAnnounce.setDepth(2);
              liveAnnounce.setCategory(true);
              gameItem.add(liveAnnounce);
              
              ItemVO trailer = new ItemVO();
              trailer.setName("트레일러");
              trailer.setDepth(2);
              trailer.setCategory(true);
              gameItem.add(trailer);
              
              gameCategory.setItems(gameItem);
              game.setItemList(gameCategory);
              
              ItemList liveAnnouncerList = new ItemList();
              List<ItemVO> liveAnnList = new ArrayList<ItemVO>();
              ItemVO daedo = new ItemVO();
              daedo.setName("대도서관");
              daedo.setDepth(3);
              daedo.setCategory(false);
              liveAnnList.add(daedo);
              
              ItemVO djl = new ItemVO();
              djl.setName("대정령");
              djl.setDepth(3);
              djl.setCategory(false);
              liveAnnList.add(djl);
              liveAnnouncerList.setItems(liveAnnList);
              liveAnnounce.setItemList(liveAnnouncerList);
              
              ItemList trailerItemList = new ItemList();
              List<ItemVO> trailerList = new ArrayList<ItemVO>();
              ItemVO e3 = new ItemVO();
              e3.setName("E3");
              e3.setDepth(3);
              e3.setCategory(false);
              trailerList.add(e3);
              
              ItemVO production = new ItemVO();
              production.setName("제작사");
              production.setDepth(3);
              production.setCategory(false);
              trailerList.add(production);
              
              trailerItemList.setItems(trailerList);
              trailer.setItemList(trailerItemList);
              
              printTree(youtube);
              
           }
           
           private static void printTree(ItemVO item) {
              
              for ( int i = 0; i<item.getDepth(); i++) {
                 System.out.print("\t");
              }
              
              System.out.println(item.getName());
              
              if ( item.isCategory() ) {
                 if ( item.getItemList() !=  null) {
                    List<ItemVO> items = item.getItemList().getItems();
                    
                    for (ItemVO itemVO : items) {
                       printTree(itemVO);
                    }
                 }
              }
           }
 
 
}
 
cs


ItemVO.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class ItemVO {
 
    private String name;
    private boolean isCategory;
    private int depth;
    
    private ItemList itemList;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public boolean isCategory() {
        return isCategory;
    }
 
    public void setCategory(boolean isCategory) {
        this.isCategory = isCategory;
    }
 
    public int getDepth() {
        return depth;
    }
 
    public void setDepth(int depth) {
        this.depth = depth;
    }
 
    public ItemList getItemList() {
        return itemList;
    }
 
    public void setItemList(ItemList itemList) {
        this.itemList = itemList;
    }
 
    
}
 
cs


ItemList.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.List;
 
public class ItemList {
 
    private List<ItemVO> items;
 
    public List<ItemVO> getItems() {
        return items;
    }
 
    public void setItems(List<ItemVO> items) {
        this.items = items;
    }
 
 
 
}
 
cs




결과화면




끝! 코드가 길어서 복잡해보이지만 사실 계속 반복작업이다.... 

'프로그래밍 > Java' 카테고리의 다른 글

정보은닉과 캡슐화  (0) 2016.05.01
사이냅소프트 면접문제  (0) 2016.05.01
String 클래스  (0) 2016.01.29
대출 이자 계산기  (0) 2016.01.28
날짜 구하기  (0) 2016.01.28
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함