Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions 준혁/chapter_10.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
## 12. 컬렉션 프레임워크
### 12-1 제네릭
여러 참조 자료형을 사용할 수 있도록 프로그래밍 하는 것

제네릭

```java
// 제너릭 클래스의 선언
public class Generic<T> {
// 제너릭 타입의 멤버변수
private T i;

// 제너릭 타입의 매개변수
public void setI(T i) {
this.i = i;
}

// 제너릭 타입을 반환하는 함수
public T getI() {
return i;
}
}
```

제네릭 클래스의 사용

```java
Generic<Integer> i = new Generic<Integer>();
i.setI(new Integer(100));
Integer j = i.getI();
```

제너릭 타입을 특정 자료형으로 제한할 수 있다
```java
public class Generic<T extends Integer>
```

### 12-2 컬렉션 프레임워크

자바에서 제공하는 자료구조 라이브러리

`Collection` 인터페이스와 `Map` 인터페이스을 기반으로 구성되어 있음

- `Collection` 인터페이스
- `List` 인터페이스
- `Set` 인터페이스
- `Map` 인터페이스
- `key`와 `value`를 가짐

### 12-3 List 인터페이스
- ArrayList 클래스 : 객체 배열을 구현한 클래스, 컬렉션 인터페이스와 List 인터페이스를 구현
- LinkedList 클래스 : 링크드 리스트를 클래스를 구현한 클래스

### 12-4 Set 인터페이스
- HashSet 클래스 : 집합 자료 구조를 구현하고 중복을 허용하지 않음
- TreeSet 클래스 : 자료의 중복을 허용하지 않으면서 출력 결과 값을 정렬하는 클래스

### 12-5 Map 인터페이스
- HashMap 클래스 : Hash 형태로 자료를 관리하는 Map 인터페이스를 구현한 클래스
- TreeMap 클래스 : 이진 검색 트리로 구성된 key 값으로 자료를 정렬한 클래스
99 changes: 99 additions & 0 deletions 준혁/chapter_11.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
## 13. 내부 클래스, 람다식, 스트림
### 13-1 내부 클래스

> 클래스 내부에 선언된 클래스

```java
class outer {
class inner {}
}
```

- 익명 내부 클래스
```java
class outer {
type getType() {
return new type() {
@override
public void run() {
// todo
}
};
}
}
```

### 13-2 람다식

> 익명 함수

```java
(int x, int y) -> {return x + y};
```

- 괄호 생략 문법 (매개변수가 2개 이상인 경우 불가)
```java
str -> {System.out.println(str);}
```

- 중괄호 생략 문법 (구현이 한 줄일 경우만 가능)
```java
str -> System.out.println(str);
```

- return 예약어 생략 문법
```java
(x, y) -> x + y;
str -> str.length();
```

- 함수형 인터페이스 생성 시 내부 메서드는 하나만 존재해야한다.
```java
@FuntionalInterface // 함수형 인터페이스에 2개 이상의 메서드 선언을 막기위한 어노테이션
interface Inter {
int add (int a, int b);
}

public class a {
public static void main(string[] args) {
int a = 1;
int b = 2;
// 람다식 사용 시 인터페이스를 정의하는 클래스없이 바로 인스턴스를 만들어 사용한다.
Inter inter = (x, y) -> {return x + y};
System.out.println(inter.add(a, b));
}
}
```

### 13-3 스트림

> 여러 자료 처리에 대한 기능을 구현해 놓은 클래스

```java
int[] arr = {1,2,3,4,5};
// stream을 이용해 배열의 요소를 하나씩 출력하는 foreach문
Arrays.stream(arr).foreach(n -> System.out.println(n));
```

- 스트림 연산
- 중간 연산
```java
// filter 연산
sList.stream().filter(s -> s.length() >= 5).foreach(s -> System.out.println(s));
// map 연산
customerList.stream().map(c -> c.getName()).foreach(s -> System.out.println(s));
```

- 최종 연산
```java
Arrays.stream(arr).sum(); // 요소의 총합
Arrays.stream(arr).count(); // 요소의 개수(long 타입)
Arrays.stream(arr).foreach(n -> System.out.println(n));
// 등등...
```

#### 스트림 특징
- 대상과 관련없이 동일한 연산 수행
- 재사용 불가
- 기존 자료 변경 X
- 중간 연산과 최종 연산이 존재함
152 changes: 152 additions & 0 deletions 준혁/chapter_12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
## 14. 예외 처리
### 14-1 예외 클래스

> 예외 클래스의 계층 구조
> ![](./img/chapter_12.png)

### 14-2 예외 처리하기

- try-catch 문
```java
try {
// 예외가 발생할 수 있는 코드 구현
} catch(예외 타입 e) {
// 예외가 발생했을 때 예외를 처리할 코드
}
```

- 예외 처리 실습
```java
public static void main(String[] args) {
int[] arr = new int[5];
// ArrayIndexOutOfBoundsException 예외 발생
try {
for (int i = 0; i <= 5; i++) {
arr[i] = i;
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
System.out.println("프로그램 종료");
}
```

```bash
# 결과
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
프로그램 종료
```

- finally 실습
```java
public static void main(String[] args) {
int[] arr = new int[5];
// ArrayIndexOutOfBoundsException 예외 발생
try {
for (int i = 0; i <= 5; i++) {
arr[i] = i;
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
} finally {
System.out.println("예외 여부 관계 없이 항상 실행");
}
}
```

```bash
# 결과
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
예외 여부 관계 없이 항상 실행
```

- try-with-resources문
시스템 리소스를 close 하지 않아도 try 내부에서 열린 리소스를 자동으로 닫아줌 (python `with`랑 비슷한 듯)
```java
try(ReSrc obj = new ReSrc()) {
} catch {
}
```

### 14-3 예외 처리 미루기

- throw 사용하기
```java
// throws 를 사용하여 예외 상황 정의
public Class loadClass(String s1, String s2) throws FileNotFoundException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(s1);
Class c = Class.forName(s2);
return c;
}

public static void main(String[] args) {
day11 test = new day11();
try {
test.loadClass("a.txt", "java.lang.String");
// 예외 처리
} catch (FileNotFoundException | ClassNotFoundException e) {
System.out.println(e);
}
}
```

- 다중 예외 처리
catch를 여러번 넣으면서 예외 처리를 여러번 할 수 있다.
```java
try {
} catch(FileNotFoundException e) {
} catch(ClassNotFoundException e) {
} catch(Exception e) {
}
```

### 14-4 사용자 정의 예외

- 상속을 통한 예외 상황 정의
```java
class IDFormatException extends Exception {
    public IDFormatException(String message) {
        super(message);
    }
}
```

- 실습 코드 작성
```java
private String userId;

String getUserId() {
return userId;
}

void setUserId(String userId) throws IDFormatException {
if (userId == null) {
throw new IDFormatException("ID는 비울 수 없습니다.");
} else if (userId.length() < 8 || userId.length() > 20) {
throw new IDFormatException("아이디는 8자 이상 20자 이하여야 합니다.");
}
this.userId = userId;
}

public static void main(String[] args) {
day11 day11 = new day11();
String userid = null;
try {
day11.setUserId(userid);
} catch(IDFormatException e) {
System.out.println(e);
}
userid = "aaaa";
try {
day11.setUserId(userid);
} catch(IDFormatException e) {
System.out.println(e);
}
}
```

```bash
# 결과
IDFormatException: ID는 비울 수 없습니다.
IDFormatException: 아이디는 8자 이상 20자 이하여야 합니다.
```
70 changes: 70 additions & 0 deletions 준혁/chapter_13.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
## 15. 자바 입출력
### 15-1 자바 입출력과 스트림
- 입출력 장치의 호환성을 보장하기 위해 만들어진 스트림
- 입력, 출력, 바이트, 문자, 기반, 보조 스트림이 존재함

### 15-2 표준 입출력
- `System.out` : 표준 출력 스트림
- `System.in` : 표준 입력 스트림
- `System.err` : 표준 오류 스트림

그 외 클래스

- `Scanner` : `java.util` 에서 제공하는 입력 스트림 클래스
- `Console` : 입력 스트림 클래스

### 15-3 바이트 단위 스트림

- `InputStream` : 바이트 단위로 읽는 스트림 중 최상위
- `FileInputStream` : 파일 읽기
```java
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("input.txt");
System.out.println(fis.read());
} catch(IOException e) {
System.out.println(e);
} finally {
try {
fis.close();
} catch(IOException e) {
System.out.println(e);
} catch(NullPointerException e) {
System.out.println(e);
}
System.out.println("end");
}
}
```
- `OutputStream`
- `FileOutputStream`

### 15-4 문자 단위 스트림

- `Reader`
- `FileReader`
- `Writer`
- `FileWriter`

### 15-5 보조 스트림

- `FilterInputStream`, `FilterOutputStream` : 보조 스트림의 상위 클래스
- `BufferdStream` : 버퍼 기능을 추가해 더 빠른 입출력을 제공
- `Data[Input, Output]Stream` : 메모리에 저장된 0, 1 상태를 그대로 읽고 씀.

### 15-6 직렬화

> 인스턴스의 순간 상태를 그대로 저장하거나 네트워크를 통해 전송하는 것.

- 역직렬화 : 저장된 내용이나 전송받은 내용을 다시 복원하는 것

생성자

- `ObjectInputStream`
- `ObjectOutputStream`

### 15-7 그 외 입출력 클래스

- `File` 클래스
- `RandomAccessFile` 클래스
Loading