Sparta/What I Learned

22.12.5

코딩하는 또롱이 2022. 12. 5. 23:43
예외처리

▶️ 에외 처리를 하는 이유

  1. 예외의 발생으로 인한 실행 중인 프로그램의 비정상 종료를 막기 위해서
  2. 개발자에게 알려서 코드를 보완할 수 있도록 하게 위해서

▶️예외처리 특징

  • finally 구문은 필수는 아니다. 
  • 별다른 예외가 발생 하지 않는다면 try-finally 순으로 진행된다.
  • catch를 여러 개 사용하여 다양한 예외들이 발생할 때마다 각각 처리 할 수 있다.
  • try-catch-finally문과 try-with-resource문 2가지가 존재한다.

▶️try-catch-finally문

public class Main {
    public static void main(String[] args) {
        int number = 10;
        int result;

        for(int i=10; i >= 0; i--){
            try{
                result = number / i;
                System.out.println(result);
            }catch (Exception e){
                System.out.println("Exception : "+ e.getMessage());
            }finally {
                System.out.println("Always");
            }
        }
    }
}
더보기

> Task :Main.main()
1
Always
1
Always
1
Always
1
Always
1
Always
2
Always
2
Always
3
Always
5
Always
10
Always
Exception : / by zero
Always

▶️  try-with-resource문

  • 입출력과 함께 자주 쓰이는 구문.
  • 기존의 try-catch(-finally)문은 자원을 닫을 때 close()를 사용해야 한다. 하지만 try-with-resource문은 try문을 벗어나는 순간 자동적으로 close()가 호출됩니다.
  • 왜? → try()안의 입출력 스트림을 생성하는 로직을 작성할 때 해당 객체가 AutoClosable 인터페이스를 구현한 객체면 알아서 닫아준다.
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {

        try (FileOutputStream out = new FileOutputStream(( "test.txt"))) {
            out.write("hello sparta".getBytes());
            out.flush();
        } catch (IOException e) {
            System.out.println("IOException : " + e.getMessage());
            e.printStackTrace();
        }
    }
}

FileOutputStream은 IOException의 자손클래스이기 때문에 catch 구문을 써주지 않으면 에러난다❗

더보기

에러가 발생하지 않는 코드이기 때문에 콘솔에는 아무 것도 표시되지 않지만 왼쪽 메뉴바에 보면 test.txt 파일이 생성된 것을 확인 할 수 있다.

 

 위와 똑같은 코드를 try-catch-finally 문으로 적어주면 아래처럼 길어진다.

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("test.txt");
        try {
            // test.txt file 에 Hello Sparta 를 출력
            out.write("Hello Sparta".getBytes());
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.close();
    }
}

▶️ 메소드에서의 예외 처리 선언

void method() throws IndexOutOfBoundsException, IllegalArgumentException {
        //메소드의 내용
        }

caller 쪽에서 IndexOutofBoundsException이라는 예외가 발생한다면 IllegalArgumentException 처리 해준다는 뜻.

 

Q 1)throws 키워드를 통해서 divide() 함수에서 발생할 수 있는 exception의 종류가 무엇인지 알게 해주세요.
Q 2)Main 함수에서 try-catch 문을 이용해서, 다음 동작을 구현하세요.
-ArithmeticException이 발생할 때는 잘못된 계산임을 알리는 문구를 출력하세요.
-ArrayIndexOutOfBoundsException이 발생할 때는 현재 배열의 index범위를 알려주는 문구를 출력하세요.
class ArrayCalculation {

    int[] arr = { 0, 1, 2, 3, 4 };

    public int divide(int denominatorIndex, int numeratorIndex) {
        return arr[denominatorIndex] / arr[numeratorIndex];
    }
}

public class Main {
    public static void main(String[] args) {
        ArrayCalculation arrayCalculation = new ArrayCalculation();

        System.out.println("2 / 1 = " + arrayCalculation.divide(2, 1));
        System.out.println("1 / 0 = " + arrayCalculation.divide(1, 0)); // java.lang.ArithmeticException: "/ by zero"
        System.out.println("Try to divide using out of index element = "
                + arrayCalculation.divide(5, 0)); // java.lang.ArrayIndexOutOfBoundsException: 5
    }

}

<정답>

더보기
class ArrayCalculation {

    int[] arr = {0, 1, 2, 3, 4};

    public int divide(int denominatorIndex, int numeratorIndex) throws ArithmeticException, ArrayIndexOutOfBoundsException {
        return arr[denominatorIndex] / arr[numeratorIndex];
    }
}

public class Main {
    public static void main(String[] args) {

        ArrayCalculation arrayCalculation = new ArrayCalculation();

        System.out.println("2 / 1 = " + arrayCalculation.divide(2, 1));

        try {
            System.out.println("1 / 0 = " + arrayCalculation.divide(1, 0)); // java.lang.ArithmeticException: "/ by zero"
        } catch (ArithmeticException arithmeticException) {
            System.out.println("Exception : " + arithmeticException.getMessage());
        }
        try {
            System.out.println("Try to divide using out of index element = " + arrayCalculation.divide(5, 0)); // java.lang.ArrayIndexOutOfBoundsException: 5
        } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) {
            System.out.println("Wrong Index. Index's length is " + (arrayCalculation.arr.length - 1));
        }
    }

}

 

 

 

시간, 날짜

▶️now() vs of()

예제에서 사용한 now()  of()는 객체를 생성할 때 사용된다. now()는 현재의 날짜 시간을 of()는 지정하는 값이 필드에 담겨서 출력된다.

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {

        System.out.println("now() usages");
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        LocalDateTime dateTime = LocalDateTime.now();

        System.out.println(time);
        System.out.println(date);
        System.out.println(dateTime);

        System.out.println("of() usages");
        LocalDate dateof = LocalDate.of(2022,12,5);
        LocalTime timeof = LocalTime.of(22,12,5);

        System.out.println(dateof);
        System.out.println(timeof);
    }
}
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class Main {
    public static void main(String[] args) {

        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
        String shortFormat = formatter.format(LocalTime.now());
        System.out.println(shortFormat);

    }
}

 

뭐라고 나오는 걸까,,,? 띠용 떄용 내생각엔 오후?? 아닐까???

 

이 망할 인텔리제이,, 한글이 됐ㄷ가 안됐다가ㅜㅜ흑흑

 

Ctrl을 누르채 FormatStyle을 클릭하면 설명이 뜬다! 끼얏호! 

/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * This file is available under and governed by the GNU General Public
 * License version 2 only, as published by the Free Software Foundation.
 * However, the following notice accompanied the original version of this
 * file:
 *
 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  * Neither the name of JSR-310 nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package java.time.format;

/**
 * Enumeration of the style of a localized date, time or date-time formatter.
 * <p>
 * These styles are used when obtaining a date-time style from configuration.
 * See {@link DateTimeFormatter} and {@link DateTimeFormatterBuilder} for usage.
 *
 * @implSpec
 * This is an immutable and thread-safe enum.
 *
 * @since 1.8
 */
public enum FormatStyle {
    // ordered from large to small

    /**
     * Full text style, with the most detail.
     * For example, the format might be 'Tuesday, April 12, 1952 AD' or '3:30:42pm PST'.
     */
    FULL,
    /**
     * Long text style, with lots of detail.
     * For example, the format might be 'January 12, 1952'.
     */
    LONG,
    /**
     * Medium text style, with some detail.
     * For example, the format might be 'Jan 12, 1952'.
     */
    MEDIUM,
    /**
     * Short text style, typically numeric.
     * For example, the format might be '12.13.52' or '3:30pm'.
     */
    SHORT;

}

🚨 왜 에러 나지?

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {

        DateTimeFormatter myFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        String myDate = myFormatter.format(LocalTime.now());
        System.out.println(myDate);

    }
}

myFormatter.format(...); 이 줄이 에러나는데 왜 에러나지? ,,,,?,,,,,? 나는 하나도 모르겟ㄷ,,, 지난주 자바 코드로 디데이 만드는것도 데이트 에러나서 캘린더로했는데에에엑!!!! ㅡ아아ㅏ아아아!!!!! 암튼 된다 치고

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {

        DateTimeFormatter myFormatter = DateTimeFormatter.ofPattern("yyyy년MM월dd일");
        String myDate = myFormatter.format(LocalTime.now());
        System.out.println(myDate);

    }
}

이렇게 포맷형식을 줘도 제대로 나온다는거,,,,(는 나만 안댐) 왜 얘만 안되지??? 

 

🚨🚨🚨🚨🚨 는 왜냐면 

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        String myDate = newFormatter.format(LocalDate.now());
        System.out.println(myDate);
    }
}

이렇게니까,,,, newFormatter,,,,,,!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!바보야!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 ");
        String myDate = newFormatter.format(LocalDate.now());
        System.out.println(myDate);
    }
}

근데 내가 뭘 만졌는진 모르겠지만 한글이 안깨진다,,, 뭘한걸까,,,?

 

 

▶️ 디데이

import java.time.LocalDate;
import java.time.Period;

public class Main {
    public static void main(String[] args) {

        LocalDate today = LocalDate.now();
        LocalDate birthday = LocalDate.of(2013,6,13);
        Period period = Period.between(today, birthday);
        System.out.println(period.getMonths());
        System.out.println(period.getDays());

        // -5 
        // -22
        // 5달 22일 전에 생일

    }
}

 

Q) 오늘의 날짜와 시간을 [연도/월/일 시간/일자]의 형식으로 값으로 출력해보세요.

 

<내가 쓴 코드>

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd hh/mm");
        String myDate = newFormatter.format(LocalDateTime.now());
        System.out.println(myDate);
    }
}

<선생님 코드>

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd h/mm");
        String myDate = newFormatter.format(LocalDateTime.now());
        System.out.println(myDate);
    }
}

 

 

컬렉션 프레임워크
List, Set, Map

▶️ 컬렉션 프레임워크란?

  • 다수의 데이터를 다루기 위한 자료구조를 표현하고 사용하는 클래스의 집합을 의미
  • 컬렉션 프레임워크의 모든 클래스는 Collection interface를 구현(implement)하는 클래스 또는 인터페이스이다.

▶️ 컬렉션 인터페이스와 자료 구조 

  • List : 순서가 있는 데이터의 집합. 데이터의 중복 O
    → ArrayList, LinkedList, Stack 등
  • Set : 순서를 유지하지 않는 데이터의 집합. 데이터 중복 X
    → HashSet, TreeSet 등
  • Map : 순서를 유지하지 않는 키(key)와 값(value)의 쌍으로 이루어진 데이터의 집합. 키 중복 X, 값 중복 O
    → HashMap, TreeMap 등
  • Stack : 마지막에 넣은 데이터를 먼저 꺼내는 자료구조, LIFO(Last In First Out)
    → Stack, ArrayDeque 
  • Queue : 먼저 넣은 데이터를 먼저 꺼내는 자료구조, FIFO(First In First Out)
    → Queue, ArrayDeque 등
  • 컬렉션 인터페이스에는 컬렉션 클래스에 저장된 데이터를 읽고, 추가하고 삭제하는 등 데이터를 다루는데 기본적인 메소드들을 정의하고 있다. 

 

 

List

👉 순서를 유지하는 데이터의 집합
👉 중복 가능
👉 ArrayList 배열을 이용하여 데이터를 저장
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        List<Integer> integerList = new ArrayList<>();
        integerList.add(1);
        integerList.add(5);
        integerList.add(4);
        integerList.add(11);
        integerList.add(10);

        System.out.println(integerList);

        Collections.sort(integerList); // ㅡ 기본적으로 오름차순 정렬
        System.out.println(integerList);
        System.out.println(integerList.size());

        integerList.remove(4);
        System.out.println(integerList);

//        for (int i=0; i<=integerList.size(); i++){
//            System.out.println(integerList.get(i));
//        }

        for (int current: integerList) {
            System.out.println(current);
        }

    }

}

index 값으로 remove 가능하다.

더보기

 

마지막 foreach문의 결과 값이랑 주석 처리된 for문의 값은 똑같다.

 

 

Set

👉 순서를 유지하지 않는 데이터의 집합
👉 데이터의 중복 X
👉 HashSet은 Set 인터페이스를 구현한 대표적인 컬렉션

import java.util.HashSet; // 클래스
import java.util.Set; // 인터페이스

public class Main {
    public static void main(String[] args) {
        Set<Integer> integerSet = new HashSet<>();
        //객체를 담을 수 있는 타입이어야만 한다. = 참조형 자료
        integerSet.add(1);
        integerSet.add(1);
        integerSet.add(3);
        integerSet.add(2);
        integerSet.add(9);
        integerSet.add(8);
        System.out.println(integerSet);
        // [1, 2, 3, 8, 9]
    }
}

add한 순서대로 나타나지 않고, 중복된 숫자는 한 개만 나옴을 알 수 있다.

import java.util.ArrayList;
import java.util.HashSet; // 클래스
import java.util.List;
import java.util.Set; // 인터페이스

public class Main {
    public static void main(String[] args) {
        Set<String> stringSet = new HashSet<>();
        //객체를 담을 수 있는 타입이어야만 한다. = 참조형 자료
        stringSet.add("LA");
        stringSet.add("NewYork");
        stringSet.add("San Francisco");
        stringSet.add("LasVegas");
        stringSet.add("Seoul");
        stringSet.add("Seoul");
        System.out.println(stringSet);
        // [San Francisco, LasVegas, LA, Seoul, NewYork]

        stringSet.remove("LA");
        System.out.println(stringSet);
        // [San Francisco, LasVegas, Seoul, NewYork]

        List<String> target = new ArrayList<>();
        target.add("LasVegas");
        target.add("Seoul");
        stringSet.removeAll(target);
        System.out.println(stringSet);
        // [San Francisco, NewYork]

        System.out.println("LA가 포함되어 있나요? " + stringSet.contains("LA"));
        System.out.println("NewYork이 포함되어 있나요? " + stringSet.contains("NewYork"));
        // LA가 포함되어 있나요? false
        // NewYork이 포함되어 있나요? true

        System.out.println(stringSet.size());
        // 2
    }
}

순서가 없으므로 index 값이 아닌 정확한 데이터 값을 이용해 remove 해 줘야한다.

contains()를 이용해 데이터가 들어있는지 확인할 수 있다.

size()를 이용해 길이를 알 수 있다.

 

 

Map
👉 키(key)와 값(value)을 하나의 데이터로 저장
👉 키(key) 중복 X (SQL로 치자면 프라이머리 키 = 주 키)
👉 값(value) 중복 O
👉 이러한 특징으로 인해 데이터를 검색하는데 뛰어난 성능을 보임
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "apple");
        map.put(2, "strawberry");
        map.put(3, "grape");
        map.put(4, "cherry");
        map.put(5, "mango");

        System.out.println(map);

        System.out.println("1st in map : " + map.get(0)); // index가 아니라 key값을 줘야하니까 null이 나옴!
        System.out.println("1st in map : " + map.get(1));

        map.remove(5);
        System.out.println(map);

        System.out.println(map.containsKey(5));
        System.out.println(map.containsValue("cherry"));
    }
}

put()은 key 값을 받아 value 값을 반환 시켜준다.

 

'Sparta > What I Learned' 카테고리의 다른 글

22.12.7  (0) 2022.12.07
22.12.6  (0) 2022.12.07
22.12.4  (0) 2022.12.05
22.12.2  (0) 2022.12.03
22.12.1  (0) 2022.12.02