티스토리 뷰

주어진 문자열(공백 없이 쉼표로 구분되어 있음)을 가지고 아래 문제에 대한 프로그램을 작성하세요.

이유덕,이재영,권종표,이재영,박민호,강상희,이재영,김지완,최승혁,이성연,박영서,박민호,전경헌,송정환,김재성,이유덕,전경헌

  1. 김씨와 이씨는 각각 몇 명 인가요?
  2. "이재영"이란 이름이 몇 번 반복되나요?
  3. 중복을 제거한 이름을 출력하세요.
  4. 중복을 제거한 이름을 오름차순으로 정렬하여 출력하세요.


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
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
 
public class Example {
 
    public static void main(String[] args) {
        
        Example main = new Example();
        main.start();
    }
 
    private void start() {
        
        String names = "이유덕,이재영,권종표,이재영,"
                + "박민호,강상희,이재영,김지완,최승혁,이성연,박영서,"
                + "박민호,전경헌,송정환,김재성,이유덕,전경헌";
        
        String[] nameList = names.split(",");
        
        int count_kim = 0;
        int count_lee = 0;
        int count_ljy = 0;
        
        List<String> list = new ArrayList<String>();
        
        for(int i = 0; i < nameList.length; i++){
            if(nameList[i].startsWith("김")){
                count_kim ++;
            }
            if(nameList[i].startsWith("이")){
                count_lee ++;
            }
            if(nameList[i].equals("이재영")){
                count_ljy++;
            }
            list.add(nameList[i]);
        }
        
        System.out.println("김씨:" + count_kim);
        System.out.println("이씨:" + count_lee);
        System.out.println("이재영:" + count_ljy);
        
        //중복제거
        List<String> uniqueNames = new ArrayList<String>(new HashSet<String>(list));
        System.out.println("중복제거:"+uniqueNames);
        
        //오름차순정렬
        Collections.sort(uniqueNames);
        System.out.println("정렬:"+uniqueNames);
    }
}
 
 
cs



결과


김씨:2

이씨:6

이재영:3

중복제거:[김재성, 김지완, 권종표, 박민호, 박영서, 송정환, 이유덕, 이재영, 이성연, 전경헌, 강상희, 최승혁]

정렬:[강상희, 권종표, 김재성, 김지완, 박민호, 박영서, 송정환, 이성연, 이유덕, 이재영, 전경헌, 최승혁]



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

정보은닉과 캡슐화  (0) 2016.05.01
재귀호출  (0) 2016.03.17
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
글 보관함