Backend/JAVA

[JAVA] 특강 16일차 : 코딩 훈련! 문제 풀이 (① 배열)

JOKUN 2022. 7. 11. 19:00

 

Array 배열

하나의 클래스에 메소드로 정의하여 메소드 호출하여 실행

📑문제

 method1

 

 method2

 

 method3

 

 method4

 method3과 같은 문제! List활용하여 다른 풀이

 

 

 

 

 

🔍풀이

import java.util.*;
import java.util.stream.Stream;

public class Practice4 {
    private static Scanner sc;
    public static void main(String[] args) {
        sc = new Scanner(System.in);

//        method1();
//        method2();
//        method3();
        method4(); 
    }

    private static void method1() {
        Set<Integer> lottoSets = new HashSet<>();
        Random ran = new Random();
        while(true){
            int ranNum = ran.nextInt(45) + 1;
            lottoSets.add(ranNum);
            if(lottoSets.size() == 6){
                break;
            }
        }
        System.out.println("오름차순");
        lottoSets.stream().sorted().forEach(e-> System.out.print(e + " "));
        System.out.println();
        System.out.println("내림차순");
        lottoSets.stream().sorted(Comparator.reverseOrder()).forEach(e-> System.out.print(e + " "));
    }

    private static void method2(){
        System.out.print("문자열 : ");
        String inputText = sc.nextLine();

        System.out.print("문자열에 있는 문자 : ");
        char[] charArray = inputText.toCharArray();
        List<Character> charList = new ArrayList<>();
        for(char ch : charArray){
            charList.add(ch);
        }
        charList.stream().distinct().forEach(e -> {
            System.out.print(e + " ");
        });
        System.out.println();
        System.out.println("문자 개수 : " + charList.stream().distinct().count());
    }

    private static void method3(){
        int cnt = 0;
        String[] strArray = null;
        System.out.print("배열의 크기를 입력하세요 : ");

        while (true) {
            int arraySize = sc.nextInt();
            sc.nextLine();

            if(strArray == null){ //첫 시작
                strArray = new String[arraySize];

            } else { // 두 번째 이후
                String[] tmpArray = strArray;
                strArray = new String[tmpArray.length + arraySize];
                for (int i = 0; i < tmpArray.length; i++) {
                    strArray[i] = tmpArray[i];
                }
            }

            for (int i = 0; i < arraySize; i++) {
                System.out.print((cnt + 1) + "번째 문자열 : ");
                strArray[cnt++] = sc.nextLine();
            }

            System.out.print("더 값을 입력하시겠습니까?(Y/N : ");
            String yn = sc.nextLine();
            yn =  yn.toLowerCase();
            if(yn.equals("y")){
                System.out.print("더 입력하고싶은 개수 : ");
            }else {
                System.out.println(Arrays.toString(strArray));
                break;
            }

        }



    }

    private static void method4(){
        List<String> strList = new ArrayList<>();

        System.out.print("배열의 크기를 입력하세요 : ");

        while (true) {
            int arraySize = sc.nextInt();
            sc.nextLine();

            for (int i = 0; i < arraySize; i++) {
                int size = strList.size();
                System.out.print((size + 1) + "번째 문자열 : ");
                strList.add(sc.nextLine());
            }

            System.out.print("더 값을 입력하시겠습니까?(Y/N : ");
            String yn = sc.nextLine();
            yn =  yn.toLowerCase();
            if(yn.equals("y")){
                System.out.print("더 입력하고싶은 개수 : ");
            }else {
                System.out.print("[");
                for (int i = 0; i < strList.size(); i++) {
                System.out.print(strList.get(i));
                    if(i != strList.size() - 1){
                        System.out.print(", ");
                    }
                }
                System.out.print("]");
                break;
            }
        }
    }

}