이글은 백기선님 스터디 할래? 스터디를 뒤늦게 따라간 기록입니다.
스터디할래 링크 >> https://github.com/whiteship/live-study
이필기를 시작으로 공부했습니다 >> https://www.notion.so/Live-Study-5-75f857b63e524d33914a8b3ec6e1e894
여기 없는내용은 스터디 할래 강의 내용 혹은 제가 java Doc보고작성하거나 예제를 만들어 추가했습니다.
그외는 같이 출처가 써있습니다.
LinkedList vs ArrayList | Vactor: Java1.0 시작 생성시 capacity 10로 정해져 10개씩 늘어남. thread에 대한 접근 동기화 보장 성능이슈 Java1.2 : List 인터페이스 대체 Collections.synchronizedList(List<T> list); // Thread-safe 가필요할 때사용. ArrayList :내부적으로 데이터를 배열에서 관리, 추가 삭제를 위해 임시배열을 생성 데이터 복사 *** 대량의 자료를 추가/삭제 하는 경우 그만큼 데이터의 복사가 많이 일어나 성능저하 *** 반면 각 데이터는 인덱스를 가지고 있기 때문에 한번에 참조가 가능 검색에는 유리 LinkedList: 데이터를 저장하는 각 노드가 이전 노드와 다음 노드의 상태만 알고 있다. ***추가 삭제 유리하지만 검색시 처음부터 노드를 순회해야함 성능상 불리 |
접근제어자 | public 동일클래스, 동일패키지, 자손클래스, 그외의 영역 protected 동일클래스 , 동일패키지, 자손클래스 (default) 동일클래스, 동일 패키지 >>자바파일안 클래스 동등두개 선언시 파일명 다른것. private 동일클래스 |
초기화블럭 vs 생성자 | public class Init { private int number; private static int staticNum; {System.out.println("초기"); } static {System.out.println("정적초기");} public Init(){ System.out.println("생성");} public static void main(String[] args) { Init init = new Init(); } } // 정적초기 > 초기 > 생성 |
this() | 생성자에서 다른생성자 호출. public Init(){} public Init(String start){ this(); } - this() 는 숨어있지않음, super()만 숨어있음 - this() 호출은 첫줄에서 |
기본생성자 | 선언하지 않아도 기본생성자 자동생성 변수가 들어간 생성자가 생기면 기본생성자를 자동 생성하지 않음 |
final | public final class String :: 상속 막을 때 상속이 적절하지 않을때 :: 특수한 String이 생겨버림. |
abstract | public abstract class Init ::인스턴스 생성막을 떄 |
Nested Class | 중첩클래스 : 클래스 내부 다른클래스 1. static nested class 2. inner class |
static nested class | public class Outer { public static class Nested { } } |
InnerClass | inner class >> Outter 호출후 사용. public class OuterClass{ static class StaticNestedCalss{} class InnerClass{ class InnerInnerClass{} } } >>컴파일시 클래스파일 각각 3개 생성 OuterClass$InnerClass$InnerInnerClass.class OuterClass$InnerClass.class OuterClass.class |
Local class | 코드블럭 안에 정의된 클래스. 특정메소드 내에서만 필요한 객체가 필요할때 public class Main{ public static boolean methodExample(String... values){ Class Value{ } } } |
Anonymous class | 익명클래스= 이름없는 클래스 클래스의 정의와 인스턴스화를 한번에 작성. 인스턴스 구현, 추상클래스상속한 처리 부분적이용. |
만약 런타임시 클래스의 정보를 알고싶다면? | Reflection API > 구체적 클래스 타입을 알지 못해도 클래스정보(생성자,필드,메서드) 접근할수 있게 해주는 자바 API, 상황에 따라 인스턴스 유동적 생성 https://woowacourse.github.io/javable/post/2020-07-16-reflection-api/#:~:text=Reflection%20API%EB%9E%80%3F,%EC%9E%88%EA%B2%8C%20%ED%95%B4%EC%A3%BC%EB%8A%94%20%EC%9E%90%EB%B0%94%20API%EB%8B%A4. |
Java Reflection API | 상기링크 참조 import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method Class carClass = Car.class; Method move = carClass.getMethod("move"); move.invoke(obj, null); import java.lang.reflect.Field; Field[] fields = Class.forName("java.com.whiteship.demo.Init").getDeclaredFields(); 리플렉션을 사용하기 때문에 실제 Exception이 일어날 수 없어 Exception 처리를 용이하기 위함입니다. >>리플렉션 사용예 .https://woowabros.github.io/experience/2020/10/08/excel-download.html |
Heap 메모리 | Heap 자료구조를 쓰기 때문. _ 완전이진트리. 항상 위의값이 아랫값보다 큼. min Heap , max Heap |
java8 변화 | 추상클래스가 없어지고 인터페이스+디폴트메소드로 옮겨가고 있음. 인터페이스 : 규약. 토비스프링3. ..참고. |
capacity 수용력
Nested 내포반복.
Reflection 반영, 비추다.
invoke 들먹이다 언급하다.
Java의 LinkedList와 ArrayList에 대한 비교 https://www.holaxprogramming.com/2014/02/12/java-list-interface/
객체지향의 사실과 오해.. 책 찾아볼것.
package com.whiteship.demo;
import com.sun.xml.internal.ws.api.ha.StickyFeature;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/*int 값을 가지고 있는 이진 트리를 나타내는 Node 라는 클래스를 정의하세요.
int value, Node left, right를 가지고 있어야 합니다.
BinrayTree라는 클래스를 정의하고 주어진 노드를 기준으로 출력하는 bfs(Node node)와 dfs(Node node) 메소드를 구현하세요.
DFS는 왼쪽, 루트, 오른쪽 순으로 순회하세요.*/
class Node_Binary{
private int value;
private Node_Binary left;
private Node_Binary right;
public Node_Binary(int value, Node_Binary left, Node_Binary right){
this.value=value;
this.left = left;
this.right=right;
}
public Node_Binary(int value){
this.value=value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node_Binary getLeft() {
return left;
}
public void setLeft(Node_Binary left) {
this.left = left;
}
public Node_Binary getRight() {
return right;
}
public void setRight(Node_Binary right) {
this.right = right;
}
}
public class BinaryTree {
private Node_Binary root;
public BinaryTree(Node_Binary root){
this.root=root;
}
public Node_Binary getRoot() {
return root;
}
public void getDFS (Node_Binary node, List<String> resultDfs){
if (node == null) return;
resultDfs.add(String.valueOf(node.getValue()));
getDFS(node.getLeft(), resultDfs);
getDFS(node.getRight(), resultDfs);
}
public List<String> getBFS (Node_Binary node, List<String> resultBfs){
Queue<Node_Binary> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
node = queue.poll();
resultBfs.add(String.valueOf(node.getValue()));
if (node.getLeft() !=null){
queue.offer(node.getLeft());
}
if (node.getRight() != null){
queue.offer(node.getRight());
}
}
return resultBfs;
}
}
package com.whiteship.demo;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
class BinaryTreeTest {
private List<String> resultDfsList = Collections.EMPTY_LIST;
// 1 - 2 -4,5
// - 3-6,7
private BinaryTree getUpBinaryTreeValue(){
Node_Binary node4 = new Node_Binary(4);
Node_Binary node5 = new Node_Binary(5);
Node_Binary node6 = new Node_Binary(6);
Node_Binary node7 = new Node_Binary(7);
Node_Binary node2 = new Node_Binary(2,node4,node5);
Node_Binary node3 = new Node_Binary(3,node6,node7);
Node_Binary root = new Node_Binary(1,node2,node3);
return new BinaryTree(root);
}
@Test
void TEST_DFS (){
BinaryTree binaryTree = getUpBinaryTreeValue();
binaryTree.getDFS(binaryTree.getRoot(), resultDfsList);
String resultDFS = String.join("",resultDfsList);
assertEquals("1245367", resultDFS);
}
@Test
void TEST_BFS(){
BinaryTree binaryTree = getUpBinaryTreeValue();
List<String> resultBfsList = binaryTree.getBFS(binaryTree.getRoot(), new ArrayList<>());
String resultBfs = String.join("",resultBfsList);
assertEquals("1234567",resultBfs);
}
}
목표
자바의 Class에 대해 학습하세요.
학습할 것 (필수)
- 클래스 정의하는 방법
- 객체 만드는 방법 (new 키워드 이해하기)
- 메소드 정의하는 방법
- 생성자 정의하는 방법
- this 키워드 이해하기
마감일시
2020년 12월 19일 토요일 오후 1시까지.
과제 (Optional)
- int 값을 가지고 있는 이진 트리를 나타내는 Node 라는 클래스를 정의하세요.
- int value, Node left, right를 가지고 있어야 합니다.
- BinrayTree라는 클래스를 정의하고 주어진 노드를 기준으로 출력하는 bfs(Node node)와 dfs(Node node) 메소드를 구현하세요.
- DFS는 왼쪽, 루트, 오른쪽 순으로 순회하세요.
'JAVA > Basic' 카테고리의 다른 글
[스터디할래? Java 07]패키지 (0) | 2021.05.14 |
---|---|
[스터디할래? Java 06]상속 (0) | 2021.05.14 |
[스터디할래? Java 04] 제어문 (0) | 2021.04.14 |
[스터디할래? Java 03] 연산자 Operator (0) | 2021.04.14 |
[스터디할래? Java 02] 자바 데이터 타입, 변수 그리고 배열 (0) | 2021.04.13 |